-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (42 loc) · 1.19 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
package main
import (
"log/slog"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/libops/ghat/lib/handler"
)
func main() {
wh, err := handler.NewHandler()
if err != nil {
slog.Error("Failed to initialize handler", "error", err)
os.Exit(1)
}
r := mux.NewRouter()
r.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("ok"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
slog.Error("Unable to write for healthcheck", "error", err)
}
}).Methods("GET")
authRouter := r.PathPrefix("/").Subrouter()
authRouter.Use(handler.LoggingMiddleware)
authRouter.Use(handler.JWTAuthMiddleware)
authRouter.HandleFunc("/repo/admin", wh.RepoAdminToken).Methods("GET")
authRouter.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`ok`))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
slog.Error("Unable to write for healthcheck", "error", err)
}
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
slog.Info("Server is starting", "port", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
slog.Error("Server failed", "err", err)
}
}