-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
74 lines (65 loc) · 2.1 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
)
// We store all templates on first launch for efficiency.
var templates map[string]*template.Template
// initializeTemplates populates `templates` for use in our handlers. The logic is that each
// of our templates is composed by base.html and some other HTML template.
func initializeTemplates() (err error) {
if templates == nil {
templates = make(map[string]*template.Template)
}
templateFiles, err := filepath.Glob("tmpl/*.html")
if err != nil {
return
}
for _, t := range templateFiles {
if t != "tmpl/base.html" {
templates[t] = template.Must(template.ParseFiles("tmpl/base.html", t))
}
}
return
}
// renderContent parses the content (given as a template) and puts it into our base template.
// The control of the input data is handled by the handlers in handlers.go
func renderContent(t string, r *http.Request, w http.ResponseWriter, data interface{}) {
// Set up various security headers
w.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self' https://steamcdn-a.akamaihd.net")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
w.Header().Set("X-Xss-Protection", "1; mode=block")
w.Header().Set("X-Content-Type-Options", "nosniff")
// Combine templates and write response
err := templates[t].ExecuteTemplate(w, "base", data)
if err != nil {
log.Println(err)
}
}
// initialize sets up all global variables relevant for the application
func initialize() {
initializeTemplates()
readConfig()
readAllCategories()
readAllAchievements()
}
func main() {
initialize()
staticHandler := http.FileServer(http.Dir("tmpl"))
http.Handle("/css/", staticHandler)
http.Handle("/img/", staticHandler)
http.Handle("/js/", staticHandler)
router := mux.NewRouter()
router.HandleFunc("/", landingHandler)
router.HandleFunc("/{username:[a-z0-9_-]+}", achievementHandler)
http.Handle("/", router)
err := http.ListenAndServe(fmt.Sprintf(":%d", config.WebserverPort), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}