This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
/
handlers.go
60 lines (53 loc) · 1.68 KB
/
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
package server
import (
"fmt"
"net/http"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/dghubble/sessions"
"golang.org/x/net/context"
"github.com/coreos/tectonic-installer/installer/server/ctxh"
)
// logRequests logs HTTP requests and calls the next handler.
func logRequests(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
log.Debugf("HTTP %s %v", req.Method, req.URL)
if next != nil {
next.ServeHTTP(w, req)
}
}
return http.HandlerFunc(fn)
}
// requireHTTPMethod will respond with HTTP 405, method not allowed if
// the http request is not the HTTP method provided.
func requireHTTPMethod(method string, next ctxh.ContextHandler) ctxh.ContextHandler {
fn := func(ctx context.Context, w http.ResponseWriter, req *http.Request) *ctxh.AppError {
if req.Method != method {
return ctxh.NewAppError(nil, fmt.Sprintf("request must be %s", method), http.StatusMethodNotAllowed)
}
next.ServeHTTP(ctx, w, req)
return nil
}
return ctxh.ContextHandlerFuncWithError(fn)
}
// syncHandler synchronizes request handling and calls the next handler. Only
// one request may be processed by the handler chain at a time.
func syncHandler(next http.Handler) http.Handler {
mu := &sync.Mutex{}
fn := func(w http.ResponseWriter, req *http.Request) {
mu.Lock()
if next != nil {
next.ServeHTTP(w, req)
}
mu.Unlock()
}
return http.HandlerFunc(fn)
}
// doneHandler removes (expires) the session cookie, if present.
func doneHandler(sessionProvider sessions.Store) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
sessionProvider.Destroy(w, installerSessionName)
fmt.Fprintf(w, "ok")
}
return http.HandlerFunc(fn)
}