Files
vindn.de/backend/cmd/server/main.go
T

70 lines
1.7 KiB
Go

package main
import (
"log"
"net/http"
"os"
"whimsoc/vindn-backend/config"
"whimsoc/vindn-backend/models"
"whimsoc/vindn-backend/repositories"
"whimsoc/vindn-backend/services"
"whimsoc/vindn-backend/tasks"
"github.com/gofiber/fiber/v2"
// Embeds the IANA timezone database into the binary so TZ=Europe/Berlin
// (see docker-compose.yaml) resolves correctly even on a minimal runtime
// image that might not ship /usr/share/zoneinfo — the twice-daily cron
// schedule in tasks/Cronjob.go depends on this being right.
_ "time/tzdata"
)
func main() {
// The prod runtime image is distroless (no shell, no wget/curl), so
// Docker's HEALTHCHECK runs this binary in a second mode instead of a
// separate tool — see backend/Dockerfile.prod.
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
runHealthcheck()
return
}
// Lade .env
config.LoadEnv()
// Verbinde DB
db := config.ConnectDatabase()
models.Migrate(db)
// Repositories & Services
eventRepo := repositories.NewEventRepository(db)
eventService := services.NewEventService(eventRepo)
// Starte Cronjob
tasks.StartCronJobs(eventService)
// Direkt beim Start einmal holen
go func() {
if err := eventService.ImportEvents(); err != nil {
log.Printf("Fehler beim initialen Import: %v", err)
}
}()
app := fiber.New()
app.Get("/health", func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})
log.Printf("Start server")
// Starte Fiber
log.Fatal(app.Listen(":" + config.GetEnv("PORT", "8080")))
}
func runHealthcheck() {
resp, err := http.Get("http://localhost:" + config.GetEnv("PORT", "8080") + "/health")
if err != nil || resp.StatusCode != http.StatusOK {
os.Exit(1)
}
}