godash/main.go

71 lines
1.7 KiB
Go
Raw Normal View History

2024-03-12 15:49:08 +01:00
package main
import (
"fmt"
"log/slog"
"net/http"
"os"
"strings"
2024-03-30 00:27:23 +01:00
"time"
2024-03-12 15:49:08 +01:00
"github.com/labstack/echo-contrib/session"
2024-03-12 15:49:08 +01:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/r3labs/sse/v2"
2024-03-30 00:27:23 +01:00
2024-03-12 15:49:08 +01:00
"gitlab.unjx.de/flohoss/godash/handlers"
"gitlab.unjx.de/flohoss/godash/internal/env"
"gitlab.unjx.de/flohoss/godash/services"
2024-03-30 00:27:23 +01:00
"github.com/glebarez/sqlite"
"github.com/wader/gormstore/v2"
"gorm.io/gorm"
2024-03-12 15:49:08 +01:00
)
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.Use(middleware.Recover())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: func(c echo.Context) bool {
return strings.Contains(c.Path(), "sse") || strings.Contains(c.Path(), "sign")
2024-03-12 15:49:08 +01:00
},
}))
2024-03-30 00:27:23 +01:00
if env.SSOEndpoint != "" {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
slog.Error("cannot connect to database", "err", err)
os.Exit(1)
}
store := gormstore.New(db, []byte(env.SessionKey))
quit := make(chan struct{})
go store.PeriodicCleanup(1*time.Hour, quit)
e.Use(session.Middleware(store))
}
2024-03-12 15:49:08 +01:00
sse := sse.New()
sse.AutoReplay = false
s := services.NewSystemService(sse)
2024-03-15 18:56:32 +01:00
w := services.NewWeatherService(sse, env)
2024-03-12 15:49:08 +01:00
b := services.NewBookmarkService()
2024-03-18 21:54:35 +01:00
authHandler := handlers.NewAuthHandler(env)
2024-03-25 20:31:51 +01:00
appHandler := handlers.NewAppHandler(env, authHandler, s, w, b)
2024-03-18 21:54:35 +01:00
handlers.SetupRoutes(e, sse, appHandler, authHandler)
2024-03-12 15:49:08 +01:00
2024-03-18 21:54:35 +01:00
slog.Info("starting server", "url", env.PublicUrl)
2024-03-19 11:36:09 +01:00
if err := e.Start(fmt.Sprintf("0.0.0.0:%d", env.Port)); err != http.ErrServerClosed {
2024-03-12 15:49:08 +01:00
slog.Error("cannot start server", "err", err)
os.Exit(1)
}
}