-
Notifications
You must be signed in to change notification settings - Fork 165
/
app.go
64 lines (54 loc) · 1.74 KB
/
app.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
package actions
import (
"github.com/arschles/go-in-5-minutes/episode24/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo-pop/pop/popmw"
"github.com/gobuffalo/envy"
csrf "github.com/gobuffalo/mw-csrf"
ssl "github.com/gobuffalo/mw-forcessl"
i18n "github.com/gobuffalo/mw-i18n"
plggr "github.com/gobuffalo/mw-paramlogger"
"github.com/gobuffalo/packr"
"github.com/unrolled/secure"
)
// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = envy.Get("GO_ENV", "development")
var app *buffalo.App
var T *i18n.Translator
// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionName: "_episode24_session",
})
// Automatically redirect to SSL
app.Use(ssl.Middleware(secure.Options{
SSLRedirect: ENV == "production",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}))
if ENV == "development" {
app.Use(plggr.ParameterLogger)
}
// Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
// Remove to disable this.
app.Use(csrf.New)
// Wraps each request in a transaction.
// c.Value("tx").(*pop.PopTransaction)
// Remove to disable this.
app.Use(popmw.Transaction(models.DB))
// Setup and use translations:
var err error
if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
app.Stop(err)
}
app.Use(T.Middleware())
app.GET("/", HomeHandler)
app.Resource("/gifms", GifmsResource{})
app.ServeFiles("/", assetsBox) // serve files from the public directory
}
return app
}