-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (38 loc) · 1.88 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
package main
import (
"fmt"
"log/slog"
"net/http"
"os"
"rebootx-on-prem/http-server-go/config"
"rebootx-on-prem/http-server-go/resources/dashboard"
"rebootx-on-prem/http-server-go/resources/runnable"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func main() {
config := config.GetConfig()
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
dashboardService := dashboard.LoadService(config)
runnableService := runnable.LoadService(config, logger)
logger.Info(fmt.Sprintf("Using DashboardServiceImpl : %s", config.DashboardServiceImpl))
logger.Info(fmt.Sprintf("Using RunnableServiceImpl : %s", config.RunnableServiceImpl))
router := mux.NewRouter()
router.Use(logMiddleware(config, logger))
router.Use(headerMiddleware(config))
router.Use(authMiddleware(config))
rootPath := fmt.Sprintf("/%s", config.PathPrefix)
dashboardsPath := fmt.Sprintf("%s/dashboards", rootPath)
runnablesPath := fmt.Sprintf("%s/runnables", rootPath)
router.HandleFunc(dashboardsPath, getDashboardsHandler(*dashboardService)).Methods("GET")
router.HandleFunc(runnablesPath, getRunnablesHandler(*runnableService)).Methods("GET")
router.HandleFunc(fmt.Sprintf("%s/{id}/reboot", runnablesPath), postRunnableRebootHandler(*runnableService)).Methods("POST")
router.HandleFunc(fmt.Sprintf("%s/{id}/stop", runnablesPath), postRunnableStopHandler(*runnableService)).Methods("POST")
headersCORS := handlers.AllowedHeaders([]string{AUTHORIZATION_HEADER, "Content-Type", "Origin"})
methodsCORS := handlers.AllowedMethods([]string{"GET", "HEAD", "OPTIONS", "POST"})
originsCORS := handlers.AllowedOrigins([]string{"*"})
routerWithCORS := handlers.CORS(headersCORS, methodsCORS, originsCORS)(router)
logger.Info(fmt.Sprintf("Listening on %s://%s:%d", config.Protocol, config.Bind, config.Port))
http.Handle("/", routerWithCORS)
http.ListenAndServe(fmt.Sprintf("%s:%d", config.Bind, config.Port), nil)
}