This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
/
router.go
116 lines (94 loc) · 2.94 KB
/
router.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
package server
import (
"compress/gzip"
"log"
"net/http"
"github.com/bakape/meguca/imager"
"github.com/bakape/meguca/server/websockets"
"github.com/bakape/meguca/util"
"github.com/dimfeld/httptreemux"
"github.com/gorilla/handlers"
)
var (
// Address is the listening address of the HTTP web server
address = ":8000"
// Defines if HTTPS should be used for listening for incoming connections.
// Requires sslCert and sslKey to be set.
ssl bool
// Path to SSL certificate
sslCert string
// Path to SSL key
sslKey string
// Defines, if all traffic should be piped through a gzip compression
// -decompression handler
enableGzip bool
isTest bool
)
// Used for overriding during tests
var webRoot = "www"
func startWebServer() (err error) {
r := createRouter()
log.Println("listening on " + address)
if ssl {
err = http.ListenAndServeTLS(address, sslCert, sslKey, r)
} else {
err = http.ListenAndServe(address, r)
}
if err != nil {
return util.WrapError("error starting web server", err)
}
return
}
// Create the monolithic router for routing HTTP requests. Separated into own
// function for easier testability.
func createRouter() http.Handler {
r := httptreemux.New()
r.NotFoundHandler = func(w http.ResponseWriter, _ *http.Request) {
text404(w)
}
r.PanicHandler = text500
// HTML
r.GET("/", wrapHandler(redirectToDefault))
r.GET("/:board/", boardHTML)
r.GET("/:board/:thread", threadHTML)
// JSON API
json := r.NewGroup("/json")
json.GET("/:board/", boardJSON)
json.GET("/:board/:thread", threadJSON)
json.GET("/post/:post", servePost)
json.GET("/config", wrapHandler(serveConfigs))
json.GET("/extensions", wrapHandler(serveExtensionMap))
json.GET("/boardConfig/:board", serveBoardConfigs)
json.GET("/boardList", wrapHandler(serveBoardList))
json.GET("/positions/:position/:user", serveStaffPositions)
json.POST("/spoiler", wrapHandler(spoilerImage))
json.GET("/boardTimestamps", wrapHandler(serveBoardTimestamps))
// Administration JSON API for logged in users
admin := r.NewGroup("/admin")
admin.POST("/configureBoard", wrapHandler(configureBoard))
admin.POST("/boardConfig", wrapHandler(servePrivateBoardConfigs))
// Assets
r.GET("/assets/*path", serveAssets)
r.GET("/images/*path", serveImages)
r.GET("/worker.js", wrapHandler(serveWorker))
// Websocket API
r.GET("/socket", wrapHandler(websockets.Handler))
// File upload
r.POST("/upload", wrapHandler(imager.NewImageUpload))
r.POST("/uploadHash", wrapHandler(imager.UploadImageHash))
h := http.Handler(r)
if enableGzip {
h = handlers.CompressHandlerLevel(h, gzip.DefaultCompression)
}
return h
}
// Adapter for http.HandlerFunc -> httptreemux.HandlerFunc
func wrapHandler(fn http.HandlerFunc) httptreemux.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, _ map[string]string) {
fn(w, r)
}
}
// Redirects to / requests to /all/ board
func redirectToDefault(res http.ResponseWriter, req *http.Request) {
http.Redirect(res, req, "/all/", 302)
}