godash/main.go

45 lines
992 B
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"
2024-09-30 20:16:29 +02:00
"gitlab.unjx.de/flohoss/godash/pkg/logger"
2024-03-12 15:49:08 +01:00
"gitlab.unjx.de/flohoss/godash/services"
)
func main() {
2024-09-30 12:05:12 +02:00
slog.SetDefault(logger.NewLogger())
2024-03-12 15:49:08 +01:00
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()
2025-01-31 12:08:03 +01:00
appHandler := handlers.NewAppHandler(env, s, w, b)
handlers.SetupRoutes(router, sse, appHandler)
2024-03-12 15:49:08 +01:00
2024-09-25 15:33:28 +02:00
slog.Info("server listening, press ctrl+c to stop", "addr", env.PublicUrl)
err = http.ListenAndServe(fmt.Sprintf(":%d", env.Port), router)
if !errors.Is(err, http.ErrServerClosed) {
slog.Error("server terminated", "error", err)
2024-03-12 15:49:08 +01:00
os.Exit(1)
}
}