neko/internal/api/router.go

55 lines
1.2 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-10-31 06:16:21 +13:00
"demodesk/neko/internal/api/member"
"demodesk/neko/internal/api/room"
"demodesk/neko/internal/types"
2020-11-02 04:54:06 +13:00
"demodesk/neko/internal/config"
2020-11-01 00:27:55 +13:00
"demodesk/neko/internal/api/utils"
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-01 00:27:55 +13:00
var AdminToken []byte
var UserToken []byte
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-11-01 00:27:55 +13:00
AdminToken = []byte(conf.AdminToken)
UserToken = []byte(conf.UserToken)
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-02 04:54:06 +13:00
func (a *ApiManagerCtx) Mount(r *chi.Mux) {
memberHandler := member.New(a.sessions)
2020-10-31 22:48:24 +13:00
r.Mount("/member", memberHandler.Router(UsersOnly, AdminsOnly))
2020-10-30 10:23:30 +13:00
2020-11-02 04:54:06 +13:00
roomHandler := room.New(a.sessions, a.desktop, a.capture)
2020-10-31 22:48:24 +13:00
r.Mount("/room", roomHandler.Router(UsersOnly, AdminsOnly))
}
2020-11-01 00:27:55 +13:00
func UsersOnly(next http.Handler) http.Handler {
return utils.AuthMiddleware(next, UserToken, AdminToken)
2020-10-31 22:48:24 +13:00
}
2020-11-01 00:27:55 +13:00
func AdminsOnly(next http.Handler) http.Handler {
return utils.AuthMiddleware(next, AdminToken)
2020-10-30 10:23:30 +13:00
}