-
Notifications
You must be signed in to change notification settings - Fork 207
/
server.go
113 lines (100 loc) · 3.33 KB
/
server.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"golang.org/x/tools/godoc/static"
)
type server struct {
mux *http.ServeMux
db store
log logger
cache responseCache
gotip bool // if set, server is using gotip
examples *examplesHandler
// When the executable was last modified. Used for caching headers of compiled assets.
modtime time.Time
}
func newServer(options ...func(s *server) error) (*server, error) {
s := &server{mux: http.NewServeMux()}
for _, o := range options {
if err := o(s); err != nil {
return nil, err
}
}
if s.db == nil {
return nil, fmt.Errorf("must provide an option func that specifies a datastore")
}
if s.log == nil {
return nil, fmt.Errorf("must provide an option func that specifies a logger")
}
if s.examples == nil {
return nil, fmt.Errorf("must provide an option func that sets the examples handler")
}
s.init()
return s, nil
}
func (s *server) init() {
s.mux.HandleFunc("/", s.handleEdit)
s.mux.HandleFunc("/fmt", s.handleFmt)
s.mux.HandleFunc("/version", s.handleVersion)
s.mux.HandleFunc("/vet", s.commandHandler("vet", vetCheck))
s.mux.HandleFunc("/compile", s.commandHandler("prog", compileAndRun))
s.mux.HandleFunc("/share", s.handleShare)
s.mux.HandleFunc("/playground.js", s.handlePlaygroundJS)
s.mux.HandleFunc("/favicon.ico", handleFavicon)
s.mux.HandleFunc("/_ah/health", s.handleHealthCheck)
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))
s.mux.Handle("/static/", staticHandler)
s.mux.Handle("/doc/play/", http.StripPrefix("/doc/play/", s.examples))
}
func (s *server) handlePlaygroundJS(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/javascript; charset=utf-8")
rd := strings.NewReader(static.Files["playground.js"])
http.ServeContent(w, r, "playground.js", s.modtime, rd)
}
func handleFavicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/favicon.ico")
}
func (s *server) handleHealthCheck(w http.ResponseWriter, r *http.Request) {
if err := s.healthCheck(r.Context()); err != nil {
http.Error(w, "Health check failed: "+err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, "ok")
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Forwarded-Proto") == "http" {
r.URL.Scheme = "https"
r.URL.Host = r.Host
http.Redirect(w, r, r.URL.String(), http.StatusFound)
return
}
if r.Header.Get("X-Forwarded-Proto") == "https" {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; preload")
}
s.mux.ServeHTTP(w, r)
}
// writeJSONResponse JSON-encodes resp and writes to w with the given HTTP
// status.
func (s *server) writeJSONResponse(w http.ResponseWriter, resp interface{}, status int) {
w.Header().Set("Content-Type", "application/json")
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(resp); err != nil {
s.log.Errorf("error encoding response: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(status)
if _, err := io.Copy(w, &buf); err != nil {
s.log.Errorf("io.Copy(w, &buf): %v", err)
return
}
}