aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/router.go
blob: a8c9c6f227152e0602af3786f023508c563419d0 (plain)
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
package handler

import (
	"net/http"

	serverconfig "git.gabrielgio.me/cerrado/pkg/config"
	"git.gabrielgio.me/cerrado/pkg/handler/about"
	"git.gabrielgio.me/cerrado/pkg/handler/config"
	"git.gabrielgio.me/cerrado/pkg/handler/git"
	"git.gabrielgio.me/cerrado/pkg/handler/static"
	"git.gabrielgio.me/cerrado/pkg/service"
)

// Mount handler gets the requires service and repository to build the handlers
// This functons wraps the whole handler package and wraps it into one part so
// its sub package don't leak in other places.
func MountHandler(
	gitService *service.GitService,
	configRepo *serverconfig.ConfigurationRepository,
) (http.Handler, error) {
	var (
		gitHandler   = git.NewGitHandler(gitService)
		aboutHandler = about.NewAboutHandler(configRepo)
		configHander = config.ConfigFile(configRepo)
	)

	staticHandler, err := static.NewStaticHander("/static/")
	if err != nil {
		return nil, err
	}

	mux := http.NewServeMux()
	mux.Handle("/static/", staticHandler)
	mux.HandleFunc("/config", configHander)
	mux.HandleFunc("/about", aboutHandler.About)
	mux.HandleFunc("/", gitHandler.List)
	return mux, nil
}