forked from assetto-corsa-web/accweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (128 loc) · 4.67 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
package main
import (
"github.com/assetto-corsa-web/accweb/api"
"github.com/assetto-corsa-web/accweb/cfg"
serverList "github.com/assetto-corsa-web/accweb/server"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
const (
staticDir = "public/static"
staticDirPrefix = "/static/"
buildJsFile = "public/dist/build.js"
buildJsPrefix = "/dist/build.js"
cssFile = "public/dist/main.css"
cssFilePrefix = "/dist/main.css"
indexFile = "public/index.html"
rootDirPrefix = "/"
defaultHttpWriteTimeout = 20
defaultHttpReadTimeout = 20
)
var (
buildJs []byte
watchBuildJs = false
)
func configureLog() {
logrus.Info("Configure logging...")
level := strings.ToLower(cfg.Get().Loglevel)
if level == "debug" {
logrus.SetLevel(logrus.DebugLevel)
} else if level == "info" {
logrus.SetLevel(logrus.InfoLevel)
} else {
logrus.SetLevel(logrus.WarnLevel)
}
}
func createConfigDir() {
if err := os.MkdirAll(cfg.Get().ConfigPath, 0755); err != nil {
logrus.WithError(err).Fatal("Error creating config directory")
}
}
func loadBuildJs() {
logrus.Info("Loading build.js...")
watchBuildJs = cfg.Get().Dev
content, err := ioutil.ReadFile(buildJsFile)
if err != nil {
logrus.WithField("err", err).Fatal("build.js not found")
}
buildJs = content
}
func setupRouter() *mux.Router {
router := mux.NewRouter()
// REST endpoints
router.Handle("/api/token", api.AuthMiddleware(api.TokenHandler, false, false)).Methods("GET")
router.Handle("/api/server", api.AuthMiddleware(api.SaveServerSettingsHandler, true, false)).Methods("POST")
router.Handle("/api/server", api.AuthMiddleware(api.CopyServerSetttingsHandler, true, false)).Methods("PUT")
router.Handle("/api/server", api.AuthMiddleware(api.DeleteServerHandler, true, false)).Methods("DELETE")
router.Handle("/api/server", api.AuthMiddleware(api.GetServerHandler, false, false)).Methods("GET")
router.HandleFunc("/api/status", api.GetServerStatusHandler).Methods("GET")
router.Handle("/api/server/import", api.AuthMiddleware(api.ImportServerHandler, true, false)).Methods("POST")
router.Handle("/api/instance", api.AuthMiddleware(api.StartInstanceHandler, false, true)).Methods("POST")
router.Handle("/api/instance", api.AuthMiddleware(api.StopInstanceHandler, false, true)).Methods("DELETE")
router.Handle("/api/instance", api.AuthMiddleware(api.GetInstanceLogsHandler, false, false)).Methods("GET")
router.HandleFunc("/api/server/export/{filename}", api.ExportServerHandler).Methods("GET")
router.HandleFunc("/api/login", api.LoginHandler).Methods("POST")
// static content
router.PathPrefix(staticDirPrefix).Handler(http.StripPrefix(staticDirPrefix, http.FileServer(http.Dir(staticDir))))
router.PathPrefix(buildJsPrefix).Handler(http.StripPrefix(buildJsPrefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if watchBuildJs {
loadBuildJs()
}
w.Header().Add("Content-Type", "text/javascript")
if _, err := w.Write(buildJs); err != nil {
w.WriteHeader(http.StatusNotFound)
}
})))
router.PathPrefix(cssFilePrefix).Handler(http.StripPrefix(cssFilePrefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/css")
http.ServeFile(w, r, cssFile)
})))
router.PathPrefix(rootDirPrefix).Handler(http.StripPrefix(rootDirPrefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, indexFile)
})))
return router
}
func configureCors(router *mux.Router) http.Handler {
logrus.Info("Configuring CORS...")
origins := strings.Split(cfg.Get().CORS.Origins, ",")
c := cors.New(cors.Options{
AllowedOrigins: origins,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
Debug: strings.ToLower(cfg.Get().CORS.Loglevel) == "debug",
})
return c.Handler(router)
}
func start(handler http.Handler) {
logrus.Info("Starting server...")
server := &http.Server{
Handler: handler,
Addr: cfg.Get().Webserver.Host,
WriteTimeout: time.Duration(cfg.Get().Webserver.WriteTimeout) * time.Second,
ReadTimeout: time.Duration(cfg.Get().Webserver.ReadTimeout) * time.Second,
}
if cfg.Get().Webserver.TLS {
logrus.Info("TLS enabled")
logrus.Fatal(server.ListenAndServeTLS(cfg.Get().Webserver.Cert, cfg.Get().Webserver.PrivateKey))
} else {
logrus.Fatal(server.ListenAndServe())
}
}
func main() {
cfg.Load()
configureLog()
createConfigDir()
api.LoadConfig()
loadBuildJs()
serverList.LoadServerList()
router := setupRouter()
corsConfig := configureCors(router)
start(corsConfig)
}