From b2af871c9a7f79571861a9eb397ba8d1ea08cb5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 15 Jan 2022 14:39:16 +0100 Subject: [PATCH] api split scopes. --- internal/api/{manager.go => api.go} | 28 +++++++++++++------ internal/api/config.go | 13 +++++++++ internal/api/pull.go | 43 +++++++++++++++++++++++++++++ internal/api/rooms.go | 42 ---------------------------- 4 files changed, 76 insertions(+), 50 deletions(-) rename internal/api/{manager.go => api.go} (80%) create mode 100644 internal/api/config.go create mode 100644 internal/api/pull.go diff --git a/internal/api/manager.go b/internal/api/api.go similarity index 80% rename from internal/api/manager.go rename to internal/api/api.go index d279e08..783cde1 100644 --- a/internal/api/manager.go +++ b/internal/api/api.go @@ -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) - }) } diff --git a/internal/api/config.go b/internal/api/config.go new file mode 100644 index 0000000..88486e5 --- /dev/null +++ b/internal/api/config.go @@ -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) +} diff --git a/internal/api/pull.go b/internal/api/pull.go new file mode 100644 index 0000000..800dcab --- /dev/null +++ b/internal/api/pull.go @@ -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) +} diff --git a/internal/api/rooms.go b/internal/api/rooms.go index 9652a13..b5f216c 100644 --- a/internal/api/rooms.go +++ b/internal/api/rooms.go @@ -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) -}