forked from RedHatInsights/chrome-service-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (94 loc) · 3.42 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/go-chi/cors"
"log"
"net/http"
"strconv"
"github.com/RedHatInsights/chrome-service-backend/config"
"github.com/RedHatInsights/chrome-service-backend/rest/connectionhub"
"github.com/RedHatInsights/chrome-service-backend/rest/database"
"github.com/RedHatInsights/chrome-service-backend/rest/featureflags"
"github.com/RedHatInsights/chrome-service-backend/rest/kafka"
m "github.com/RedHatInsights/chrome-service-backend/rest/middleware"
"github.com/RedHatInsights/chrome-service-backend/rest/routes"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
func init() {
godotenv.Load()
flag.Parse()
database.Init()
}
func main() {
cfg := config.Get()
featureflags.Init(cfg)
setupLogger(cfg)
router := chi.NewRouter()
metricsRouter := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.URLFormat)
router.Get("/health", HealthProbe)
fs := http.FileServer(http.Dir("./static/"))
// can't be in sub router as we don't enforce identity header
router.Handle("/api/chrome-service/v1/static/*", http.StripPrefix("/api/chrome-service/v1/static", fs))
router.Route("/api/chrome-service/v1/", func(subrouter chi.Router) {
subrouter.Use(m.ParseHeaders)
subrouter.Use(m.InjectUser)
subrouter.Get("/hello-world", HelloWorld)
subrouter.Route("/last-visited", routes.MakeLastVisitedRoutes)
subrouter.Route("/favorite-pages", routes.MakeFavoritePagesRoutes)
subrouter.Route("/self-report", routes.MakeSelfReportRoutes)
subrouter.Route("/user", routes.MakeUserIdentityRoutes)
subrouter.Route("/emit-message", routes.BroadcastMessage)
})
// We might want to set up some event listeners at some point, but the pod will
// have to restart for these to take effect. We can't enable and disable websockets on the fly
if featureflags.IsEnabled("chrome-service.websockets.enabled") {
// start the connection hub
go connectionhub.ConnectionHub.Run()
logrus.Infoln("Enabling WebSockets")
kafka.InitializeConsumers()
router.Route("/wss/chrome-service/v1/", func(subrouter chi.Router) {
subrouter.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{
"wss://stage.foo.redhat.com:1337",
"wss://prod.foo.redhat.com:1337",
},
}))
subrouter.Route("/ws", routes.MakeWsRoute)
})
} else {
logrus.Infoln("WebSockets are currently disabled")
}
metricsRouter.Handle("/metrics", promhttp.Handler())
go func() {
metricsStringAddr := fmt.Sprintf(":%s", strconv.Itoa(cfg.MetricsPort))
if err := http.ListenAndServe(metricsStringAddr, metricsRouter); err != nil {
log.Fatalf("Metrics server stopped %v", err)
}
}()
serverStringAddr := fmt.Sprintf(":%s", strconv.Itoa(cfg.WebPort))
if err := http.ListenAndServe(serverStringAddr, router); err != nil {
log.Fatalf("Chrome-service-api has stopped due to %v", err)
}
}
func HelloWorld(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("que lo que manin"))
}
func HealthProbe(response http.ResponseWriter, request *http.Request) {
response.Write([]byte("Why yes thank you, I am quite healthy :D"))
}
func setupLogger(opts *config.ChromeServiceConfig) {
logLevel, err := logrus.ParseLevel(opts.LogLevel)
if err != nil {
logLevel = logrus.InfoLevel
}
logrus.SetLevel(logLevel)
}