package bootstrap import ( "time" "github.com/gorilla/securecookie" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/middleware/logger" "github.com/kataras/iris/v12/middleware/recover" "github.com/kataras/iris/v12/sessions" "github.com/kataras/iris/v12/websocket" ) type Configurator func(*Bootstrapper) type Bootstrapper struct { *iris.Application AppName string AppOwner string AppSpawnDate time.Time Sessions *sessions.Sessions } // New returns a new Bootstrapper. func New(appName, appOwner string, cfgs ...Configurator) *Bootstrapper { b := &Bootstrapper{ AppName: appName, AppOwner: appOwner, AppSpawnDate: time.Now(), Application: iris.New(), } for _, cfg := range cfgs { cfg(b) } return b } // SetupViews loads the templates. func (b *Bootstrapper) SetupViews(viewsDir string) { b.RegisterView(iris.HTML(viewsDir, ".html").Layout("shared/layout.html")) } // SetupSessions initializes the sessions, optionally. func (b *Bootstrapper) SetupSessions(expires time.Duration, cookieHashKey, cookieBlockKey []byte) { b.Sessions = sessions.New(sessions.Config{ Cookie: "SECRET_SESS_COOKIE_" + b.AppName, Expires: expires, Encoding: securecookie.New(cookieHashKey, cookieBlockKey), }) } // SetupWebsockets prepares the websocket server. func (b *Bootstrapper) SetupWebsockets(endpoint string, handler websocket.ConnHandler) { ws := websocket.New(websocket.DefaultGorillaUpgrader, handler) b.Get(endpoint, websocket.Handler(ws)) } // SetupErrorHandlers prepares the http error handlers // `(context.StatusCodeNotSuccessful`, which defaults to >=400 (but you can change it). func (b *Bootstrapper) SetupErrorHandlers() { b.OnAnyErrorCode(func(ctx iris.Context) { err := iris.Map{ "app": b.AppName, "status": ctx.GetStatusCode(), "message": ctx.Values().GetString("message"), } if jsonOutput := ctx.URLParamExists("json"); jsonOutput { ctx.JSON(err) return } ctx.ViewData("Err", err) ctx.ViewData("Title", "Error") if err := ctx.View("shared/error.html"); err != nil { ctx.HTML("