swirl/main.go

134 lines
2.7 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package main
import (
2021-02-26 05:19:40 +00:00
"embed"
2021-12-23 10:00:14 +00:00
"encoding/json"
2021-12-06 12:24:22 +00:00
"io/fs"
2017-09-29 12:18:29 +00:00
"net/http"
2021-12-06 12:24:22 +00:00
"strings"
2022-01-06 08:54:14 +00:00
"time"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/app"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/auxo/app/container"
2017-10-27 12:31:30 +00:00
"github.com/cuigh/auxo/app/flag"
2021-12-23 10:00:14 +00:00
"github.com/cuigh/auxo/data"
2017-11-17 04:31:19 +00:00
"github.com/cuigh/auxo/data/valid"
"github.com/cuigh/auxo/errors"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/auxo/log"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/net/web"
2017-11-08 10:36:13 +00:00
"github.com/cuigh/auxo/net/web/filter"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/auxo/util/run"
_ "github.com/cuigh/swirl/api"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/biz"
2021-12-23 11:28:31 +00:00
_ "github.com/cuigh/swirl/dao/bolt"
_ "github.com/cuigh/swirl/dao/mongo"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/misc"
"github.com/cuigh/swirl/scaler"
2017-09-26 12:50:09 +00:00
)
2021-12-06 12:24:22 +00:00
var (
//go:embed ui/dist
webFS embed.FS
)
2021-02-26 05:19:40 +00:00
2017-09-26 12:50:09 +00:00
func main() {
2017-10-27 12:31:30 +00:00
app.Name = "Swirl"
2022-02-10 03:09:03 +00:00
app.Version = "1.0.0rc2"
2017-10-27 12:31:30 +00:00
app.Desc = "A web management UI for Docker, focused on swarm cluster"
2018-09-11 08:36:54 +00:00
app.Action = func(ctx *app.Context) error {
2021-12-06 12:24:22 +00:00
return run.Pipeline(misc.LoadOptions, initSystem, scaler.Start, startServer)
2017-10-27 12:31:30 +00:00
}
2017-11-09 09:16:18 +00:00
app.Flags.Register(flag.All)
2017-10-27 12:31:30 +00:00
app.Start()
}
2021-12-06 12:24:22 +00:00
func startServer() (err error) {
s := web.Auto()
s.Validator = &valid.Validator{}
s.ErrorHandler.Default = handleError
s.Use(filter.NewRecover())
s.Static("/", http.FS(loadWebFS()), "index.html")
const prefix = "api."
g := s.Group("/api", findFilters("identifier", "authorizer")...)
container.Range(func(name string, service interface{}) bool {
if strings.HasPrefix(name, prefix) {
g.Handle("/"+name[len(prefix):], service)
2017-11-20 03:32:51 +00:00
}
2021-12-06 12:24:22 +00:00
return true
2017-09-29 12:18:29 +00:00
})
2021-12-06 12:24:22 +00:00
app.Run(s)
return
}
func loadWebFS() fs.FS {
sub, err := fs.Sub(webFS, "ui/dist")
if err != nil {
panic(err)
}
return sub
}
func handleError(ctx web.Context, err error) {
var (
status = http.StatusInternalServerError
code int32 = 1
)
if e, ok := err.(*web.Error); ok {
status = e.Status()
}
if e, ok := err.(*errors.CodedError); ok {
code = e.Code
}
err = ctx.Status(status).Result(code, err.Error(), nil)
if err != nil {
ctx.Logger().Error(err)
}
}
func findFilters(names ...string) []web.Filter {
var filters []web.Filter
for _, name := range names {
filters = append(filters, container.Find(name).(web.Filter))
}
return filters
}
func initSystem() error {
return container.Call(func(b biz.SystemBiz) error {
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(time.Minute)
defer cancel()
return b.Init(ctx)
2017-09-29 12:18:29 +00:00
})
2021-12-06 12:24:22 +00:00
}
func loadSetting(sb biz.SettingBiz) *misc.Setting {
var (
2021-12-23 10:00:14 +00:00
err error
opts data.Map
b []byte
s = &misc.Setting{}
2021-12-06 12:24:22 +00:00
)
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(30 * time.Second)
defer cancel()
if opts, err = sb.Load(ctx); err == nil {
2021-12-23 10:00:14 +00:00
if b, err = json.Marshal(opts); err == nil {
err = json.Unmarshal(b, s)
}
2017-11-08 10:36:13 +00:00
}
2021-12-06 12:24:22 +00:00
if err != nil {
log.Get("misc").Error("failed to load setting: ", err)
}
return s
}
func init() {
container.Put(loadSetting)
2017-09-26 12:50:09 +00:00
}