neko/internal/api/router.go

88 lines
1.9 KiB
Go
Raw Normal View History

2020-10-29 22:23:30 +01:00
package api
import (
2020-10-31 12:27:55 +01:00
"net/http"
2020-10-29 22:23:30 +01:00
"github.com/go-chi/chi"
2020-10-31 12:27:55 +01:00
2020-11-21 23:42:34 +01:00
"demodesk/neko/internal/api/members"
2020-10-30 18:16:21 +01:00
"demodesk/neko/internal/api/room"
2021-02-14 14:40:17 +01:00
"demodesk/neko/internal/config"
2020-11-16 19:37:52 +01:00
"demodesk/neko/internal/http/auth"
2020-10-30 18:16:21 +01:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
2020-10-29 22:23:30 +01:00
)
2020-11-01 16:54:06 +01:00
type ApiManagerCtx struct {
2020-12-12 17:07:45 +01:00
sessions types.SessionManager
2021-03-14 14:44:32 +01:00
members types.MemberManager
2020-12-12 17:07:45 +01:00
desktop types.DesktopManager
capture types.CaptureManager
routers map[string]func(chi.Router)
2020-10-29 22:23:30 +01:00
}
2020-10-30 18:16:21 +01:00
func New(
sessions types.SessionManager,
2021-03-14 14:44:32 +01:00
members types.MemberManager,
2020-11-01 16:54:06 +01:00
desktop types.DesktopManager,
capture types.CaptureManager,
2020-10-31 10:48:24 +01:00
conf *config.Server,
2020-11-01 16:54:06 +01:00
) *ApiManagerCtx {
2020-10-30 18:16:21 +01:00
2020-11-01 16:54:06 +01:00
return &ApiManagerCtx{
2020-12-12 17:07:45 +01:00
sessions: sessions,
2021-03-14 14:44:32 +01:00
members: members,
2020-12-12 17:07:45 +01:00
desktop: desktop,
capture: capture,
routers: make(map[string]func(chi.Router)),
2020-10-30 18:16:21 +01:00
}
2020-10-29 22:23:30 +01:00
}
2020-11-14 18:16:25 +01:00
func (api *ApiManagerCtx) Route(r chi.Router) {
2021-01-29 22:22:14 +01:00
r.Post("/login", api.Login)
2020-10-29 22:23:30 +01:00
2021-01-29 22:22:14 +01:00
// Authenticated area
r.Group(func(r chi.Router) {
r.Use(api.Authenticate)
2021-01-29 22:22:14 +01:00
r.Post("/logout", api.Logout)
r.Get("/whoami", api.Whoami)
2020-10-31 10:48:24 +01:00
2021-03-14 14:44:32 +01:00
membersHandler := members.New(api.members)
2021-01-29 22:22:14 +01:00
r.Route("/members", membersHandler.Route)
2021-04-05 23:03:49 +02:00
r.Route("/members_bulk", membersHandler.RouteBulk)
2020-12-12 17:07:45 +01:00
2021-01-29 22:22:14 +01: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-19 15:04:01 +01:00
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
//nolint
w.Write([]byte("true"))
})
2020-10-31 10:48:24 +01:00
}
func (api *ApiManagerCtx) Authenticate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-03-13 20:42:56 +01:00
session, err := api.sessions.Authenticate(r)
if err != nil {
2021-04-24 20:53:37 +02:00
if api.sessions.CookieEnabled() {
api.sessions.CookieClearToken(w, r)
}
2020-11-28 15:00:21 +01:00
utils.HttpUnauthorized(w, err)
2021-03-25 10:29:28 +01:00
return
}
2021-03-25 10:29:28 +01:00
next.ServeHTTP(w, auth.SetSession(r, session))
})
2020-10-29 22:23:30 +01:00
}
2020-12-12 17:07:45 +01:00
func (api *ApiManagerCtx) AddRouter(path string, router func(chi.Router)) {
api.routers[path] = router
}