cerrado @ 3784181e4fad3c947dfa95081d8a0b34f5be12d4

 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/auth"
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	authService *service.AuthService,
21	configRepo *serverconfig.ConfigurationRepository,
22) (http.Handler, error) {
23	var (
24		gitHandler   = git.NewGitHandler(gitService, configRepo)
25		aboutHandler = about.NewAboutHandler(configRepo)
26		loginHandler = auth.NewLoginHandler(authService)
27	)
28
29	staticHandler, err := static.ServeStaticHandler()
30	if err != nil {
31		return nil, err
32	}
33
34	mux := ext.NewRouter()
35	mux.AddMiddleware(ext.Compress)
36	mux.AddMiddleware(ext.Log)
37
38	if configRepo.IsAuthEnabled() {
39		mux.AddMiddleware(ext.Authenticate(authService))
40		mux.HandleFunc("/login/{$}", loginHandler.Login)
41		mux.HandleFunc("/logout/{$}", loginHandler.Logout)
42	} else {
43		mux.AddMiddleware(ext.DisableAuthentication)
44	}
45
46	mux.HandleFunc("/static/{file}", staticHandler)
47	mux.HandleFunc("/{name}/about/{$}", gitHandler.About)
48	mux.HandleFunc("/{name}/", gitHandler.Summary)
49	mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs)
50	mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree)
51	mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob)
52	mux.HandleFunc("/{name}/log/{ref}/", gitHandler.Log)
53	mux.HandleFunc("/{name}/commit/{ref}/", gitHandler.Commit)
54	mux.HandleFunc("/{name}/archive/{file}", gitHandler.Archive)
55	mux.HandleFunc("/about", aboutHandler.About)
56	mux.HandleFunc("/", gitHandler.List)
57	return mux.Handler(), nil
58}