neko/internal/api/router.go

89 lines
2.0 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"
2020-11-22 11:42:34 +13:00
"demodesk/neko/internal/api/members"
2020-10-31 06:16:21 +13:00
"demodesk/neko/internal/api/room"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/config"
2020-11-17 07:37:52 +13:00
"demodesk/neko/internal/http/auth"
2020-10-31 06:16:21 +13:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/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,
2020-10-31 22:48:24 +13:00
conf *config.Server,
2020-11-02 04:54:06 +13:00
) *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)
}
})
2021-03-20 03:04:01 +13:00
2021-09-17 10:58:50 +12:00
r.Get("/health", func(w http.ResponseWriter, r *http.Request) error {
_, err := w.Write([]byte("true"))
return err
2021-03-20 03:04:01 +13:00
})
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
}