2024-02-16 21:31:00 +00:00
|
|
|
package server
|
2024-02-10 11:59:58 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-16 21:31:00 +00:00
|
|
|
"context"
|
2024-02-27 11:04:05 +00:00
|
|
|
"log/slog"
|
2024-02-10 11:59:58 +00:00
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2024-05-23 16:33:30 +00:00
|
|
|
"github.com/mentos1386/zdravko/database"
|
|
|
|
"github.com/mentos1386/zdravko/internal/config"
|
|
|
|
"github.com/mentos1386/zdravko/internal/temporal"
|
|
|
|
"github.com/mentos1386/zdravko/web/templates"
|
2024-04-28 14:06:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-02-10 11:59:58 +00:00
|
|
|
)
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
type Server struct {
|
2024-06-07 09:43:32 +00:00
|
|
|
echo *echo.Echo
|
|
|
|
cfg *config.ServerConfig
|
|
|
|
logger *slog.Logger
|
|
|
|
version string
|
2024-02-27 22:47:20 +00:00
|
|
|
|
|
|
|
worker *Worker
|
2024-02-16 21:31:00 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 09:43:32 +00:00
|
|
|
func NewServer(version string, cfg *config.ServerConfig) (*Server, error) {
|
2024-02-16 21:31:00 +00:00
|
|
|
return &Server{
|
2024-06-07 09:43:32 +00:00
|
|
|
cfg: cfg,
|
|
|
|
echo: echo.New(),
|
|
|
|
logger: slog.Default(),
|
|
|
|
version: version,
|
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-04-28 14:06:49 +00:00
|
|
|
sqliteDb, err := database.ConnectToDatabase(s.logger, s.cfg.SqliteDatabasePath)
|
2024-02-11 10:56:21 +00:00
|
|
|
if err != nil {
|
2024-04-28 14:06:49 +00:00
|
|
|
return errors.Wrap(err, "failed to connect to sqlite")
|
2024-02-11 10:56:21 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
temporalClient, err := temporal.ConnectServerToTemporal(s.logger, s.cfg)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
2024-04-28 14:06:49 +00:00
|
|
|
return errors.Wrap(err, "failed to connect to temporal")
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
kvStore, err := database.NewBadgerKeyValueStore(s.cfg.KeyValueDatabasePath)
|
2024-04-28 14:06:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to open kv store")
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 16:33:30 +00:00
|
|
|
s.worker = NewWorker(temporalClient, s.cfg, s.logger, sqliteDb, kvStore)
|
2024-02-11 22:48:37 +00:00
|
|
|
|
2024-06-07 09:43:32 +00:00
|
|
|
templates, err := templates.NewTemplates(s.version, s.logger)
|
2024-05-29 14:39:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create templates")
|
|
|
|
}
|
|
|
|
s.echo.Renderer = templates
|
|
|
|
|
2024-02-28 15:40:24 +00:00
|
|
|
s.echo.Use(middleware.Logger())
|
2024-02-27 22:47:20 +00:00
|
|
|
s.echo.Use(middleware.Recover())
|
2024-02-29 14:12:20 +00:00
|
|
|
s.echo.Use(middleware.Secure())
|
2024-04-28 14:06:49 +00:00
|
|
|
Routes(s.echo, sqliteDb, kvStore, temporalClient, s.cfg, s.logger)
|
2024-02-12 10:22:14 +00:00
|
|
|
|
2024-02-27 22:47:20 +00:00
|
|
|
go func() {
|
|
|
|
if err := s.worker.Start(); err != nil {
|
|
|
|
panic(err)
|
2024-02-20 10:24:04 +00:00
|
|
|
}
|
2024-02-27 22:47:20 +00:00
|
|
|
}()
|
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 {
|
2024-02-27 22:47:20 +00:00
|
|
|
s.worker.Stop()
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
ctx := context.Background()
|
2024-02-21 09:41:20 +00:00
|
|
|
return s.echo.Shutdown(ctx)
|
2024-02-10 11:59:58 +00:00
|
|
|
}
|