forked from kataras/iris
/
bootstrapper.go
127 lines (104 loc) 路 3.14 KB
/
bootstrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package bootstrap
import (
"time"
"github.com/gorilla/securecookie"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
"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
// `(context.StatusCodeNotSuccessful`, which defaults to < 200 || >= 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")
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"
)
// Configure accepts configurations and runs them inside the Bootstraper's context.
func (b *Bootstrapper) Configure(cs ...Configurator) {
for _, c := range cs {
c(b)
}
}
// 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)
// middleware, after static files
b.Use(recover.New())
b.Use(logger.New())
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...)
}