-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
61 lines (47 loc) · 1.89 KB
/
server.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
package server
import (
"net/http"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
httpSwagger "github.com/swaggo/http-swagger"
_ "github.com/BedrockStreaming/prescaling-exporter/docs"
"github.com/BedrockStreaming/prescaling-exporter/pkg/config"
"github.com/BedrockStreaming/prescaling-exporter/pkg/handlers"
)
// @title Prescaling API
// @version 1.0.0
// @description This API was built with FastAPI to deal with prescaling recordings in CRD
type IServer interface {
Initialize() error
}
type Server struct {
statusHandler handlers.IStatusHandlers
eventHandlers handlers.IEventHandlers
}
func NewServer(statusHandler handlers.IStatusHandlers, eventHandlers handlers.IEventHandlers) IServer {
return &Server{
statusHandler: statusHandler,
eventHandlers: eventHandlers,
}
}
func (s *Server) Initialize() error {
router := mux.NewRouter()
router.PathPrefix("/swagger/").Handler(httpSwagger.Handler(
httpSwagger.URL("doc.json"), //The url pointing to API definition
httpSwagger.DeepLinking(true),
httpSwagger.DocExpansion("none"),
httpSwagger.DomID("swagger-ui"),
)).Methods(http.MethodGet)
router.Handle("/metrics", promhttp.Handler())
router.HandleFunc("/status", s.statusHandler.Index)
apiv1 := router.PathPrefix("/api/v1/events").Subrouter()
apiv1.HandleFunc("/", s.eventHandlers.List).Methods(http.MethodGet)
apiv1.HandleFunc("/", s.eventHandlers.Create).Methods(http.MethodPost)
apiv1.HandleFunc("/current", s.eventHandlers.Current).Methods(http.MethodGet)
apiv1.HandleFunc("/{name}", s.eventHandlers.Get).Methods(http.MethodGet)
apiv1.HandleFunc("/{name}", s.eventHandlers.Update).Methods(http.MethodPut)
apiv1.HandleFunc("/{name}", s.eventHandlers.Delete).Methods(http.MethodDelete)
log.Info("Listen on port: ", config.Config.Port)
return http.ListenAndServe(":"+config.Config.Port, router)
}