updated vmail backend
This commit is contained in:
parent
40f2487786
commit
c3e5c0060d
@ -23,9 +23,7 @@ func Initialize(ctx *context.Context, config *config.Config) (err error) {
|
||||
config.Db.SetMapper(names.GonicMapper{})
|
||||
config.Db.SetQuotePolicy(dialects.QuotePolicyReserved)
|
||||
|
||||
if config.Debug {
|
||||
config.Db.ShowSQL(true)
|
||||
}
|
||||
config.Db.ShowSQL(config.Debug)
|
||||
|
||||
err = models.NewEngine(ctx, config)
|
||||
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
// GetAdmins return list of apiadmins
|
||||
func GetAdmins(ctx *context.Context, config *config.Config) (apiadmins []*api.Admin, err error) {
|
||||
var admins []Admin
|
||||
err = config.Db.Find(&admins)
|
||||
err = config.Db.OrderBy(keyname).Find(&admins)
|
||||
for _, adm := range admins {
|
||||
apiadmins = append(apiadmins, adm.APIFormat())
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package models
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.paulbsd.com/paulbsd/vmail/src/api"
|
||||
@ -10,24 +11,28 @@ import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// GetAliases ...
|
||||
func GetAliases(ctx *context.Context, config *config.Config) (apialiases []*api.Alias, err error) {
|
||||
// GetAliases get aliases from vmail
|
||||
func GetAliases(ctx *context.Context, config *config.Config, limit string) (apialiases []*api.Alias, err error) {
|
||||
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 {
|
||||
apialiases = append(apialiases, al.APIFormat())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetAliasModel ...
|
||||
// GetAliasModel get alias model from vmail
|
||||
func GetAliasModel(ctx *context.Context, config *config.Config) (apialias *api.Alias, err error) {
|
||||
var alias Alias
|
||||
apialias = alias.APIFormat()
|
||||
return
|
||||
}
|
||||
|
||||
// GetAlias ...
|
||||
// GetAlias get alias from vmail
|
||||
func GetAlias(ctx *context.Context, config *config.Config, id interface{}) (apialias *api.Alias, err error) {
|
||||
var alias 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
|
||||
}
|
||||
|
||||
// CreateAlias ...
|
||||
// CreateAlias creates alias on vmail
|
||||
func CreateAlias(ctx *context.Context, config *config.Config) (num int64, err error) {
|
||||
var alias Alias
|
||||
num, err = config.Db.Insert(&alias)
|
||||
@ -48,20 +53,22 @@ func CreateAlias(ctx *context.Context, config *config.Config) (num int64, err er
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateAlias ...
|
||||
func UpdateAlias(ctx *context.Context, config *config.Config, c echo.Context) (num int64, err error) {
|
||||
// UpdateAlias updates alias on vmail
|
||||
func UpdateAlias(ctx *context.Context, config *config.Config, id string, c echo.Context) (apialias *api.Alias, err error) {
|
||||
var alias Alias
|
||||
var apialias = new(api.Alias)
|
||||
apialias = new(api.Alias)
|
||||
if err = c.Bind(apialias); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// DeleteAlias ...
|
||||
// DeleteAlias deletes alias on vmail
|
||||
func DeleteAlias(ctx *context.Context, config *config.Config, id interface{}) (num int64, err error) {
|
||||
var alias 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,
|
||||
Goto: alias.Goto,
|
||||
Domain: alias.Domain,
|
||||
Created: alias.Created.Format(timetostring),
|
||||
Modified: alias.Modified.Format(timetostring),
|
||||
Created: alias.Created.In(timezone).Format(timetostring),
|
||||
Modified: alias.Modified.In(timezone).Format(timetostring),
|
||||
Active: alias.Active,
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,17 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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 keyname string = "id"
|
||||
const defaultlimit = 10
|
||||
|
||||
func quotaFormat(in int64) (out string) {
|
||||
var converted units.Base2Bytes
|
||||
|
@ -2,7 +2,6 @@ package routers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.paulbsd.com/paulbsd/vmail/src/config"
|
||||
@ -24,92 +23,81 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
|
||||
|
||||
// Admins
|
||||
e.GET("/api/admin", func(c echo.Context) (err error) {
|
||||
admins, err := models.GetAdmins(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, admins)
|
||||
ret, err := models.GetAdmins(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.GET("/api/admin/model", func(c echo.Context) (err error) {
|
||||
admins, err := models.GetAdminModel(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, admins)
|
||||
ret, err := models.GetAdminModel(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.GET("/api/admin/:id", func(c echo.Context) (err error) {
|
||||
admins, err := models.GetAdmin(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, admins)
|
||||
ret, err := models.GetAdmin(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.POST("/api/admin/:id", func(c echo.Context) (err error) {
|
||||
admins, err := models.CreateAdmin(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, admins)
|
||||
e.POST("/api/admin", func(c echo.Context) (err error) {
|
||||
ret, err := models.CreateAdmin(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.PUT("/api/admin/:id", func(c echo.Context) (err error) {
|
||||
admins, err := models.UpdateAdmin(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, admins)
|
||||
ret, err := models.UpdateAdmin(ctx, cfg, c)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.DELETE("/api/admin/:id", func(c echo.Context) (err error) {
|
||||
admins, err := models.DeleteAdmin(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, admins)
|
||||
ret, err := models.DeleteAdmin(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
|
||||
// Aliases
|
||||
e.GET("/api/alias", func(c echo.Context) (err error) {
|
||||
aliases, err := models.GetAliases(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, aliases)
|
||||
ret, err := models.GetAliases(ctx, cfg, c.QueryParam("limit"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.GET("/api/alias/model", func(c echo.Context) (err error) {
|
||||
aliases, err := models.GetAliasModel(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, aliases)
|
||||
ret, err := models.GetAliasModel(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.GET("/api/alias/:id", func(c echo.Context) (err error) {
|
||||
alias, err := models.GetAlias(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, alias)
|
||||
ret, err := models.GetAlias(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.POST("/api/alias", func(c echo.Context) (err error) {
|
||||
_, err = models.CreateAlias(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, nil)
|
||||
ret, err := models.CreateAlias(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.PUT("/api/alias/:id", func(c echo.Context) (err error) {
|
||||
_, err = models.UpdateAlias(ctx, cfg, c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Error %s", err))
|
||||
}
|
||||
return c.JSON(http.StatusOK, nil)
|
||||
ret, err := models.UpdateAlias(ctx, cfg, c.Param("id"), c)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.DELETE("/api/alias/:id", func(c echo.Context) (err error) {
|
||||
aliases, err := models.DeleteAlias(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, aliases)
|
||||
ret, err := models.DeleteAlias(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
|
||||
// Mailboxes
|
||||
e.GET("/api/mailbox", func(c echo.Context) (err error) {
|
||||
mailboxes, err := models.GetMailboxes(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, mailboxes)
|
||||
ret, err := models.GetMailboxes(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.GET("/api/mailbox/:id", func(c echo.Context) (err error) {
|
||||
mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, mailbox)
|
||||
ret, err := models.GetMailbox(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.POST("/api/mailbox", func(c echo.Context) (err error) {
|
||||
mailbox, err := models.CreateMailbox(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, mailbox)
|
||||
ret, err := models.CreateMailbox(ctx, cfg)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.PUT("/api/mailbox/:id", func(c echo.Context) (err error) {
|
||||
mailbox, err := models.UpdateMailbox(ctx, cfg, c)
|
||||
return c.JSON(http.StatusOK, mailbox)
|
||||
ret, err := models.UpdateMailbox(ctx, cfg, c)
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
e.DELETE("/api/mailbox/:id", func(c echo.Context) (err error) {
|
||||
mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id"))
|
||||
return c.JSON(http.StatusOK, mailbox)
|
||||
ret, err := models.GetMailbox(ctx, cfg, c.Param("id"))
|
||||
return JSONResult(c, err, ret)
|
||||
})
|
||||
|
||||
// Logs
|
||||
e.GET("/api/log", func(c echo.Context) (err error) {
|
||||
logs, err := models.GetLogs(ctx, cfg)
|
||||
return c.JSON(http.StatusOK, logs)
|
||||
ret, err := models.GetLogs(ctx, cfg)
|
||||
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)
|
||||
}
|
||||
|
@ -26,3 +26,11 @@ func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
|
||||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.Port)))
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user