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
|
// Entrypoint for the web application
package app
import (
"archives/pkg/app/cache"
"archives/pkg/app/home"
"archives/pkg/app/list"
"archives/pkg/app/message"
"archives/pkg/app/popular"
"archives/pkg/app/recent"
"archives/pkg/app/search"
"archives/pkg/config"
"fmt"
"log"
"net/http"
)
// Serve is used to serve the web application
func Serve() {
// init caches
cache.Init()
cache.Update()
fmt.Println("Serving on Port " + config.Port())
for _, mailingList := range config.AllPublicMailingLists() {
setRoute("/"+mailingList+"/message/", message.Show)
setRoute("/"+mailingList+"/messages/", list.Messages)
setRoute("/"+mailingList+"/threads/", list.Threads)
setRoute("/"+mailingList+"/", list.Show)
}
setRoute("/lists", list.Browse)
setRoute("/popular", popular.Threads)
setRoute("/search", search.Search)
setRoute("/recent", recent.Show)
setRoute("/", home.Show)
fs := http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))
http.Handle("/assets/", fs)
setRoute("/cache/update", cache.UpdateHandler)
log.Fatal(http.ListenAndServe(":"+config.Port(), nil))
}
// define a route using the default middleware and the given handler
func setRoute(path string, handler http.HandlerFunc) {
http.HandleFunc(path, mw(handler))
}
// mw is used as default middleware to set the default headers
func mw(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
setDefaultHeaders(w)
handler(w, r)
}
}
// setDefaultHeaders sets the default headers that apply for all pages
func setDefaultHeaders(w http.ResponseWriter) {
w.Header().Set("Cache-Control", config.CacheControl())
}
|