neko/internal/api/router.go

84 lines
1.8 KiB
Go
Raw Normal View History

2020-10-30 10:23:30 +13:00
package api
import (
2020-11-01 00:27:55 +13:00
"net/http"
2020-10-30 10:23:30 +13:00
"github.com/go-chi/chi"
2020-11-01 00:27:55 +13:00
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
routers map[string]func(chi.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,
routers: make(map[string]func(chi.Router)),
2020-10-31 06:16:21 +13:00
}
2020-10-30 10:23:30 +13:00
}
2020-11-15 06:16:25 +13:00
func (api *ApiManagerCtx) Route(r chi.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
r.Group(func(r chi.Router) {
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)
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
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
//nolint
w.Write([]byte("true"))
})
2020-10-31 22:48:24 +13:00
}
func (api *ApiManagerCtx) Authenticate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-03-14 08:42:56 +13:00
session, err := api.sessions.Authenticate(r)
if err != nil {
2021-03-25 22:29:28 +13:00
api.sessions.CookieClearToken(w, r)
2020-11-29 03:00:21 +13:00
utils.HttpUnauthorized(w, err)
2021-03-25 22:29:28 +13:00
return
}
2021-03-25 22:29:28 +13:00
next.ServeHTTP(w, auth.SetSession(r, session))
})
2020-10-30 10:23:30 +13:00
}
2020-12-13 05:07:45 +13:00
func (api *ApiManagerCtx) AddRouter(path string, router func(chi.Router)) {
api.routers[path] = router
}