-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (119 loc) · 3.97 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
package main
import (
"fmt"
"github.com/caddyserver/certmagic"
"github.com/emvi/logbuch"
"github.com/gorilla/mux"
"github.com/rs/cors"
"io/ioutil"
"net/http"
"os"
"strings"
)
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 = "/"
logTimeFormat = "2006-01-02_15:04:05"
envPrefix = "STS_WIKI_"
)
var (
buildJs []byte
watchBuildJs = false
)
func configureLog() {
logbuch.SetFormatter(logbuch.NewFieldFormatter(logTimeFormat, "\t\t"))
logbuch.Info("Configure logging...")
level := strings.ToLower(os.Getenv("STS_WIKI_LOGLEVEL"))
if level == "debug" {
logbuch.SetLevel(logbuch.LevelDebug)
} else if level == "info" {
logbuch.SetLevel(logbuch.LevelInfo)
} else {
logbuch.SetLevel(logbuch.LevelWarning)
}
}
func logEnvConfig() {
for _, e := range os.Environ() {
if strings.HasPrefix(e, envPrefix) {
pair := strings.Split(e, "=")
logbuch.Info(pair[0] + "=" + pair[1])
}
}
}
func loadBuildJs() {
logbuch.Info("Loading build.js...")
watchBuildJs = os.Getenv("STS_WIKI_WATCH_BUILD_JS") != ""
content, err := ioutil.ReadFile(buildJsFile)
if err != nil {
logbuch.Fatal("build.js not found", logbuch.Fields{"err": err})
}
buildJs = make([]byte, 0)
buildJs = append(buildJs, []byte(fmt.Sprintf("var STS_WIKI_CLIENT_ID='%s';", os.Getenv("STS_WIKI_CLIENT_ID")))...)
buildJs = append(buildJs, []byte(fmt.Sprintf("var STS_WIKI_CLIENT_SECRET='%s';", os.Getenv("STS_WIKI_CLIENT_SECRET")))...)
buildJs = append(buildJs, []byte(fmt.Sprintf("var STS_WIKI_ORGANIZATION='%s';", os.Getenv("STS_WIKI_ORGANIZATION")))...)
buildJs = append(buildJs, content...)
}
func setupRouter() *mux.Router {
router := mux.NewRouter()
// 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 {
logbuch.Info("Configuring CORS...")
origins := strings.Split(os.Getenv("STS_WIKI_ALLOWED_ORIGINS"), ",")
c := cors.New(cors.Options{
AllowedOrigins: origins,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
Debug: strings.ToLower(os.Getenv("STS_WIKI_CORS_LOGLEVEL")) == "debug",
})
return c.Handler(router)
}
func start(handler http.Handler) {
logbuch.Info("Starting server...")
if strings.ToLower(os.Getenv("STS_WIKI_TLS")) == "true" {
logbuch.Info("TLS enabled")
certmagic.DefaultACME.Agreed = true
certmagic.DefaultACME.Email = os.Getenv("STS_WIKI_TLS_EMAIL")
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA
if err := certmagic.HTTPS(strings.Split(os.Getenv("STS_WIKI_DOMAIN"), ","), handler); err != nil {
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
}
} else {
if err := http.ListenAndServe(os.Getenv("STS_WIKI_HOST"), handler); err != nil {
logbuch.Fatal("Error starting server", logbuch.Fields{"err": err})
}
}
}
func main() {
configureLog()
logEnvConfig()
loadBuildJs()
router := setupRouter()
corsConfig := configureCors(router)
start(corsConfig)
}