feat: Add Diyanet API provider

feat: Add UI
This commit is contained in:
2023-03-05 14:41:38 +01:00
parent d4ff42387f
commit 93af84cef8
17 changed files with 642 additions and 43 deletions
+31 -9
View File
@@ -9,10 +9,12 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/logger"
recover "github.com/gofiber/fiber/v2/middleware/recover"
"prayertimes/pkg/prayer"
"prayertimes/templates"
)
type httpError struct {
@@ -20,7 +22,8 @@ type httpError struct {
}
type Services struct {
PrayerTimesProvider prayer.TimesProvider
TimesProvider prayer.TimesProvider
LocationTimesProvider prayer.LocationTimesProvider
}
func New(services Services) *fiber.App {
@@ -42,29 +45,48 @@ func New(services Services) *fiber.App {
app.Use(
favicon.New(),
recover.New(recover.Config{
EnableStackTrace: true,
}),
recover.New(recover.Config{EnableStackTrace: true}),
logger.New(),
cors.New(),
)
cacheHeader := fmt.Sprintf("max-age=%0f", (time.Hour * 24).Seconds())
app.Get("/healthz", func(ctx *fiber.Ctx) error {
return ctx.SendString("OK")
})
app.Get("/api/v1/diyanet/prayertimes", func(ctx *fiber.Ctx) error {
locationID := ctx.Query("location_id")
times, err := services.PrayerTimesProvider.Get(ctx.Context(), locationID)
app.Get("/api/v1/diyanet/prayertimes", func(ctx *fiber.Ctx) error {
var times []prayer.Times
var err error
locationID := ctx.Query("location_id")
var loc struct {
Latitude float64 `query:"latitude"`
Longitude float64 `query:"longitude"`
}
if locationID != "" {
times, err = services.TimesProvider.Get(ctx.Context(), locationID)
} else if err := ctx.QueryParser(&loc); err == nil {
times, err = services.LocationTimesProvider.GetByCoords(ctx.Context(), prayer.Coordinates{
Latitude: loc.Latitude,
Longitude: loc.Latitude,
})
} else {
return fmt.Errorf("%w: missing location id or coordinates", fiber.ErrBadRequest)
}
if err != nil {
return err
}
ctx.Response().Header.Add(fiber.HeaderCacheControl, cacheHeader)
ctx.Response().Header.Add(fiber.HeaderCacheControl, "max-age=86400")
return ctx.JSON(times)
})
app.Use("/", filesystem.New(filesystem.Config{
Root: templates.HttpFS(),
}))
return app
}