godash/main.go

64 lines
1.5 KiB
Go
Raw Permalink 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"
2024-09-25 15:33:28 +02:00
"net/url"
2024-03-12 15:49:08 +01:00
"os"
2024-09-25 15:33:28 +02:00
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
2024-03-12 15:49:08 +01:00
"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()
2024-09-25 15:33:28 +02:00
parsedUrl, _ := url.Parse(env.PublicUrl)
2024-09-25 15:49:21 +02:00
secret := []byte(env.SessionKey)
if len(secret) == 0 {
secret = securecookie.GenerateRandomKey(32)
}
store := sessions.NewCookieStore(secret)
2024-09-25 15:33:28 +02:00
store.Options = &sessions.Options{
Domain: parsedUrl.Hostname(),
MaxAge: 86400 * 30,
Secure: parsedUrl.Scheme == "https",
HttpOnly: true,
Partitioned: true,
SameSite: http.SameSiteLaxMode,
}
authHandler := handlers.NewAuthHandler(env, store)
appHandler := handlers.NewAppHandler(env, store, s, w, b)
2024-09-10 19:46:16 +02:00
handlers.SetupRoutes(router, sse, appHandler, authHandler)
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)
}
}