2021-12-06 12:24:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/cuigh/auxo/app"
|
|
|
|
"github.com/cuigh/auxo/data"
|
2021-12-16 12:23:08 +00:00
|
|
|
"github.com/cuigh/auxo/errors"
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/biz"
|
2021-12-23 11:28:31 +00:00
|
|
|
"github.com/cuigh/swirl/dao"
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/swirl/docker"
|
2021-12-16 12:23:08 +00:00
|
|
|
"github.com/cuigh/swirl/misc"
|
2021-12-06 12:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SystemHandler encapsulates system related handlers.
|
|
|
|
type SystemHandler struct {
|
|
|
|
CheckState web.HandlerFunc `path:"/check-state" auth:"*" desc:"check system state"`
|
|
|
|
CreateAdmin web.HandlerFunc `path:"/create-admin" method:"post" auth:"*" desc:"initialize administrator account"`
|
|
|
|
Version web.HandlerFunc `path:"/version" auth:"*" desc:"fetch app version"`
|
|
|
|
Summarize web.HandlerFunc `path:"/summarize" auth:"?" desc:"fetch statistics data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSystem creates an instance of SystemHandler
|
2021-12-16 12:23:08 +00:00
|
|
|
func NewSystem(d *docker.Docker, b biz.SystemBiz, ub biz.UserBiz) *SystemHandler {
|
2021-12-06 12:24:22 +00:00
|
|
|
return &SystemHandler{
|
|
|
|
CheckState: systemCheckState(b),
|
2021-12-16 12:23:08 +00:00
|
|
|
CreateAdmin: systemCreateAdmin(ub),
|
2021-12-06 12:24:22 +00:00
|
|
|
Version: systemVersion,
|
|
|
|
Summarize: systemSummarize(d),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func systemCheckState(b biz.SystemBiz) web.HandlerFunc {
|
|
|
|
return func(c web.Context) (err error) {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
state, err := b.CheckState(ctx)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return success(c, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func systemVersion(c web.Context) (err error) {
|
|
|
|
return success(c, data.Map{
|
|
|
|
"version": app.Version,
|
|
|
|
"goVersion": runtime.Version(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func systemSummarize(d *docker.Docker) web.HandlerFunc {
|
|
|
|
return func(c web.Context) (err error) {
|
|
|
|
summary := struct {
|
|
|
|
NodeCount int `json:"nodeCount"`
|
|
|
|
NetworkCount int `json:"networkCount"`
|
|
|
|
ServiceCount int `json:"serviceCount"`
|
|
|
|
StackCount int `json:"stackCount"`
|
|
|
|
}{}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if summary.NodeCount, err = d.NodeCount(ctx); err != nil {
|
2021-12-06 12:24:22 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
if summary.NetworkCount, err = d.NetworkCount(ctx); err != nil {
|
2021-12-06 12:24:22 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
if summary.ServiceCount, err = d.ServiceCount(ctx); err != nil {
|
2021-12-06 12:24:22 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
if summary.StackCount, err = d.StackCount(ctx); err != nil {
|
2021-12-06 12:24:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return success(c, summary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 12:23:08 +00:00
|
|
|
func systemCreateAdmin(ub biz.UserBiz) web.HandlerFunc {
|
2021-12-06 12:24:22 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2022-02-10 03:09:03 +00:00
|
|
|
args := &struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
*dao.User
|
|
|
|
}{}
|
|
|
|
if err = c.Bind(args, true); err != nil {
|
2021-12-16 12:23:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-12-16 12:23:08 +00:00
|
|
|
var count int
|
2022-01-06 08:54:14 +00:00
|
|
|
if count, err = ub.Count(ctx); err == nil && count > 0 {
|
2021-12-16 12:23:08 +00:00
|
|
|
return errors.Coded(misc.ErrSystemInitialized, "system was already initialized")
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2021-12-16 12:23:08 +00:00
|
|
|
|
2022-02-10 03:09:03 +00:00
|
|
|
user := args.User
|
|
|
|
user.Password = args.Password
|
2021-12-16 12:23:08 +00:00
|
|
|
user.Admin = true
|
|
|
|
user.Type = biz.UserTypeInternal
|
2022-01-06 08:54:14 +00:00
|
|
|
_, err = ub.Create(ctx, user, nil)
|
2021-12-06 12:24:22 +00:00
|
|
|
return ajax(c, err)
|
|
|
|
}
|
|
|
|
}
|