godash/main.go

44 lines
1 KiB
Go
Raw Normal View History

2024-03-12 15:49:08 +01:00
package main
import (
"errors"
2024-03-12 15:49:08 +01:00
"fmt"
"log/slog"
"net/http"
"os"
"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"
)
func main() {
env, err := env.Parse()
if err != nil {
slog.Error("cannot parse environment variables", "err", err)
os.Exit(1)
}
router := http.NewServeMux()
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-05-21 17:33:13 +02:00
appHandler := handlers.NewAppHandler(env, s, w, b)
2024-09-10 19:46:16 +02:00
authHandler := handlers.NewAuthHandler(env)
handlers.SetupRoutes(router, sse, appHandler, authHandler)
2024-03-12 15:49:08 +01:00
lis := fmt.Sprintf(":%d", env.Port)
slog.Info("server listening, press ctrl+c to stop", "addr", "http://localhost"+lis)
2024-09-16 11:45:24 +02:00
err = http.ListenAndServe(lis, authHandler.SessionManager.LoadAndSave(router))
if !errors.Is(err, http.ErrServerClosed) {
slog.Error("server terminated", "error", err)
2024-03-12 15:49:08 +01:00
os.Exit(1)
}
}