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"
|
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"
|
2020-11-15 05:51:18 +13:00
|
|
|
"demodesk/neko/internal/utils"
|
2020-11-02 04:54:06 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2020-10-30 10:23:30 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:54:06 +13:00
|
|
|
type ApiManagerCtx struct {
|
|
|
|
sessions types.SessionManager
|
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
2020-10-30 10:23:30 +13:00
|
|
|
}
|
|
|
|
|
2020-10-31 06:16:21 +13:00
|
|
|
func New(
|
|
|
|
sessions types.SessionManager,
|
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-10-31 06:16:21 +13:00
|
|
|
sessions: sessions,
|
2020-11-02 04:54:06 +13:00
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
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) {
|
2020-11-15 05:51:18 +13:00
|
|
|
r.Use(api.Authenticate)
|
2020-10-30 10:23:30 +13:00
|
|
|
|
2020-11-22 11:42:34 +13:00
|
|
|
membersHandler := members.New(api.sessions)
|
|
|
|
r.Route("/members", membersHandler.Route)
|
2020-11-15 05:51:18 +13:00
|
|
|
|
|
|
|
roomHandler := room.New(api.sessions, api.desktop, api.capture)
|
2020-11-15 06:16:25 +13:00
|
|
|
r.Route("/room", roomHandler.Route)
|
2020-10-31 22:48:24 +13:00
|
|
|
|
2020-11-15 05:51:18 +13:00
|
|
|
r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
|
2020-11-17 07:37:52 +13:00
|
|
|
session := auth.GetSession(r)
|
2020-11-15 05:51:18 +13:00
|
|
|
utils.HttpBadRequest(w, "Hi `" + session.ID() + "`, you are authenticated.")
|
|
|
|
})
|
2020-10-31 22:48:24 +13:00
|
|
|
}
|
|
|
|
|
2020-11-15 05:51:18 +13:00
|
|
|
func (api *ApiManagerCtx) Authenticate(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session, err := api.sessions.Authenticate(r)
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpNotAuthenticated(w, err)
|
|
|
|
} else {
|
2020-11-17 07:37:52 +13:00
|
|
|
next.ServeHTTP(w, auth.SetSession(r, session))
|
2020-11-15 05:51:18 +13:00
|
|
|
}
|
|
|
|
})
|
2020-10-30 10:23:30 +13:00
|
|
|
}
|