-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
205 lines (166 loc) · 5.39 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"fmt"
"net/http"
"crypto/tls"
"encoding/gob"
"log"
"net/http/pprof"
_ "net/http/pprof"
"strings"
"time"
"github.com/gorilla/securecookie"
"github.com/julienschmidt/httprouter"
"github.com/raggaer/castro/app"
"github.com/raggaer/castro/app/controllers"
"github.com/raggaer/castro/app/models"
"github.com/raggaer/castro/app/util"
"github.com/ulule/limiter"
"github.com/ulule/limiter/drivers/store/memory"
"github.com/urfave/negroni"
lua "github.com/yuin/gopher-lua"
"golang.org/x/crypto/acme/autocert"
)
func main() {
// Register gob data
gob.Register(&models.CsrfToken{})
gob.Register(&lua.LTable{})
gob.Register(&util.CastroMap{})
gob.Register(map[string]interface{}{})
gob.Register([]interface{}{})
// Show credits and application name
fmt.Printf(`
Castro - High performance content management system for Open Tibia servers
Running version: %v
Compiled at: %v
`, util.VERSION, util.BUILD_DATE)
// Check if application is installed
if !isInstalled() {
// Run the installation process
if err := startInstallerApplication(); err != nil {
log.Fatal(err)
}
return
}
// Run main app entry point
app.Start()
// Create rate-limiter instance
rate := limiter.Rate{
Period: util.Config.Configuration.RateLimit.Time.Duration,
Limit: util.Config.Configuration.RateLimit.Number,
}
// Create rate-limiter storage
store := memory.NewStore()
// Create rate-limiter
limiter := limiter.New(store, rate)
// Declare our new http router
router := httprouter.New()
// Declare application endpoints
router.GET("/", controllers.LuaPage)
router.POST("/", controllers.LuaPage)
router.POST("/subtopic/*filepath", controllers.LuaPage)
router.GET("/subtopic/*filepath", controllers.LuaPage)
router.GET("/extensions/:id/static/*filepath", controllers.ExtensionStatic)
router.POST("/nocsrf/*filepath", controllers.LuaPage)
router.NotFound = http.HandlerFunc(PageNotFound)
// Register pprof router only on development mode
if util.Config.Configuration.IsDev() {
router.GET("/pprof/heap", wrapHandler(pprof.Handler("heap")))
}
// Create the session storage
util.SessionStore = securecookie.New(
[]byte(util.Config.Configuration.Cookies.HashKey),
[]byte(util.Config.Configuration.Cookies.BlockKey),
)
// Create the middleware negroni instance with some application middleware
n := negroni.New(
newRateLimitHandler(limiter),
newSecurityHandler(),
newSessionHandler(),
newMicrotimeHandler(),
newCsrfHandler(),
newI18nHandler(),
)
// Use static handler if enabled
if util.Config.Configuration.Static.Enabled {
n.Use(negroni.NewStatic(http.Dir(util.Config.Configuration.Static.Directory)))
}
// Use negroni logger only in development mode
if util.Config.Configuration.IsDev() || util.Config.Configuration.IsLog() {
n.Use(negroni.NewLogger())
}
// Disable httprouter not found handler
router.HandleMethodNotAllowed = false
// Tell negroni to use our http router
n.UseHandler(router)
// Create castro server
server := http.Server{
Addr: fmt.Sprintf(":%v", util.Config.Configuration.Port),
Handler: n,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
// Check if Castro should run on SSL mode
if util.Config.Configuration.SSL.Enabled {
// Check if user is using auto-certificate
if util.Config.Configuration.SSL.Auto {
// Create auto-certificate manager
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache("tls"),
}
// Set auto-certificate hosts
if strings.HasPrefix(util.Config.Configuration.URL, "www") {
m.HostPolicy = autocert.HostWhitelist(util.Config.Configuration.URL, strings.Replace(util.Config.Configuration.URL, "www.", "", 1))
} else {
m.HostPolicy = autocert.HostWhitelist(util.Config.Configuration.URL, "www."+util.Config.Configuration.URL)
}
// Set server TLS option
server.TLSConfig = &tls.Config{
GetCertificate: m.GetCertificate,
}
// Listen to non-https ACME challenges connections
go http.ListenAndServe(":http", m.HTTPHandler(nil))
// Listen to https connections using autocert
if err := server.ListenAndServeTLS("", ""); err != nil {
util.Logger.Logger.Fatalf("Cannot start Castro autocert HTTPS server: %v", err)
}
}
// Redirect all non https connections
go httpsRedirect()
// If SSL is enabled listen with cert and key
if err := server.ListenAndServeTLS(
util.Config.Configuration.SSL.Cert,
util.Config.Configuration.SSL.Key,
); err != nil {
util.Logger.Logger.Fatalf("Cannot start Castro HTTPS server: %v", err)
}
} else {
// Listen without using ssl
if err := server.ListenAndServe(); err != nil {
util.Logger.Logger.Fatalf("Cannot start Castro HTTP server: %v", err)
}
}
}
// wrapHandler converts a normal http handler to a httprouter handler
func wrapHandler(h http.Handler) httprouter.Handle {
return func(rw http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h.ServeHTTP(rw, req)
}
}
// httpsRedirect gets all non-https traffic and redirects to https
func httpsRedirect() {
// Create router
mux := httprouter.New()
mux.GET("/*filepath", controllers.SSLRedirect)
// Create server
server := http.Server{
Addr: fmt.Sprintf(":%v", 80),
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
util.Logger.Logger.Fatalf("Cannot start HTTP redirect server: %v", err)
}
}