forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
35 lines (29 loc) · 841 Bytes
/
static.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
// Copyright 2015 Eleme Inc. All rights reserved.
package webapp
import (
"net/http"
"strings"
)
// Routes start with `/admin` should be authed.
const authPathPrefix = "/admin"
// staticHandler serves all non-api routes as static files.
type staticHandler struct {
// Auth
auth *authHandler
// FileServer
fileHandler http.Handler
}
// newStaticHandler creates a staticHandler.
func newStaticHandler(root http.FileSystem, auth *authHandler) *staticHandler {
fileHandler := http.FileServer(root)
return &staticHandler{auth, fileHandler}
}
// ServeHTTP implements http.Handler.
func (sh *staticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Auth /admin prefixed routes.
if strings.HasPrefix(r.URL.Path, authPathPrefix) && !sh.auth.auth(w, r) {
return
}
// Public resource.
sh.fileHandler.ServeHTTP(w, r)
}