Compare commits
2 Commits
d6859d8cf9
...
16cb8c7f58
| Author | SHA1 | Date | |
|---|---|---|---|
| 16cb8c7f58 | |||
| 8896188ec4 |
@@ -13,7 +13,10 @@ type authHandler struct {
|
|||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func Route(config *config.Config) func(chi.Router) {
|
func MustRoute(config *config.Config) func(chi.Router) {
|
||||||
|
if config == nil {
|
||||||
|
panic("config is nil")
|
||||||
|
}
|
||||||
h := &authHandler{
|
h := &authHandler{
|
||||||
cfg: config,
|
cfg: config,
|
||||||
}
|
}
|
||||||
@@ -31,4 +34,4 @@ func (h *authHandler) handleLogout(w http.ResponseWriter, r *http.Request) {}
|
|||||||
|
|
||||||
func (h *authHandler) handleMe(w http.ResponseWriter, r *http.Request) {}
|
func (h *authHandler) handleMe(w http.ResponseWriter, r *http.Request) {}
|
||||||
|
|
||||||
func (h *authHandler) handleRevoke(w http.ResponseWriter, r *http.Request) {}
|
func (h *authHandler) handleRevoke(w http.ResponseWriter, r *http.Request) {}
|
||||||
|
|||||||
@@ -29,7 +29,10 @@ type blockHandler struct {
|
|||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func Route(config *config.Config) func(chi.Router) {
|
func MustRoute(config *config.Config) func(chi.Router) {
|
||||||
|
if config == nil {
|
||||||
|
panic("config is nil")
|
||||||
|
}
|
||||||
h := &blockHandler{
|
h := &blockHandler{
|
||||||
cfg: config,
|
cfg: config,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func NewRouter(cfg *config.Config) *Router {
|
|||||||
|
|
||||||
// RouteHandler sets up the routes and middleware for the router.
|
// RouteHandler sets up the routes and middleware for the router.
|
||||||
// TODO: implement hot reload for static files enabled/disabled
|
// TODO: implement hot reload for static files enabled/disabled
|
||||||
func (r *Router) RouteHandler() chi.Router {
|
func (r *Router) MustRoute() chi.Router {
|
||||||
r.r.Use(middleware.Logger)
|
r.r.Use(middleware.Logger)
|
||||||
r.r.Use(middleware.Recoverer)
|
r.r.Use(middleware.Recoverer)
|
||||||
r.r.Use(middleware.Timeout(r.cfg.Server.TimeoutSeconds))
|
r.r.Use(middleware.Timeout(r.cfg.Server.TimeoutSeconds))
|
||||||
@@ -58,8 +58,8 @@ func (r *Router) RouteHandler() chi.Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.r.Route("/api", func(api chi.Router) {
|
r.r.Route("/api", func(api chi.Router) {
|
||||||
api.Route("/block", block.Route(r.cfg))
|
api.Route("/block", block.MustRoute(r.cfg))
|
||||||
api.Route("/auth", auth.Route(r.cfg))
|
api.Route("/auth", auth.MustRoute(r.cfg))
|
||||||
})
|
})
|
||||||
|
|
||||||
r.r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
r.r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
18
cmd/serve.go
18
cmd/serve.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime/debug"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"git.oblat.lv/alex/triggerssmith/api"
|
"git.oblat.lv/alex/triggerssmith/api"
|
||||||
@@ -81,7 +82,20 @@ var serveCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
slog.Error("Application panicked", slog.Any("error", r))
|
stack := debug.Stack()
|
||||||
|
|
||||||
|
f, err := os.OpenFile("panic.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to open panic.log", slog.Any("error", err))
|
||||||
|
} else {
|
||||||
|
defer f.Close()
|
||||||
|
f.WriteString(fmt.Sprintf("Panic: %v\n", r))
|
||||||
|
f.Write(stack)
|
||||||
|
f.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Error("Application panicked: the stack is flushed to disk", slog.Any("error", r))
|
||||||
|
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -130,7 +144,7 @@ var serveCmd = &cobra.Command{
|
|||||||
|
|
||||||
router := api.NewRouter(cfg)
|
router := api.NewRouter(cfg)
|
||||||
|
|
||||||
srv.SetHandler(router.RouteHandler())
|
srv.SetHandler(router.MustRoute())
|
||||||
srv.Init()
|
srv.Init()
|
||||||
|
|
||||||
var addr = net.JoinHostPort(cfg.Server.Addr, fmt.Sprintf("%d", cfg.Server.Port))
|
var addr = net.JoinHostPort(cfg.Server.Addr, fmt.Sprintf("%d", cfg.Server.Port))
|
||||||
|
|||||||
Reference in New Issue
Block a user