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/git"
10 "git.gabrielgio.me/cerrado/pkg/handler/static"
11 "git.gabrielgio.me/cerrado/pkg/service"
12)
13
14// Mount handler gets the requires service and repository to build the handlers
15// This functons wraps the whole handler package and wraps it into one part so
16// its sub package don't leak in other places.
17func MountHandler(
18 gitService *service.GitService,
19 configRepo *serverconfig.ConfigurationRepository,
20) (http.Handler, error) {
21 var (
22 gitHandler = git.NewGitHandler(gitService, configRepo)
23 aboutHandler = about.NewAboutHandler(configRepo)
24 )
25
26 staticHandler, err := static.ServeStaticHandler()
27 if err != nil {
28 return nil, err
29 }
30
31 mux := ext.NewRouter()
32 mux.AddMiddleware(ext.Compress)
33 mux.AddMiddleware(ext.Log)
34
35 mux.HandleFunc("/static/{file}", staticHandler)
36 mux.HandleFunc("/{name}/about/{$}", gitHandler.About)
37 mux.HandleFunc("/{name}/", gitHandler.Summary)
38 mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs)
39 mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree)
40 mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob)
41 mux.HandleFunc("/{name}/log/{ref}/", gitHandler.Log)
42 mux.HandleFunc("/{name}/commit/{ref}/", gitHandler.Commit)
43 mux.HandleFunc("/{name}/archive/{file}", gitHandler.Archive)
44 mux.HandleFunc("/about", aboutHandler.About)
45 mux.HandleFunc("/", gitHandler.List)
46 return mux.Handler(), nil
47}