mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
api split scopes.
This commit is contained in:
@@ -17,14 +17,32 @@ type ApiManagerCtx struct {
|
||||
|
||||
func New(roomManager types.RoomManager, conf *config.API) *ApiManagerCtx {
|
||||
return &ApiManagerCtx{
|
||||
logger: log.With().Str("module", "router").Logger(),
|
||||
logger: log.With().Str("module", "api").Logger(),
|
||||
rooms: roomManager,
|
||||
conf: conf,
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) Mount(r chi.Router) {
|
||||
r.Get("/config/rooms", manager.roomsConfig)
|
||||
//
|
||||
// config
|
||||
//
|
||||
|
||||
r.Get("/config/rooms", manager.configRooms)
|
||||
|
||||
//
|
||||
// pull
|
||||
//
|
||||
|
||||
r.Route("/pull", func(r chi.Router) {
|
||||
r.Get("/", manager.pullStatus)
|
||||
r.Post("/", manager.pullStart)
|
||||
r.Delete("/", manager.pullStop)
|
||||
})
|
||||
|
||||
//
|
||||
// rooms
|
||||
//
|
||||
|
||||
r.Get("/rooms", manager.roomsList)
|
||||
r.Post("/rooms", manager.roomCreate)
|
||||
@@ -41,10 +59,4 @@ func (manager *ApiManagerCtx) Mount(r chi.Router) {
|
||||
r.Post("/restart", manager.roomGenericAction(manager.rooms.Restart))
|
||||
r.Post("/recreate", manager.roomRecreate)
|
||||
})
|
||||
|
||||
r.Route("/pull", func(r chi.Router) {
|
||||
r.Get("/", manager.roomPullStatus)
|
||||
r.Post("/", manager.roomPullStart)
|
||||
r.Delete("/", manager.roomPullStop)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (manager *ApiManagerCtx) configRooms(w http.ResponseWriter, r *http.Request) {
|
||||
response := manager.rooms.Config()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"m1k1o/neko_rooms/internal/types"
|
||||
)
|
||||
|
||||
func (manager *ApiManagerCtx) pullStart(w http.ResponseWriter, r *http.Request) {
|
||||
request := types.PullStart{}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
err := manager.rooms.PullStart(request)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
response := manager.rooms.PullStatus()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) pullStatus(w http.ResponseWriter, r *http.Request) {
|
||||
response := manager.rooms.PullStatus()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) pullStop(w http.ResponseWriter, r *http.Request) {
|
||||
err := manager.rooms.PullStop()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -9,13 +9,6 @@ import (
|
||||
"m1k1o/neko_rooms/internal/types"
|
||||
)
|
||||
|
||||
func (manager *ApiManagerCtx) roomsConfig(w http.ResponseWriter, r *http.Request) {
|
||||
response := manager.rooms.Config()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) roomsList(w http.ResponseWriter, r *http.Request) {
|
||||
response, err := manager.rooms.List()
|
||||
if err != nil {
|
||||
@@ -157,38 +150,3 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(id string) error) fu
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) roomPullStart(w http.ResponseWriter, r *http.Request) {
|
||||
request := types.PullStart{}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
err := manager.rooms.PullStart(request)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
response := manager.rooms.PullStatus()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) roomPullStatus(w http.ResponseWriter, r *http.Request) {
|
||||
response := manager.rooms.PullStatus()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) roomPullStop(w http.ResponseWriter, r *http.Request) {
|
||||
err := manager.rooms.PullStop()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user