aboutsummaryrefslogtreecommitdiffstats
path: root/api/router.go
blob: 423529feb3a51cd43eaac87744d73630c7dca1b5 (plain)
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
package api

import (
	"net/http"

	"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/session"
	"github.com/LMBishop/confplanner/pkg/user"
)

type ApiServices struct {
	UserService       user.Service
	FavouritesService favourites.Service
	ScheduleService   schedule.Service
	CalendarService   calendar.Service
	IcalService       ical.Service
	SessionService    session.Service
}

func NewServer(apiServices ApiServices, baseURL string) *http.ServeMux {
	mustAuthenticate := middleware.MustAuthenticate(apiServices.UserService, apiServices.SessionService)

	mux := http.NewServeMux()

	mux.HandleFunc("POST /register", handlers.Register(apiServices.UserService))
	mux.HandleFunc("POST /login", handlers.Login(apiServices.UserService, apiServices.SessionService))
	mux.HandleFunc("POST /logout", mustAuthenticate(handlers.Register(apiServices.UserService)))

	mux.HandleFunc("GET /favourites", mustAuthenticate(handlers.GetFavourites(apiServices.FavouritesService)))
	mux.HandleFunc("POST /favourites", mustAuthenticate(handlers.CreateFavourite(apiServices.FavouritesService)))
	mux.HandleFunc("DELETE /favourites", mustAuthenticate(handlers.DeleteFavourite(apiServices.FavouritesService)))

	mux.HandleFunc("GET /schedule", mustAuthenticate(handlers.GetSchedule(apiServices.ScheduleService)))

	mux.HandleFunc("GET /calendar", mustAuthenticate(handlers.GetCalendar(apiServices.CalendarService, baseURL)))
	mux.HandleFunc("POST /calendar", mustAuthenticate(handlers.CreateCalendar(apiServices.CalendarService, baseURL)))
	mux.HandleFunc("DELETE /calendar", mustAuthenticate(handlers.DeleteCalendar(apiServices.CalendarService)))
	mux.HandleFunc("/calendar/ical", handlers.GetIcal(apiServices.IcalService, apiServices.CalendarService))

	return mux
}