-
Notifications
You must be signed in to change notification settings - Fork 11
/
srv.go
48 lines (42 loc) · 1.44 KB
/
srv.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
package main
import (
"encoding/base64"
"fmt"
"github.com/hanjm/log"
"net/http"
_ "net/http/pprof"
)
func HTTPServer(tm *TasksManager, port int, basicAuth string) {
http.Handle("/download/", http.StripPrefix("/download", http.FileServer(http.Dir(tm.downloadDir))))
http.Handle("/file_download_proxy/ws", http.HandlerFunc(tm.WebSocketHandler))
http.Handle("/file_download_proxy/task", http.HandlerFunc(tm.TaskHandler))
http.HandleFunc("/favicon.ico", HandleFile("favicon.ico"))
http.Handle("/file_download_proxy/", HandleFile("index.html"))
listenAddr := fmt.Sprintf(":%d", port)
log.Infof("service start at %s", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, Auth(http.DefaultServeMux, basicAuth)))
}
func HandleFile(filename string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, filename)
}
}
type BasicAuthHandler struct {
Token string
Next http.Handler
}
func (b *BasicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if b.Token != "" && r.Header.Get("Authorization") != b.Token {
w.Header().Set("WWW-Authenticate", `Basic realm="StatusUnauthorized"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
b.Next.ServeHTTP(w, r)
}
func Auth(handler http.Handler, basicAuth string) http.Handler {
var token string
if basicAuth != "" {
token = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(basicAuth)))
}
return &BasicAuthHandler{token, handler}
}