-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathhandler.go
151 lines (137 loc) · 5.67 KB
/
handler.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package handler
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
metricSource "github.com/moira-alert/moira/metric_source"
"github.com/rs/cors"
httpSwagger "github.com/swaggo/http-swagger"
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/api"
moiramiddle "github.com/moira-alert/moira/api/middleware"
_ "github.com/moira-alert/moira/docs" // docs is generated by Swag CLI, you have to import it.
)
var (
database moira.Database
searchIndex moira.Searcher
)
const (
contactKey moiramiddle.ContextKey = "contact"
subscriptionKey moiramiddle.ContextKey = "subscription"
)
// NewHandler creates new api handler request uris based on github.com/go-chi/chi.
func NewHandler(
db moira.Database,
log moira.Logger,
index moira.Searcher,
apiConfig *api.Config,
metricSourceProvider *metricSource.SourceProvider,
webConfig *api.WebConfig,
) http.Handler {
database = db
searchIndex = index
var contactsTemplate []api.WebContact
if webConfig != nil {
contactsTemplate = webConfig.Contacts
}
contactsTemplateMiddleware := moiramiddle.ContactsTemplateContext(contactsTemplate)
router := chi.NewRouter()
router.Use(render.SetContentType(render.ContentTypeJSON))
router.Use(moiramiddle.UserContext)
router.Use(moiramiddle.RequestLogger(log))
router.Use(middleware.NoCache)
router.Use(moiramiddle.LimitsContext(apiConfig.Limits))
router.NotFound(notFoundHandler)
router.MethodNotAllowed(methodNotAllowedHandler)
// @title Moira Alert
// @version master
// @description This is an API description for [Moira Alert API](https://moira.readthedocs.io/en/latest/overview.html)
// @description Check us out on [Github](https://github.com/moira-alert) or look up our [guide](https://moira.readthedocs.io) on getting started with Moira
// @contact.name Moira Team
// @contact.email [email protected]
// @license.name MIT
// @BasePath /api
//
// @tag.name contact
// @tag.description APIs for working with Moira contacts. For more details, see <https://moira.readthedocs.io/en/latest/installation/webhooks_scripts.html#contact/>
//
// @tag.name config
// @tag.description View Moira's runtime configuration. For more details, see <https://moira.readthedocs.io/en/latest/installation/configuration.html>
//
// @tag.name event
// @tag.description APIs for interacting with notification events. See <https://moira.readthedocs.io/en/latest/user_guide/trigger_page.html#event-history/> for details
//
// @tag.name health
// @tag.description interact with Moira states/health status. See <https://moira.readthedocs.io/en/latest/user_guide/selfstate.html#self-state-monitor/> for details
//
// @tag.name notification
// @tag.description manage notifications that are currently in queue. See <https://moira.readthedocs.io/en/latest/user_guide/hidden_pages.html#notifications/>
//
// @tag.name pattern
// @tag.description APIs for interacting with graphite patterns in Moira. See <https://moira.readthedocs.io/en/latest/development/architecture.html#pattern/>
//
// @tag.name subscription
// @tag.description APIs for managing a user's subscription(s). See <https://moira.readthedocs.io/en/latest/development/architecture.html#subscription/> to learn about Moira subscriptions
//
// @tag.name tag
// @tag.description APIs for managing tags (a grouping of tags and subscriptions). See <https://moira.readthedocs.io/en/latest/user_guide/subscriptions.html#tags/>
//
// @tag.name trigger
// @tag.description APIs for interacting with Moira triggers. See <https://moira.readthedocs.io/en/latest/development/architecture.html#trigger/> to learn about Triggers
//
// @tag.name team
// @tag.description APIs for interacting with Moira teams
//
// @tag.name teamSubscription
// @tag.description APIs for interacting with Moira subscriptions owned by certain team
//
// @tag.name teamContact
// @tag.description APIs for interacting with Moira contacts owned by certain team
//
// @tag.name user
// @tag.description APIs for interacting with Moira users
router.Route("/api", func(router chi.Router) {
router.Use(moiramiddle.DatabaseContext(database))
router.Use(moiramiddle.AuthorizationContext(&apiConfig.Authorization))
router.Route("/health", health)
router.Route("/", func(router chi.Router) {
router.Use(moiramiddle.ReadOnlyMiddleware(apiConfig))
router.Get("/config", getWebConfig(webConfig))
router.Route("/user", user)
router.With(moiramiddle.Triggers(
apiConfig.MetricsTTL,
)).Route("/trigger", triggers(metricSourceProvider, searchIndex))
router.Route("/tag", tag)
router.Route("/pattern", pattern)
router.Route("/event", event)
router.Route("/subscription", subscription)
router.Route("/notification", notification)
router.With(contactsTemplateMiddleware).
Route("/teams", teams)
router.With(contactsTemplateMiddleware).
Route("/contact", func(router chi.Router) {
contact(router)
contactEvents(router)
})
router.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("/api/swagger/doc.json"),
))
})
})
if apiConfig.EnableCORS {
return cors.AllowAll().Handler(router)
}
return router
}
func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Content-Type-Options", "nosniff")
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusNotFound)
render.Render(writer, request, api.ErrNotFound) //nolint
}
func methodNotAllowedHandler(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusMethodNotAllowed)
render.Render(writer, request, api.ErrMethodNotAllowed) //nolint
}