neko/internal/api/router.go

82 lines
1.9 KiB
Go
Raw Normal View History

2020-10-30 10:23:30 +13:00
package api
import (
2021-09-17 10:58:50 +12:00
"context"
2021-08-30 03:23:49 +12:00
"errors"
2020-11-01 00:27:55 +13:00
"net/http"
"gitlab.com/demodesk/neko/server/internal/api/members"
"gitlab.com/demodesk/neko/server/internal/api/room"
2022-03-20 23:43:00 +13:00
"gitlab.com/demodesk/neko/server/pkg/auth"
"gitlab.com/demodesk/neko/server/pkg/types"
"gitlab.com/demodesk/neko/server/pkg/utils"
2020-10-30 10:23:30 +13:00
)
2020-11-02 04:54:06 +13:00
type ApiManagerCtx struct {
2020-12-13 05:07:45 +13:00
sessions types.SessionManager
2021-03-15 02:44:32 +13:00
members types.MemberManager
2020-12-13 05:07:45 +13:00
desktop types.DesktopManager
capture types.CaptureManager
2021-09-17 10:58:50 +12:00
routers map[string]func(types.Router)
2020-10-30 10:23:30 +13:00
}
2020-10-31 06:16:21 +13:00
func New(
sessions types.SessionManager,
2021-03-15 02:44:32 +13:00
members types.MemberManager,
2020-11-02 04:54:06 +13:00
desktop types.DesktopManager,
capture types.CaptureManager,
) *ApiManagerCtx {
2020-10-31 06:16:21 +13:00
2020-11-02 04:54:06 +13:00
return &ApiManagerCtx{
2020-12-13 05:07:45 +13:00
sessions: sessions,
2021-03-15 02:44:32 +13:00
members: members,
2020-12-13 05:07:45 +13:00
desktop: desktop,
capture: capture,
2021-09-17 10:58:50 +12:00
routers: make(map[string]func(types.Router)),
2020-10-31 06:16:21 +13:00
}
2020-10-30 10:23:30 +13:00
}
2021-09-17 10:58:50 +12:00
func (api *ApiManagerCtx) Route(r types.Router) {
2021-01-30 10:22:14 +13:00
r.Post("/login", api.Login)
2020-10-30 10:23:30 +13:00
2021-01-30 10:22:14 +13:00
// Authenticated area
2021-09-17 10:58:50 +12:00
r.Group(func(r types.Router) {
2021-01-30 10:22:14 +13:00
r.Use(api.Authenticate)
2021-01-30 10:22:14 +13:00
r.Post("/logout", api.Logout)
r.Get("/whoami", api.Whoami)
2020-10-31 22:48:24 +13:00
2021-03-15 02:44:32 +13:00
membersHandler := members.New(api.members)
2021-01-30 10:22:14 +13:00
r.Route("/members", membersHandler.Route)
2021-04-06 09:03:49 +12:00
r.Route("/members_bulk", membersHandler.RouteBulk)
2020-12-13 05:07:45 +13:00
2021-01-30 10:22:14 +13:00
roomHandler := room.New(api.sessions, api.desktop, api.capture)
r.Route("/room", roomHandler.Route)
for path, router := range api.routers {
r.Route(path, router)
}
})
2020-10-31 22:48:24 +13:00
}
2021-09-17 10:58:50 +12:00
func (api *ApiManagerCtx) Authenticate(w http.ResponseWriter, r *http.Request) (context.Context, error) {
session, err := api.sessions.Authenticate(r)
if err != nil {
if api.sessions.CookieEnabled() {
api.sessions.CookieClearToken(w, r)
}
2021-03-25 22:29:28 +13:00
2021-09-17 10:58:50 +12:00
if errors.Is(err, types.ErrSessionLoginDisabled) {
return nil, utils.HttpForbidden("login is disabled for this session")
}
return nil, utils.HttpUnauthorized().WithInternalErr(err)
}
return auth.SetSession(r, session), nil
2020-10-30 10:23:30 +13:00
}
2020-12-13 05:07:45 +13:00
2021-09-17 10:58:50 +12:00
func (api *ApiManagerCtx) AddRouter(path string, router func(types.Router)) {
2020-12-13 05:07:45 +13:00
api.routers[path] = router
}