mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
Refactor handlers: make Config not global
This commit is contained in:
parent
7dd5483ea3
commit
b98c171644
5 changed files with 212 additions and 196 deletions
83
mux.go
83
mux.go
|
@ -12,28 +12,7 @@ import (
|
|||
"goji.io/pat"
|
||||
)
|
||||
|
||||
// Config struct holds program configuration.
|
||||
var Config = struct {
|
||||
Path string
|
||||
Listen string
|
||||
Log string
|
||||
CPUProfile string
|
||||
TLSKey string
|
||||
TLSCert string
|
||||
TLS bool
|
||||
NoAuth bool
|
||||
AppendOnly bool
|
||||
PrivateRepos bool
|
||||
Prometheus bool
|
||||
Debug bool
|
||||
Version bool
|
||||
}{
|
||||
Path: "/tmp/restic",
|
||||
Listen: ":8000",
|
||||
AppendOnly: false,
|
||||
}
|
||||
|
||||
func debugHandler(next http.Handler) http.Handler {
|
||||
func (c Config) debugHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%s %s", r.Method, r.URL)
|
||||
|
@ -41,8 +20,8 @@ func debugHandler(next http.Handler) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func logHandler(next http.Handler) http.Handler {
|
||||
accessLog, err := os.OpenFile(Config.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
func (c Config) logHandler(next http.Handler) http.Handler {
|
||||
accessLog, err := os.OpenFile(c.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
}
|
||||
|
@ -50,43 +29,43 @@ func logHandler(next http.Handler) http.Handler {
|
|||
return handlers.CombinedLoggingHandler(accessLog, next)
|
||||
}
|
||||
|
||||
// NewMux is master HTTP multiplexer/router.
|
||||
func NewMux() *goji.Mux {
|
||||
// NewHandler returns the master HTTP multiplexer/router.
|
||||
func NewHandler(config Config) *goji.Mux {
|
||||
mux := goji.NewMux()
|
||||
|
||||
if Config.Debug {
|
||||
mux.Use(debugHandler)
|
||||
if config.Debug {
|
||||
mux.Use(config.debugHandler)
|
||||
}
|
||||
|
||||
if Config.Log != "" {
|
||||
mux.Use(logHandler)
|
||||
if config.Log != "" {
|
||||
mux.Use(config.logHandler)
|
||||
}
|
||||
|
||||
if Config.Prometheus {
|
||||
if config.Prometheus {
|
||||
mux.Handle(pat.Get("/metrics"), promhttp.Handler())
|
||||
}
|
||||
|
||||
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)
|
||||
mux.HandleFunc(pat.Head("/config"), config.CheckConfig)
|
||||
mux.HandleFunc(pat.Head("/:repo/config"), config.CheckConfig)
|
||||
mux.HandleFunc(pat.Get("/config"), config.GetConfig)
|
||||
mux.HandleFunc(pat.Get("/:repo/config"), config.GetConfig)
|
||||
mux.HandleFunc(pat.Post("/config"), config.SaveConfig)
|
||||
mux.HandleFunc(pat.Post("/:repo/config"), config.SaveConfig)
|
||||
mux.HandleFunc(pat.Delete("/config"), config.DeleteConfig)
|
||||
mux.HandleFunc(pat.Delete("/:repo/config"), config.DeleteConfig)
|
||||
mux.HandleFunc(pat.Get("/:type/"), config.ListBlobs)
|
||||
mux.HandleFunc(pat.Get("/:repo/:type/"), config.ListBlobs)
|
||||
mux.HandleFunc(pat.Head("/:type/:name"), config.CheckBlob)
|
||||
mux.HandleFunc(pat.Head("/:repo/:type/:name"), config.CheckBlob)
|
||||
mux.HandleFunc(pat.Get("/:type/:name"), config.GetBlob)
|
||||
mux.HandleFunc(pat.Get("/:repo/:type/:name"), config.GetBlob)
|
||||
mux.HandleFunc(pat.Post("/:type/:name"), config.SaveBlob)
|
||||
mux.HandleFunc(pat.Post("/:repo/:type/:name"), config.SaveBlob)
|
||||
mux.HandleFunc(pat.Delete("/:type/:name"), config.DeleteBlob)
|
||||
mux.HandleFunc(pat.Delete("/:repo/:type/:name"), config.DeleteBlob)
|
||||
mux.HandleFunc(pat.Post("/"), config.CreateRepo)
|
||||
mux.HandleFunc(pat.Post("/:repo"), config.CreateRepo)
|
||||
mux.HandleFunc(pat.Post("/:repo/"), config.CreateRepo)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue