7
"github.com/go-chi/chi"
8
"github.com/go-chi/chi/middleware"
9
"github.com/go-chi/render"
12
"go.avito.ru/DO/moira"
13
"go.avito.ru/DO/moira/api"
14
moira_middle "go.avito.ru/DO/moira/api/middleware"
17
var database moira.Database
18
var triggerInheritanceDatabase moira.TriggerInheritanceDatabase
19
var superUsers = make(map[string]bool)
21
const contactKey moira_middle.ContextKey = "contact"
22
const subscriptionKey moira_middle.ContextKey = "subscription"
24
// NewHandler creates new api handler request uris based on github.com/go-chi/chi
27
triggerInheritanceDb moira.TriggerInheritanceDatabase,
30
configFileContent []byte,
34
triggerInheritanceDatabase = triggerInheritanceDb
36
router := chi.NewRouter()
37
router.Use(render.SetContentType(render.ContentTypeJSON))
38
router.Use(moira_middle.UserContext)
39
router.Use(moira_middle.RequestLogger(log))
40
router.Use(moira_middle.AppVersion(appVersion))
41
router.Use(middleware.NoCache)
43
router.NotFound(notFoundHandler)
44
router.MethodNotAllowed(methodNotAllowedHandler)
46
router.Route("/api", func(router chi.Router) {
47
router.Use(moira_middle.DatabaseContext(database))
48
router.Use(moira_middle.ConfigContext(*config))
49
router.Get("/config", webConfig(configFileContent))
50
router.Route("/user", user)
51
router.Route("/trigger", triggers)
52
router.Route("/tag", tag)
53
router.Route("/pattern", pattern)
54
router.Route("/event", event)
55
router.Route("/contact", contact)
56
router.Route("/subscription", subscription)
57
router.Route("/notification", notification)
58
router.Route("/silent-pattern", silent)
59
router.Route("/global-settings", globalSettings)
60
router.Route("/stats/metrics", metricStats)
61
router.Route("/maintenance", maintenance)
64
if config.EnableCORS {
65
return cors.AllowAll().Handler(router)
68
for _, userLogin := range config.SuperUsers {
69
superUsers[userLogin] = true
75
func webConfig(content []byte) http.HandlerFunc {
76
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
78
render.Render(writer, request, api.ErrorInternalServer(fmt.Errorf("Web config file was not loaded")))
81
writer.Header().Set("Content-Type", "application/json")
86
func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
87
writer.Header().Set("X-Content-Type-Options", "nosniff")
88
writer.Header().Set("Content-Type", "application/json")
89
writer.WriteHeader(404)
90
render.Render(writer, request, api.ErrNotFound)
93
func methodNotAllowedHandler(writer http.ResponseWriter, request *http.Request) {
94
writer.Header().Set("Content-Type", "application/json")
95
writer.WriteHeader(405)
96
render.Render(writer, request, api.ErrMethodNotAllowed)