swirl/main.go

127 lines
3.9 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package main
import (
2017-09-29 12:18:29 +00:00
"net/http"
"os"
2017-09-26 12:50:09 +00:00
"path/filepath"
"runtime"
"github.com/cuigh/auxo/app"
2017-10-27 12:31:30 +00:00
"github.com/cuigh/auxo/app/flag"
2017-12-05 04:19:32 +00:00
_ "github.com/cuigh/auxo/cache/memory"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/config"
2017-11-17 04:31:19 +00:00
"github.com/cuigh/auxo/data/valid"
"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"
"github.com/cuigh/auxo/net/web/filter/auth"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/net/web/renderer/jet"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/controller"
"github.com/cuigh/swirl/misc"
"github.com/cuigh/swirl/model"
"github.com/cuigh/swirl/scaler"
"github.com/cuigh/swirl/security"
2017-09-26 12:50:09 +00:00
)
func main() {
2017-10-27 12:31:30 +00:00
misc.BindOptions()
app.Name = "Swirl"
2018-07-04 06:50:19 +00:00
app.Version = "0.8.2"
2017-10-27 12:31:30 +00:00
app.Desc = "A web management UI for Docker, focused on swarm cluster"
app.Action = func(ctx *app.Context) {
2018-03-20 05:07:19 +00:00
err := config.UnmarshalOption("swirl", &misc.Options)
if err != nil {
2018-04-27 13:17:00 +00:00
log.Get(app.Name).Error("Failed to load options: ", err)
2018-03-20 05:07:19 +00:00
os.Exit(1)
}
setting, err := biz.Setting.Get()
if err != nil {
2018-04-27 13:17:00 +00:00
log.Get(app.Name).Error("Failed to load settings: ", err)
os.Exit(1)
}
2018-04-16 09:21:20 +00:00
biz.Stack.Migrate()
2018-04-27 13:17:00 +00:00
if setting.Metrics.Prometheus != "" {
scaler.Start()
}
app.Run(server(setting))
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()
}
func server(setting *model.Setting) *web.Server {
ws := web.Auto()
2017-09-29 12:18:29 +00:00
// customize error handler
ws.ErrorHandler.OnCode(http.StatusNotFound, func(ctx web.Context, err error) {
2017-11-20 03:32:51 +00:00
if ctx.IsAJAX() {
ctx.Status(http.StatusNotFound).HTML(http.StatusText(http.StatusNotFound)) // nolint: gas
2017-11-20 03:32:51 +00:00
} else {
ctx.Status(http.StatusNotFound).Render("404", nil) // nolint: gas
2017-11-20 03:32:51 +00:00
}
2017-09-29 12:18:29 +00:00
})
ws.ErrorHandler.OnCode(http.StatusForbidden, func(ctx web.Context, err error) {
2017-11-20 03:32:51 +00:00
if ctx.IsAJAX() {
ctx.Status(http.StatusForbidden).HTML("You do not have permission to perform this operation") // nolint: gas
2017-11-20 03:32:51 +00:00
} else {
ctx.Status(http.StatusForbidden).Render("403", nil) // nolint: gas
2017-11-20 03:32:51 +00:00
}
2017-09-29 12:18:29 +00:00
})
2017-11-08 10:36:13 +00:00
// set render
2017-11-17 04:31:19 +00:00
ws.Validator = &valid.Validator{Tag: "valid"}
2018-01-04 07:54:28 +00:00
ws.Renderer = jet.Must(jet.Debug(config.GetBool("debug")), jet.VarMap(misc.Funcs), jet.VarMap(map[string]interface{}{
"language": setting.Language,
"version": app.Version,
"go_version": runtime.Version(),
"time": misc.FormatTime(setting.TimeZone.Offset),
"i18n": misc.Message(setting.Language),
}))
2017-09-26 12:50:09 +00:00
// register global filters
2017-11-08 10:36:13 +00:00
ws.Use(filter.NewRecover())
2017-09-26 12:50:09 +00:00
// register static handlers
2018-04-12 07:09:27 +00:00
ws.File("/favicon.ico", filepath.Join(filepath.Dir(app.Path()), "assets/swirl/img/favicon.ico"))
ws.Static("/assets", filepath.Join(filepath.Dir(app.Path()), "assets"))
2017-09-26 12:50:09 +00:00
// create biz group
2017-11-08 10:36:13 +00:00
form := &auth.Form{
Identifier: security.Identifier,
2018-03-20 05:07:19 +00:00
Timeout: misc.Options.AuthTimeout,
2017-11-08 10:36:13 +00:00
SlidingExpiration: true,
}
g := ws.Group("", form, filter.NewAuthorizer(security.Checker))
2017-09-26 12:50:09 +00:00
// register auth handlers
g.Post("/login", form.LoginJSON(security.Validator(setting)), web.WithName("login"), web.WithAuthorize(web.AuthAnonymous))
2017-12-20 08:31:05 +00:00
g.Get("/logout", form.Logout, web.WithName("logout"), web.WithAuthorize(web.AuthAuthenticated))
2017-09-26 12:50:09 +00:00
// register controllers
g.Handle("", controller.Home())
g.Handle("/profile", controller.Profile())
g.Handle("/registry", controller.Registry())
g.Handle("/node", controller.Node())
g.Handle("/service", controller.Service(), web.FilterFunc(security.Permiter))
2017-09-26 12:50:09 +00:00
g.Handle("/service/template", controller.Template())
g.Handle("/stack", controller.Stack())
g.Handle("/network", controller.Network())
g.Handle("/secret", controller.Secret())
g.Handle("/config", controller.Config())
g.Handle("/task", controller.Task())
g.Handle("/container", controller.Container())
g.Handle("/image", controller.Image())
g.Handle("/volume", controller.Volume())
g.Handle("/system/user", controller.User())
g.Handle("/system/role", controller.Role())
g.Handle("/system/setting", controller.Setting())
g.Handle("/system/event", controller.Event())
2018-03-22 08:13:54 +00:00
g.Handle("/system/chart", controller.Chart())
2017-09-26 12:50:09 +00:00
2017-10-27 12:31:30 +00:00
return ws
2017-09-26 12:50:09 +00:00
}