swirl/main.go

111 lines
3.2 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package main
import (
2017-10-12 03:04:27 +00:00
"fmt"
2017-09-29 12:18:29 +00:00
"net/http"
2017-09-26 12:50:09 +00:00
"path/filepath"
"runtime"
"time"
"github.com/cuigh/auxo/app"
2017-10-27 12:31:30 +00:00
"github.com/cuigh/auxo/app/flag"
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"
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"
)
func main() {
2017-10-27 12:31:30 +00:00
misc.BindOptions()
app.Name = "Swirl"
app.Version = "0.6.3"
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) {
misc.LoadOptions()
app.Run(server())
}
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() *web.Server {
2017-10-12 03:04:27 +00:00
setting, err := biz.Setting.Get()
if err != nil {
panic(fmt.Sprintf("Load setting failed: %v", err))
}
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"}
ws.Renderer = jet.New().SetDebug(config.GetBool("debug")).
2017-10-12 03:04:27 +00:00
AddFunc("time", misc.FormatTime(setting.TimeZone.Offset)).
AddFunc("i18n", misc.Message(setting.Language)).
2017-09-26 12:50:09 +00:00
AddFuncs(misc.Funcs).
2017-10-13 11:27:48 +00:00
AddVariable("language", setting.Language).
2017-10-27 12:31:30 +00:00
AddVariable("version", app.Version).
2017-09-26 12:50:09 +00:00
AddVariable("go_version", runtime.Version())
// 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
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: biz.User.Identify,
Timeout: time.Minute * 30,
SlidingExpiration: true,
}
g := ws.Group("", form, filter.NewAuthorizer(biz.User.Authorize))
2017-09-26 12:50:09 +00:00
// register auth handlers
g.Post("/login", form.LoginJSON(biz.User.Login)).SetAuthorize(web.AuthAnonymous)
g.Get("/logout", form.Logout).SetAuthorize(web.AuthAuthenticated)
// 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())
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())
2017-10-27 12:31:30 +00:00
return ws
2017-09-26 12:50:09 +00:00
}