iris/_examples/structuring/handler-based/bootstrap/bootstrapper.go
Gerasimos (Makis) Maropoulos 38c6241055 Add a third, simple, example for folder structuring as requested at https://github.com/kataras/iris/issues/748
One change to code base but it will be described at the next version:

Error Handlers (`app.OnErrorCode/OnAnyErrorCode`)  respect the `app.UseGlobal`'s middlewares now (not the `app.Use` for reasons we can all understand, hopefully).


Former-commit-id: ec97bbb04548f9932cf4d7b950be513b70747bcb
2017-10-01 16:29:25 +03:00

114 lines
2.7 KiB
Go

package bootstrap
import (
"time"
"github.com/gorilla/securecookie"
"github.com/kataras/iris"
"github.com/kataras/iris/sessions"
"github.com/kataras/iris/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, onConnection websocket.ConnectionFunc) {
ws := websocket.New(websocket.Config{})
ws.OnConnection(onConnection)
b.Get(endpoint, ws.Handler())
b.Any("/iris-ws.js", func(ctx iris.Context) {
ctx.Write(websocket.ClientSource)
})
}
// SetupErrorHandlers prepares the http error handlers (>=400).
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")
ctx.View("shared/error.html")
})
}
const (
// StaticAssets is the root directory for public assets like images, css, js.
StaticAssets = "./public/"
// Favicon is the relative 9to the "StaticAssets") favicon path for our app.
Favicon = "favicon.ico"
)
// Bootstrap prepares our application.
//
// Returns itself.
func (b *Bootstrapper) Bootstrap() *Bootstrapper {
b.SetupViews("./views")
b.SetupSessions(24*time.Hour,
[]byte("the-big-and-secret-fash-key-here"),
[]byte("lot-secret-of-characters-big-too"),
)
b.SetupErrorHandlers()
// static files
b.Favicon(StaticAssets + Favicon)
b.StaticWeb(StaticAssets[1:len(StaticAssets)-1], StaticAssets)
return b
}
// Listen starts the http server with the specified "addr".
func (b *Bootstrapper) Listen(addr string, cfgs ...iris.Configurator) {
b.Run(iris.Addr(addr), cfgs...)
}