1package handler
2
3import (
4 "net/http"
5
6 serverconfig "git.gabrielgio.me/cerrado/pkg/config"
7 "git.gabrielgio.me/cerrado/pkg/ext"
8 "git.gabrielgio.me/cerrado/pkg/handler/about"
9 "git.gabrielgio.me/cerrado/pkg/handler/config"
10 "git.gabrielgio.me/cerrado/pkg/handler/git"
11 "git.gabrielgio.me/cerrado/pkg/handler/static"
12 "git.gabrielgio.me/cerrado/pkg/service"
13)
14
15// Mount handler gets the requires service and repository to build the handlers
16// This functons wraps the whole handler package and wraps it into one part so
17// its sub package don't leak in other places.
18func MountHandler(
19 gitService *service.GitService,
20 configRepo *serverconfig.ConfigurationRepository,
21) (http.Handler, error) {
22 var (
23 gitHandler = git.NewGitHandler(gitService, configRepo)
24 aboutHandler = about.NewAboutHandler(configRepo)
25 configHander = config.ConfigFile(configRepo)
26 )
27
28 staticHandler, err := static.ServeStaticHandler()
29 if err != nil {
30 return nil, err
31 }
32
33 mux := http.NewServeMux()
34
35 mux.HandleFunc("/static/{file}", m(staticHandler))
36 mux.HandleFunc("/{name}/about/{$}", m(gitHandler.About))
37 mux.HandleFunc("/{name}", m(gitHandler.Summary))
38 mux.HandleFunc("/{name}/refs/{$}", m(gitHandler.Refs))
39 mux.HandleFunc("/{name}/tree/{ref}/{rest...}", m(gitHandler.Tree))
40 mux.HandleFunc("/{name}/blob/{ref}/{rest...}", m(gitHandler.Blob))
41 mux.HandleFunc("/{name}/log/{ref}", m(gitHandler.Log))
42 mux.HandleFunc("/config", m(configHander))
43 mux.HandleFunc("/about", m(aboutHandler.About))
44 mux.HandleFunc("/", m(gitHandler.List))
45 return mux, nil
46}
47
48func m(next func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
49 return ext.Compress(next)
50}