forked from google/periph
/
web_handlers.go
68 lines (59 loc) · 1.96 KB
/
web_handlers.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
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
//go:generate go get github.com/tdewolff/minify/cmd/minify
//go:generate go run internal/static_gen.go -o static_prod.go
package main
import "net/http"
func (s *webServer) addOtherHandlers() {
http.HandleFunc("/raw/periph/v1/xsrf_token", noContent(s.apiXSRFTokenHandler))
http.HandleFunc("/favicon.ico", getOnly(s.getFavicon))
// Do not use getOnly here as it is the 'catch all, one and we want to check
// that before the method.
http.HandleFunc("/", noContent(s.getRoot))
}
// Static handlers
// /
func (s *webServer) getRoot(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
if r.Method != "GET" && r.Method != "HEAD" {
http.Error(w, "Only GET is allowed", http.StatusMethodNotAllowed)
return
}
s.setXSRFCookie(r.RemoteAddr, w)
content := getContent("static/index.html")
if content == nil {
http.Error(w, "Content missing", 500)
return
}
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Cache-Control", cacheControlContent)
_, _ = w.Write(content)
}
// /favicon.ico
func (s *webServer) getFavicon(w http.ResponseWriter, r *http.Request) {
content := getContent("static/favicon.ico")
if content == nil {
http.Error(w, "Content missing", 500)
return
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Cache-Control", cacheControlContent)
_, _ = w.Write(content)
}
// Other handlers
// /raw/periph/v1/xsrf_token
func (s *webServer) apiXSRFTokenHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Only POST is allowed", http.StatusMethodNotAllowed)
return
}
t := s.setXSRFCookie(r.RemoteAddr, w)
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Cache-Control", cacheControlNone)
w.WriteHeader(200)
_, _ = w.Write([]byte(t))
}