forked from mmcgrana/gobyexample
-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
208 lines (182 loc) · 4.63 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"encoding/base64"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func config(k string) string {
v := os.Getenv(k)
if v == "" {
panic("missing " + k)
}
return v
}
func runLogging(logs chan string) {
for log := range logs {
fmt.Println(log)
}
}
func wrapLogging(f http.HandlerFunc, logs chan string) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
start := time.Now()
f(res, req)
method := req.Method
path := req.URL.Path
elapsed := float64(time.Since(start)) / 1000000.0
logs <- fmt.Sprintf("request at=finish method=%s path=%s elapsed=%f", method, path, elapsed)
}
}
func wrapCanonicalHost(f http.HandlerFunc, canonicalHost string, forceHttps bool) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
scheme := "http"
if h, ok := req.Header["X-Forwarded-Proto"]; ok {
if h[0] == "https" {
scheme = "https"
}
}
hostPort := strings.Split(req.Host, ":")
host := hostPort[0]
if (forceHttps && (scheme != "https")) || host != canonicalHost {
if forceHttps {
scheme = "https"
}
hostPort[0] = canonicalHost
url := scheme + "://" + strings.Join(hostPort, ":") + req.URL.String()
http.Redirect(res, req, url, 301)
return
}
f(res, req)
}
}
type Authenticator func(string, string) bool
func testAuth(r *http.Request, auth Authenticator) bool {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
return false
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return false
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return false
}
return auth(pair[0], pair[1])
}
func requireAuth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="private"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
}
func wrapAuth(h http.HandlerFunc, a Authenticator) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if testAuth(r, a) {
h(w, r)
} else {
requireAuth(w, r)
}
}
}
var reqCount int64 = 0
func wrapReqCount(h http.HandlerFunc, reqCountPtr *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(reqCountPtr, 1)
h(w, r)
atomic.AddInt64(reqCountPtr, -1)
}
}
func static(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, "public"+req.URL.Path)
}
func notFound(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, "public/404.html")
}
func checkAuth(user, pass string) bool {
auth := os.Getenv("AUTH")
if auth == "" {
return true
}
return auth == strings.Join([]string{user, pass}, ":")
}
func routerHandlerFunc(router *mux.Router) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
router.ServeHTTP(res, req)
}
}
func router() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/", static).Methods("GET")
router.HandleFunc("/favicon.ico", static).Methods("GET")
router.HandleFunc("/play.png", static).Methods("GET")
router.HandleFunc("/site.css", static).Methods("GET")
entries, err := ioutil.ReadDir("public")
check(err)
for _, f := range entries {
if !strings.Contains(f.Name(), ".") {
router.HandleFunc("/" + f.Name(), static).Methods("GET")
}
}
router.NotFoundHandler = http.HandlerFunc(notFound)
return router
}
func main() {
logs := make(chan string, 10000)
go runLogging(logs)
handler := routerHandlerFunc(router())
if os.Getenv("AUTH") != "" {
handler = wrapAuth(handler, checkAuth)
}
handler = wrapCanonicalHost(handler, config("CANONICAL_HOST"), config("FORCE_HTTPS") == "1")
handler = wrapLogging(handler, logs)
handler = wrapReqCount(handler, &reqCount)
server := &http.Server{Handler: handler}
listener, listenErr := net.Listen("tcp", ":"+config("PORT"))
if listenErr != nil {
panic(listenErr)
}
stop := make(chan bool, 1)
sig := make(chan os.Signal, 1)
go func() {
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
logs <- "trap at=start"
<-sig
for {
reqCountCurrent := atomic.LoadInt64(&reqCount)
if reqCountCurrent > 0 {
logs <- fmt.Sprintf("trap at=draining remaining=%d", reqCountCurrent)
time.Sleep(time.Second)
} else {
logs <- fmt.Sprintf("trap at=finish")
stop <- true
return
}
}
}()
go func() {
logs <- "serve at=start"
server.Serve(listener)
logs <- "serve at=finish"
}()
<-stop
logs <- "close at=start"
closeErr := listener.Close()
if closeErr != nil {
panic(closeErr)
}
logs <- "close at=finish"
}