feat: Initial prototype
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cache"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/fiber/v2/middleware/favicon"
|
||||
recover "github.com/gofiber/fiber/v2/middleware/recover"
|
||||
|
||||
"prayertimes/pkg/prayer"
|
||||
)
|
||||
|
||||
type httpError struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Services struct {
|
||||
PrayerTimesProvider prayer.TimesProvider
|
||||
}
|
||||
|
||||
func New(services Services) *fiber.App {
|
||||
app := fiber.New(fiber.Config{
|
||||
Immutable: true,
|
||||
DisableStartupMessage: false,
|
||||
ReadTimeout: time.Second * 10,
|
||||
WriteTimeout: time.Second * 30,
|
||||
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
||||
if errors.Is(err, prayer.ErrInvalidLocation) {
|
||||
return ctx.Status(http.StatusUnprocessableEntity).JSON(httpError{
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return fiber.DefaultErrorHandler(ctx, err)
|
||||
},
|
||||
})
|
||||
|
||||
app.Use(
|
||||
recover.New(recover.Config{
|
||||
EnableStackTrace: true,
|
||||
}),
|
||||
favicon.New(),
|
||||
cors.New(),
|
||||
)
|
||||
|
||||
app.Get("/diyanet/prayertimes", cache.New(), func(ctx *fiber.Ctx) error {
|
||||
locationID := ctx.Query("location_id")
|
||||
|
||||
times, err := services.PrayerTimesProvider.Get(ctx.Context(), locationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.JSON(times)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
Reference in New Issue
Block a user