1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package api
import (
"errors"
"log/slog"
"time"
"github.com/LMBishop/confplanner/api/dto"
"github.com/LMBishop/confplanner/api/handlers"
"github.com/LMBishop/confplanner/api/middleware"
"github.com/LMBishop/confplanner/pkg/calendar"
"github.com/LMBishop/confplanner/pkg/favourites"
"github.com/LMBishop/confplanner/pkg/ical"
"github.com/LMBishop/confplanner/pkg/schedule"
"github.com/LMBishop/confplanner/pkg/user"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
)
type ApiServices struct {
UserService user.Service
FavouritesService favourites.Service
ScheduleService schedule.Service
CalendarService calendar.Service
IcalService ical.Service
}
func NewServer(apiServices ApiServices, baseURL string) *fiber.App {
sessionStore := session.New(session.Config{
Expiration: 24 * time.Hour,
KeyLookup: "cookie:confplanner_session",
CookieSameSite: "None",
})
app := fiber.New(fiber.Config{
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
var ok *dto.OkResponse
if errors.As(err, &ok) {
return ctx.Status(ok.Code).JSON(ok)
}
var e *dto.ErrorResponse
if errors.As(err, &e) {
return ctx.Status(e.Code).JSON(e)
}
slog.Error("fiber runtime error", "error", err, "URL", ctx.OriginalURL())
return ctx.Status(500).JSON(dto.ErrorResponse{
Code: 500,
Message: "Internal Server Error",
})
},
AppName: "confplanner",
})
// app.Use(cors.New())
requireAuthenticated := middleware.RequireAuthenticated(apiServices.UserService, sessionStore)
app.Post("/register", handlers.Register(apiServices.UserService))
app.Post("/login", handlers.Login(apiServices.UserService, sessionStore))
app.Post("/logout", requireAuthenticated, handlers.Logout(sessionStore))
app.Get("/favourites", requireAuthenticated, handlers.GetFavourites(apiServices.FavouritesService))
app.Post("/favourites", requireAuthenticated, handlers.CreateFavourite(apiServices.FavouritesService))
app.Delete("/favourites", requireAuthenticated, handlers.DeleteFavourite(apiServices.FavouritesService))
app.Get("/schedule", requireAuthenticated, handlers.GetSchedule(apiServices.ScheduleService))
app.Get("/calendar", requireAuthenticated, handlers.GetCalendar(apiServices.CalendarService, baseURL))
app.Post("/calendar", requireAuthenticated, handlers.CreateCalendar(apiServices.CalendarService, baseURL))
app.Delete("/calendar", requireAuthenticated, handlers.DeleteCalendar(apiServices.CalendarService))
app.Use("/calendar/ical", handlers.GetIcal(apiServices.IcalService, apiServices.CalendarService))
return app
}
|