This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
/
app.go
102 lines (85 loc) · 2.92 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
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
package actions
import (
"github.com/gobuffalo/buffalo"
popmw "github.com/gobuffalo/buffalo-pop/pop/popmw"
"github.com/gobuffalo/envy"
csrf "github.com/gobuffalo/mw-csrf"
forcessl "github.com/gobuffalo/mw-forcessl"
i18n "github.com/gobuffalo/mw-i18n"
paramlogger "github.com/gobuffalo/mw-paramlogger"
"github.com/gobuffalo/packr/v2"
"github.com/unrolled/secure"
"github.com/gobuffalo/toolkit/models"
"github.com/gobuffalo/toolkit/models/discovery"
"time"
)
// 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: "_toolkit_session",
})
// Automatically redirect to SSL
app.Use(forceSSL())
if ENV == "development" {
app.Use(paramlogger.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:
app.Use(translations())
app.Use(func(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
c.Set("year", time.Now().Year())
c.Set("knownTags", discovery.KnownTags)
return next(c)
}
})
tools := ToolsResource{}
app.GET("/", tools.List)
app.GET("/about", func(c buffalo.Context) error {
return c.Render(200, r.HTML("about.plush.html"))
})
app.Resource("/tools", tools)
app.ServeFiles("/", assetsBox) // serve files from the public directory
if err := addRepoWorkers(app.Context, app.Worker); err != nil {
app.Stop(err)
}
}
return app
}
// translations will load locale files, set up the translator `actions.T`,
// and will return a middleware to use to load the correct locale for each
// request.
// for more information: https://gobuffalo.io/en/docs/localization
func translations() buffalo.MiddlewareFunc {
var err error
if T, err = i18n.New(packr.New("../locales", "../locales"), "en-US"); err != nil {
app.Stop(err)
}
return T.Middleware()
}
// forceSSL will return a middleware that will redirect an incoming request
// if it is not HTTPS. "http://example.com" => "https://example.com".
// This middleware does **not** enable SSL. for your application. To do that
// we recommend using a proxy: https://gobuffalo.io/en/docs/proxy
// for more information: https://github.com/unrolled/secure/
func forceSSL() buffalo.MiddlewareFunc {
return forcessl.Middleware(secure.Options{
SSLRedirect: ENV == "production",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
})
}