forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
135 lines (115 loc) · 3.33 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
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
128
129
130
131
132
133
134
135
package actions
import (
"fmt"
"log"
"github.com/garyburd/redigo/redis"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/middleware"
"github.com/gobuffalo/buffalo/middleware/csrf"
"github.com/gobuffalo/buffalo/middleware/i18n"
"github.com/gobuffalo/buffalo/middleware/ssl"
"github.com/gobuffalo/buffalo/worker"
"github.com/gobuffalo/gocraft-work-adapter"
"github.com/gobuffalo/packr"
"github.com/gomods/athens/pkg/config/env"
"github.com/gomods/athens/pkg/user"
"github.com/rs/cors"
"github.com/unrolled/secure"
)
const (
// FetcherWorkerName is the name of the worker fetching sources from experienced cache misses
FetcherWorkerName = "olympusfetcher"
// ReporterWorkerName is the name of the worker reporting cache misses
ReporterWorkerName = "olympusreporter"
workerQueue = "default"
workerModuleKey = "module"
workerVersionKey = "version"
workerTryCountKey = "trycount"
maxTryCount = 5
)
// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = env.GoEnvironmentWithDefault("development")
var app *buffalo.App
// T is the translator to use
var T *i18n.Translator
var gopath string
var userStore *user.Store
func init() {
g, err := env.GoPath()
if err != nil {
log.Fatal(err)
}
gopath = g
}
// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() (*buffalo.App, error) {
if app == nil {
redisPort := env.RedisQueuePortWithDefault(":6379")
app = buffalo.New(buffalo.Options{
Env: ENV,
PreWares: []buffalo.PreWare{
cors.Default().Handler,
},
SessionName: "_athens_session",
Worker: getWorker(redisPort),
})
// Automatically redirect to SSL
app.Use(ssl.ForceSSL(secure.Options{
SSLRedirect: ENV == "production",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}))
if ENV == "development" {
app.Use(middleware.ParameterLogger)
}
initializeTracing(app)
// Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
// Remove to disable this.
csrfMiddleware := csrf.New
app.Use(csrfMiddleware)
// Wraps each request in a transaction.
// c.Value("tx").(*pop.PopTransaction)
// Remove to disable this.
// app.Use(middleware.PopTransaction(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)
store, err := GetStorage()
if err != nil {
err = fmt.Errorf("error getting storage configuration (%s)", err)
return nil, err
}
if err := store.Connect(); err != nil {
err = fmt.Errorf("error connecting to storage (%s)", err)
return nil, err
}
if err := addProxyRoutes(app, store); err != nil {
err = fmt.Errorf("error adding proxy routes (%s)", err)
return nil, err
}
// serve files from the public directory:
// has to be last
app.ServeFiles("/", assetsBox)
}
return app, nil
}
func getWorker(port string) worker.Worker {
return gwa.New(gwa.Options{
Pool: &redis.Pool{
MaxActive: 5,
MaxIdle: 5,
Wait: true,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", port)
},
},
Name: FetcherWorkerName,
MaxConcurrency: 25,
})
}