neko/internal/api/router.go

58 lines
1.3 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-10-31 22:48:24 +13:00
"demodesk/neko/internal/types/config"
2020-11-01 00:27:55 +13:00
"demodesk/neko/internal/api/utils"
2020-10-30 10:23:30 +13:00
)
2020-10-31 06:16:21 +13:00
type API struct {
sessions types.SessionManager
remote types.RemoteManager
broadcast types.BroadcastManager
websocket types.WebSocketHandler
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,
remote types.RemoteManager,
broadcast types.BroadcastManager,
websocket types.WebSocketHandler,
2020-10-31 22:48:24 +13:00
conf *config.Server,
2020-10-31 06:16:21 +13:00
) *API {
2020-11-01 00:27:55 +13:00
AdminToken = []byte(conf.AdminToken)
UserToken = []byte(conf.UserToken)
2020-10-31 06:16:21 +13:00
return &API{
sessions: sessions,
remote: remote,
broadcast: broadcast,
websocket: websocket,
}
2020-10-30 10:23:30 +13:00
}
2020-10-31 22:48:24 +13:00
func (a *API) Mount(r *chi.Mux) {
2020-10-31 06:16:21 +13:00
memberHandler := member.New(a.sessions, a.websocket)
2020-10-31 22:48:24 +13:00
r.Mount("/member", memberHandler.Router(UsersOnly, AdminsOnly))
2020-10-30 10:23:30 +13:00
2020-10-31 06:16:21 +13:00
roomHandler := room.New(a.sessions, a.remote, a.broadcast, a.websocket)
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
}