updated vmail backend

This commit is contained in:
Paul 2021-04-05 21:33:28 +02:00
parent 40f2487786
commit c3e5c0060d
6 changed files with 73 additions and 68 deletions

View File

@ -23,9 +23,7 @@ func Initialize(ctx *context.Context, config *config.Config) (err error) {
config.Db.SetMapper(names.GonicMapper{}) config.Db.SetMapper(names.GonicMapper{})
config.Db.SetQuotePolicy(dialects.QuotePolicyReserved) config.Db.SetQuotePolicy(dialects.QuotePolicyReserved)
if config.Debug { config.Db.ShowSQL(config.Debug)
config.Db.ShowSQL(true)
}
err = models.NewEngine(ctx, config) err = models.NewEngine(ctx, config)

View File

@ -13,7 +13,7 @@ import (
// GetAdmins return list of apiadmins // GetAdmins return list of apiadmins
func GetAdmins(ctx *context.Context, config *config.Config) (apiadmins []*api.Admin, err error) { func GetAdmins(ctx *context.Context, config *config.Config) (apiadmins []*api.Admin, err error) {
var admins []Admin var admins []Admin
err = config.Db.Find(&admins) err = config.Db.OrderBy(keyname).Find(&admins)
for _, adm := range admins { for _, adm := range admins {
apiadmins = append(apiadmins, adm.APIFormat()) apiadmins = append(apiadmins, adm.APIFormat())
} }

View File

@ -3,6 +3,7 @@ package models
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"git.paulbsd.com/paulbsd/vmail/src/api" "git.paulbsd.com/paulbsd/vmail/src/api"
@ -10,24 +11,28 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
// GetAliases ... // GetAliases get aliases from vmail
func GetAliases(ctx *context.Context, config *config.Config) (apialiases []*api.Alias, err error) { func GetAliases(ctx *context.Context, config *config.Config, limit string) (apialiases []*api.Alias, err error) {
var aliases []Alias var aliases []Alias
err = config.Db.Find(&aliases) l, _ := strconv.Atoi(limit)
if l == 0 {
l = defaultlimit
}
err = config.Db.Limit(l, 0).OrderBy(keyname).Find(&aliases)
for _, al := range aliases { for _, al := range aliases {
apialiases = append(apialiases, al.APIFormat()) apialiases = append(apialiases, al.APIFormat())
} }
return return
} }
// GetAliasModel ... // GetAliasModel get alias model from vmail
func GetAliasModel(ctx *context.Context, config *config.Config) (apialias *api.Alias, err error) { func GetAliasModel(ctx *context.Context, config *config.Config) (apialias *api.Alias, err error) {
var alias Alias var alias Alias
apialias = alias.APIFormat() apialias = alias.APIFormat()
return return
} }
// GetAlias ... // GetAlias get alias from vmail
func GetAlias(ctx *context.Context, config *config.Config, id interface{}) (apialias *api.Alias, err error) { func GetAlias(ctx *context.Context, config *config.Config, id interface{}) (apialias *api.Alias, err error) {
var alias Alias var alias Alias
has, err := config.Db.Where(fmt.Sprintf("%s = ?", keyname), id).Get(&alias) has, err := config.Db.Where(fmt.Sprintf("%s = ?", keyname), id).Get(&alias)
@ -38,7 +43,7 @@ func GetAlias(ctx *context.Context, config *config.Config, id interface{}) (apia
return return
} }
// CreateAlias ... // CreateAlias creates alias on vmail
func CreateAlias(ctx *context.Context, config *config.Config) (num int64, err error) { func CreateAlias(ctx *context.Context, config *config.Config) (num int64, err error) {
var alias Alias var alias Alias
num, err = config.Db.Insert(&alias) num, err = config.Db.Insert(&alias)
@ -48,20 +53,22 @@ func CreateAlias(ctx *context.Context, config *config.Config) (num int64, err er
return return
} }
// UpdateAlias ... // UpdateAlias updates alias on vmail
func UpdateAlias(ctx *context.Context, config *config.Config, c echo.Context) (num int64, err error) { func UpdateAlias(ctx *context.Context, config *config.Config, id string, c echo.Context) (apialias *api.Alias, err error) {
var alias Alias var alias Alias
var apialias = new(api.Alias) apialias = new(api.Alias)
if err = c.Bind(apialias); err != nil { if err = c.Bind(apialias); err != nil {
fmt.Println(err)
return return
} }
alias.APIParse(*apialias) alias.APIParse(*apialias)
num, err = config.Db.ID(alias.ID).AllCols().Update(&alias) num, err := config.Db.ID(id).AllCols().Update(&alias)
if num < 1 {
err = fmt.Errorf("%s", "No record updated")
}
return return
} }
// DeleteAlias ... // DeleteAlias deletes alias on vmail
func DeleteAlias(ctx *context.Context, config *config.Config, id interface{}) (num int64, err error) { func DeleteAlias(ctx *context.Context, config *config.Config, id interface{}) (num int64, err error) {
var alias Alias var alias Alias
num, err = config.Db.Where(fmt.Sprintf("%s = ?", keyname), id).Delete(&alias) num, err = config.Db.Where(fmt.Sprintf("%s = ?", keyname), id).Delete(&alias)
@ -93,8 +100,8 @@ func (alias *Alias) APIFormat() *api.Alias {
Address: alias.Address, Address: alias.Address,
Goto: alias.Goto, Goto: alias.Goto,
Domain: alias.Domain, Domain: alias.Domain,
Created: alias.Created.Format(timetostring), Created: alias.Created.In(timezone).Format(timetostring),
Modified: alias.Modified.Format(timetostring), Modified: alias.Modified.In(timezone).Format(timetostring),
Active: alias.Active, Active: alias.Active,
} }
} }

View File

@ -2,13 +2,17 @@ package models
import ( import (
"fmt" "fmt"
"time"
"git.paulbsd.com/paulbsd/vmail/utils/units" "git.paulbsd.com/paulbsd/vmail/utils/units"
) )
// Mon Jan 2 15:04:05 MST 2006 var timezone, _ = time.LoadLocation("Europe/Paris")
// For template: Mon Jan 2 15:04:05 MST 2006
const timetostring string = "02/01/2006 15:04:05" const timetostring string = "02/01/2006 15:04:05"
const keyname string = "id" const keyname string = "id"
const defaultlimit = 10
func quotaFormat(in int64) (out string) { func quotaFormat(in int64) (out string) {
var converted units.Base2Bytes var converted units.Base2Bytes

View File

@ -2,7 +2,6 @@ package routers
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"git.paulbsd.com/paulbsd/vmail/src/config" "git.paulbsd.com/paulbsd/vmail/src/config"
@ -24,92 +23,81 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
// Admins // Admins
e.GET("/api/admin", func(c echo.Context) (err error) { e.GET("/api/admin", func(c echo.Context) (err error) {
admins, err := models.GetAdmins(ctx, cfg) ret, err := models.GetAdmins(ctx, cfg)
return c.JSON(http.StatusOK, admins) return JSONResult(c, err, ret)
}) })
e.GET("/api/admin/model", func(c echo.Context) (err error) { e.GET("/api/admin/model", func(c echo.Context) (err error) {
admins, err := models.GetAdminModel(ctx, cfg) ret, err := models.GetAdminModel(ctx, cfg)
return c.JSON(http.StatusOK, admins) return JSONResult(c, err, ret)
}) })
e.GET("/api/admin/:id", func(c echo.Context) (err error) { e.GET("/api/admin/:id", func(c echo.Context) (err error) {
admins, err := models.GetAdmin(ctx, cfg, c.Param("id")) ret, err := models.GetAdmin(ctx, cfg, c.Param("id"))
return c.JSON(http.StatusOK, admins) return JSONResult(c, err, ret)
}) })
e.POST("/api/admin/:id", func(c echo.Context) (err error) { e.POST("/api/admin", func(c echo.Context) (err error) {
admins, err := models.CreateAdmin(ctx, cfg, c.Param("id")) ret, err := models.CreateAdmin(ctx, cfg)
return c.JSON(http.StatusOK, admins) return JSONResult(c, err, ret)
}) })
e.PUT("/api/admin/:id", func(c echo.Context) (err error) { e.PUT("/api/admin/:id", func(c echo.Context) (err error) {
admins, err := models.UpdateAdmin(ctx, cfg, c.Param("id")) ret, err := models.UpdateAdmin(ctx, cfg, c)
return c.JSON(http.StatusOK, admins) return JSONResult(c, err, ret)
}) })
e.DELETE("/api/admin/:id", func(c echo.Context) (err error) { e.DELETE("/api/admin/:id", func(c echo.Context) (err error) {
admins, err := models.DeleteAdmin(ctx, cfg, c.Param("id")) ret, err := models.DeleteAdmin(ctx, cfg, c.Param("id"))
return c.JSON(http.StatusOK, admins) return JSONResult(c, err, ret)
}) })
// Aliases // Aliases
e.GET("/api/alias", func(c echo.Context) (err error) { e.GET("/api/alias", func(c echo.Context) (err error) {
aliases, err := models.GetAliases(ctx, cfg) ret, err := models.GetAliases(ctx, cfg, c.QueryParam("limit"))
return c.JSON(http.StatusOK, aliases) return JSONResult(c, err, ret)
}) })
e.GET("/api/alias/model", func(c echo.Context) (err error) { e.GET("/api/alias/model", func(c echo.Context) (err error) {
aliases, err := models.GetAliasModel(ctx, cfg) ret, err := models.GetAliasModel(ctx, cfg)
return c.JSON(http.StatusOK, aliases) return JSONResult(c, err, ret)
}) })
e.GET("/api/alias/:id", func(c echo.Context) (err error) { e.GET("/api/alias/:id", func(c echo.Context) (err error) {
alias, err := models.GetAlias(ctx, cfg, c.Param("id")) ret, err := models.GetAlias(ctx, cfg, c.Param("id"))
return JSONResult(c, err, alias) return JSONResult(c, err, ret)
}) })
e.POST("/api/alias", func(c echo.Context) (err error) { e.POST("/api/alias", func(c echo.Context) (err error) {
_, err = models.CreateAlias(ctx, cfg) ret, err := models.CreateAlias(ctx, cfg)
return c.JSON(http.StatusOK, nil) return JSONResult(c, err, ret)
}) })
e.PUT("/api/alias/:id", func(c echo.Context) (err error) { e.PUT("/api/alias/:id", func(c echo.Context) (err error) {
_, err = models.UpdateAlias(ctx, cfg, c) ret, err := models.UpdateAlias(ctx, cfg, c.Param("id"), c)
if err != nil { return JSONResult(c, err, ret)
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Error %s", err))
}
return c.JSON(http.StatusOK, nil)
}) })
e.DELETE("/api/alias/:id", func(c echo.Context) (err error) { e.DELETE("/api/alias/:id", func(c echo.Context) (err error) {
aliases, err := models.DeleteAlias(ctx, cfg, c.Param("id")) ret, err := models.DeleteAlias(ctx, cfg, c.Param("id"))
return c.JSON(http.StatusOK, aliases) return JSONResult(c, err, ret)
}) })
// Mailboxes // Mailboxes
e.GET("/api/mailbox", func(c echo.Context) (err error) { e.GET("/api/mailbox", func(c echo.Context) (err error) {
mailboxes, err := models.GetMailboxes(ctx, cfg) ret, err := models.GetMailboxes(ctx, cfg)
return c.JSON(http.StatusOK, mailboxes) return JSONResult(c, err, ret)
}) })
e.GET("/api/mailbox/:id", func(c echo.Context) (err error) { e.GET("/api/mailbox/:id", func(c echo.Context) (err error) {
mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id")) ret, err := models.GetMailbox(ctx, cfg, c.Param("id"))
return c.JSON(http.StatusOK, mailbox) return JSONResult(c, err, ret)
}) })
e.POST("/api/mailbox", func(c echo.Context) (err error) { e.POST("/api/mailbox", func(c echo.Context) (err error) {
mailbox, err := models.CreateMailbox(ctx, cfg) ret, err := models.CreateMailbox(ctx, cfg)
return c.JSON(http.StatusOK, mailbox) return JSONResult(c, err, ret)
}) })
e.PUT("/api/mailbox/:id", func(c echo.Context) (err error) { e.PUT("/api/mailbox/:id", func(c echo.Context) (err error) {
mailbox, err := models.UpdateMailbox(ctx, cfg, c) ret, err := models.UpdateMailbox(ctx, cfg, c)
return c.JSON(http.StatusOK, mailbox) return JSONResult(c, err, ret)
}) })
e.DELETE("/api/mailbox/:id", func(c echo.Context) (err error) { e.DELETE("/api/mailbox/:id", func(c echo.Context) (err error) {
mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id")) ret, err := models.GetMailbox(ctx, cfg, c.Param("id"))
return c.JSON(http.StatusOK, mailbox) return JSONResult(c, err, ret)
}) })
// Logs // Logs
e.GET("/api/log", func(c echo.Context) (err error) { e.GET("/api/log", func(c echo.Context) (err error) {
logs, err := models.GetLogs(ctx, cfg) ret, err := models.GetLogs(ctx, cfg)
return c.JSON(http.StatusOK, logs) return JSONResult(c, err, ret)
}) })
} }
// JSONResult handles returns and error management on backend api
func JSONResult(c echo.Context, inputerr error, data interface{}) (err error) {
if inputerr != nil {
return c.JSON(http.StatusInternalServerError, nil)
}
return c.JSON(http.StatusOK, data)
}

View File

@ -26,3 +26,11 @@ func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.Port))) e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.Port)))
return return
} }
// JSONResult handles returns and error management on backend api
func JSONResult(c echo.Context, inputerr error, data interface{}) (err error) {
if inputerr != nil {
return c.JSON(http.StatusInternalServerError, inputerr)
}
return c.JSON(http.StatusOK, data)
}