zdravko/pkg/server/server.go

120 lines
3.2 KiB
Go
Raw Normal View History

2024-02-16 21:31:00 +00:00
package server
import (
2024-02-16 21:31:00 +00:00
"context"
2024-02-27 11:04:05 +00:00
"log/slog"
"net/http"
2024-02-27 11:04:05 +00:00
"code.tjo.space/mentos1386/zdravko/database"
"code.tjo.space/mentos1386/zdravko/internal/config"
"code.tjo.space/mentos1386/zdravko/internal/handlers"
2024-02-16 21:31:00 +00:00
"code.tjo.space/mentos1386/zdravko/internal/temporal"
"code.tjo.space/mentos1386/zdravko/web/static"
"code.tjo.space/mentos1386/zdravko/web/templates"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
2024-02-16 21:31:00 +00:00
type Server struct {
2024-02-27 11:04:05 +00:00
echo *echo.Echo
cfg *config.ServerConfig
logger *slog.Logger
2024-02-16 21:31:00 +00:00
}
func NewServer(cfg *config.ServerConfig) (*Server, error) {
2024-02-16 21:31:00 +00:00
return &Server{
2024-02-27 11:04:05 +00:00
cfg: cfg,
echo: echo.New(),
logger: slog.Default().WithGroup("server"),
2024-02-16 21:31:00 +00:00
}, nil
}
func (s *Server) Name() string {
return "HTTP WEB and API Server"
}
2024-02-11 09:15:00 +00:00
2024-02-16 21:31:00 +00:00
func (s *Server) Start() error {
2024-02-21 09:41:20 +00:00
s.echo.Renderer = templates.NewTemplates()
2024-02-24 21:07:49 +00:00
//s.echo.Use(middleware.Logger())
2024-02-21 09:41:20 +00:00
s.echo.Use(middleware.Recover())
2024-02-27 11:04:05 +00:00
db, err := database.ConnectToDatabase(s.logger, s.cfg.DatabasePath)
2024-02-11 10:56:21 +00:00
if err != nil {
2024-02-16 21:31:00 +00:00
return err
2024-02-11 10:56:21 +00:00
}
2024-02-27 11:04:05 +00:00
temporalClient, err := temporal.ConnectServerToTemporal(s.logger, s.cfg)
if err != nil {
2024-02-16 21:31:00 +00:00
return err
}
2024-02-27 11:04:05 +00:00
h := handlers.NewBaseHandler(db, temporalClient, s.cfg, s.logger)
// Health
2024-02-21 09:41:20 +00:00
s.echo.GET("/health", func(c echo.Context) error {
2024-02-27 11:04:05 +00:00
err = db.Ping()
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{"status": "ok"})
})
2024-02-11 10:56:21 +00:00
// Server static files
2024-02-21 09:41:20 +00:00
stat := s.echo.Group("/static")
stat.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Filesystem: http.FS(static.Static),
}))
// Public
2024-02-21 09:41:20 +00:00
s.echo.GET("", h.Index)
2024-02-23 08:36:13 +00:00
s.echo.GET("/incidents", h.Incidents)
// Settings
2024-02-21 09:41:20 +00:00
settings := s.echo.Group("/settings")
settings.Use(h.Authenticated)
settings.GET("", h.SettingsOverviewGET)
settings.GET("/monitors", h.SettingsMonitorsGET)
settings.GET("/monitors/create", h.SettingsMonitorsCreateGET)
settings.POST("/monitors/create", h.SettingsMonitorsCreatePOST)
settings.GET("/monitors/:slug", h.SettingsMonitorsDescribeGET)
settings.POST("/monitors/:slug", h.SettingsMonitorsDescribePOST)
2024-02-24 21:07:49 +00:00
settings.GET("/worker-groups", h.SettingsWorkerGroupsGET)
settings.GET("/worker-groups/create", h.SettingsWorkerGroupsCreateGET)
settings.POST("/worker-groups/create", h.SettingsWorkerGroupsCreatePOST)
settings.GET("/worker-groups/:slug", h.SettingsWorkerGroupsDescribeGET)
settings.Match([]string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}, "/temporal*", h.Temporal)
// OAuth2
2024-02-21 09:41:20 +00:00
oauth2 := s.echo.Group("/oauth2")
oauth2.GET("/login", h.OAuth2LoginGET)
oauth2.GET("/callback", h.OAuth2CallbackGET)
oauth2.GET("/logout", h.OAuth2LogoutGET, h.Authenticated)
// API
2024-02-21 09:41:20 +00:00
apiv1 := s.echo.Group("/api/v1")
apiv1.Use(h.Authenticated)
apiv1.GET("/workers/connect", h.ApiV1WorkersConnectGET)
apiv1.POST("/monitors/:slug/history", h.ApiV1MonitorsHistoryPOST)
// Error handler
2024-02-21 09:41:20 +00:00
s.echo.HTTPErrorHandler = func(err error, c echo.Context) {
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}
if code == http.StatusNotFound {
_ = h.Error404(c)
return
}
_ = c.String(code, err.Error())
2024-02-16 21:31:00 +00:00
}
2024-02-21 09:41:20 +00:00
return s.echo.Start(":" + s.cfg.Port)
2024-02-16 21:31:00 +00:00
}
func (s *Server) Stop() error {
ctx := context.Background()
2024-02-21 09:41:20 +00:00
return s.echo.Shutdown(ctx)
}