forked from restic/rest-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
79 lines (67 loc) · 2.01 KB
/
mux.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
package restserver
import (
"log"
"net/http"
"os"
goji "goji.io"
"github.com/gorilla/handlers"
"goji.io/pat"
)
var Config = struct {
Path string
Listen string
TLS bool
Log string
CPUProfile string
Debug bool
AppendOnly bool
}{
Path: "/tmp/restic",
Listen: ":8000",
AppendOnly: false,
}
func debugHandler(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL)
next.ServeHTTP(w, r)
})
}
func logHandler(next http.Handler) http.Handler {
accessLog, err := os.OpenFile(Config.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("error: %v", err)
}
return handlers.CombinedLoggingHandler(accessLog, next)
}
func NewMux() *goji.Mux {
mux := goji.NewMux()
if Config.Debug {
mux.Use(debugHandler)
}
if Config.Log != "" {
mux.Use(logHandler)
}
mux.HandleFunc(pat.Head("/config"), CheckConfig)
mux.HandleFunc(pat.Head("/:repo/config"), CheckConfig)
mux.HandleFunc(pat.Get("/config"), GetConfig)
mux.HandleFunc(pat.Get("/:repo/config"), GetConfig)
mux.HandleFunc(pat.Post("/config"), SaveConfig)
mux.HandleFunc(pat.Post("/:repo/config"), SaveConfig)
mux.HandleFunc(pat.Delete("/config"), DeleteConfig)
mux.HandleFunc(pat.Delete("/:repo/config"), DeleteConfig)
mux.HandleFunc(pat.Get("/:type/"), ListBlobs)
mux.HandleFunc(pat.Get("/:repo/:type/"), ListBlobs)
mux.HandleFunc(pat.Head("/:type/:name"), CheckBlob)
mux.HandleFunc(pat.Head("/:repo/:type/:name"), CheckBlob)
mux.HandleFunc(pat.Get("/:type/:name"), GetBlob)
mux.HandleFunc(pat.Get("/:repo/:type/:name"), GetBlob)
mux.HandleFunc(pat.Post("/:type/:name"), SaveBlob)
mux.HandleFunc(pat.Post("/:repo/:type/:name"), SaveBlob)
mux.HandleFunc(pat.Delete("/:type/:name"), DeleteBlob)
mux.HandleFunc(pat.Delete("/:repo/:type/:name"), DeleteBlob)
mux.HandleFunc(pat.Post("/"), CreateRepo)
mux.HandleFunc(pat.Post("/:repo"), CreateRepo)
mux.HandleFunc(pat.Post("/:repo/"), CreateRepo)
return mux
}