-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (54 loc) · 1.27 KB
/
main.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
package main
import (
"log"
"net/http"
)
func main() {
fileSvc := newFileService()
if err := fileSvc.initDirs(); err != nil {
log.Printf("initialize directories failed: %v", err)
return
}
mux := newServeMux()
mux.HandleFunc("/upload", fileSvc.upload)
mux.HandleFunc("/show", fileSvc.show)
mux.HandleFunc("/filenames", fileSvc.listFilenames)
log.Println("listen on port: 4545")
log.Println(http.ListenAndServe(":4545", mux))
}
type mux struct {
*http.ServeMux
}
func newServeMux() *mux {
return &mux{
ServeMux: http.NewServeMux(),
}
}
// TODO(quixote-liu): optimize the origin of username and password.
const (
username = "admin@quixote_lcs"
password = "hubei@lcs_1208"
)
func (mux *mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// set cores headers
setCorsHeaders(w, r)
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
// authenticate
uname, pword, ok := r.BasicAuth()
if !ok {
responseJSON(w, http.StatusUnauthorized, H{
"error": "authentication failed: missing username or password",
})
return
}
if uname != username || pword != password {
responseJSON(w, http.StatusUnauthorized, H{
"error": "authentication failed: username or password error",
})
return
}
mux.ServeMux.ServeHTTP(w, r)
}