godash/main.go
2024-03-12 15:49:08 +01:00

56 lines
1.2 KiB
Go

package main
import (
"fmt"
"log/slog"
"net/http"
"os"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/r3labs/sse/v2"
"gitlab.unjx.de/flohoss/godash/handlers"
"gitlab.unjx.de/flohoss/godash/internal/env"
"gitlab.unjx.de/flohoss/godash/services"
)
func main() {
env, err := env.Parse()
if err != nil {
slog.Error("cannot parse environment variables", "err", err)
os.Exit(1)
}
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.Debug = true
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Recover())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: func(c echo.Context) bool {
return strings.Contains(c.Path(), "sse")
},
}))
e.Static("/", "assets")
e.Static("/storage/icons", "storage/icons")
sse := sse.New()
sse.AutoReplay = false
sse.CreateStream("weather")
s := services.NewSystemService(sse)
b := services.NewBookmarkService()
ah := handlers.NewAppHandler(env, s, b)
handlers.SetupRoutes(e, sse, ah)
slog.Info("starting server", "url", fmt.Sprintf("http://localhost:%d", env.Port))
if err := e.Start(fmt.Sprintf(":%d", env.Port)); err != http.ErrServerClosed {
slog.Error("cannot start server", "err", err)
os.Exit(1)
}
}