2020-10-30 10:23:30 +13:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-15 05:51:18 +13:00
|
|
|
"context"
|
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-10-31 06:16:21 +13:00
|
|
|
"demodesk/neko/internal/api/member"
|
|
|
|
"demodesk/neko/internal/api/room"
|
|
|
|
"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-11-15 05:51:18 +13:00
|
|
|
const (
|
|
|
|
keySessionCtx int = iota
|
|
|
|
)
|
2020-10-31 22:48:24 +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-15 05:51:18 +13:00
|
|
|
memberHandler := member.New(api.sessions)
|
2020-11-15 06:16:25 +13:00
|
|
|
r.Route("/member", memberHandler.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-15 06:22:15 +13:00
|
|
|
session := 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 {
|
|
|
|
ctx := context.WithValue(r.Context(), keySessionCtx, session)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
})
|
2020-10-30 10:23:30 +13:00
|
|
|
}
|
2020-11-15 06:22:15 +13:00
|
|
|
|
|
|
|
func GetSession(r *http.Request) types.Session {
|
|
|
|
return r.Context().Value(keySessionCtx).(types.Session)
|
|
|
|
}
|