2023-04-28 18:28:01 +00:00
|
|
|
package configapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2023-07-10 12:41:30 +00:00
|
|
|
"strings"
|
2023-04-28 18:28:01 +00:00
|
|
|
|
|
|
|
"github.com/azukaar/cosmos-server/src/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UpdateRouteRequest struct {
|
|
|
|
RouteName string `json:"routeName"`
|
|
|
|
Operation string `json:"operation"`
|
|
|
|
NewRoute *utils.ProxyRouteConfig `json:"newRoute,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigApiPatch(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if utils.AdminOnly(w, req) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Log("RouteSettingsUpdate: Patching config")
|
|
|
|
|
2023-05-13 17:38:39 +00:00
|
|
|
utils.ConfigLock.Lock()
|
|
|
|
defer utils.ConfigLock.Unlock()
|
2023-04-28 18:28:01 +00:00
|
|
|
|
|
|
|
var updateReq UpdateRouteRequest
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&updateReq)
|
|
|
|
if err != nil {
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Error("RouteSettingsUpdate: Invalid Update Request", err)
|
2023-04-28 18:28:01 +00:00
|
|
|
utils.HTTPError(w, "Invalid Update Request", http.StatusBadRequest, "UR001")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := utils.ReadConfigFromFile()
|
|
|
|
routes := config.HTTPConfig.ProxyConfig.Routes
|
|
|
|
routeIndex := -1
|
|
|
|
|
2023-04-29 11:11:03 +00:00
|
|
|
if updateReq.Operation != "add" {
|
|
|
|
if updateReq.RouteName == "" {
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Error("RouteSettingsUpdate: RouteName must be provided", nil)
|
2023-04-29 11:11:03 +00:00
|
|
|
utils.HTTPError(w, "RouteName must be provided", http.StatusBadRequest, "UR002")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, route := range routes {
|
|
|
|
if route.Name == updateReq.RouteName {
|
|
|
|
routeIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if routeIndex == -1 {
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Error("RouteSettingsUpdate: Route not found: "+updateReq.RouteName, nil)
|
2023-04-29 11:11:03 +00:00
|
|
|
utils.HTTPError(w, "Route not found", http.StatusNotFound, "UR002")
|
|
|
|
return
|
2023-04-28 18:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch updateReq.Operation {
|
2023-04-29 11:11:03 +00:00
|
|
|
case "replace":
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Log("RouteSettingsUpdate: Replacing route: "+updateReq.RouteName)
|
2023-04-29 11:11:03 +00:00
|
|
|
if updateReq.NewRoute == nil {
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Error("RouteSettingsUpdate: NewRoute must be provided for replace operation", nil)
|
2023-04-29 11:11:03 +00:00
|
|
|
utils.HTTPError(w, "NewRoute must be provided for replace operation", http.StatusBadRequest, "UR003")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
routes[routeIndex] = *updateReq.NewRoute
|
|
|
|
case "move_up":
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Log("RouteSettingsUpdate: Moving up route: "+updateReq.RouteName)
|
2023-04-29 11:11:03 +00:00
|
|
|
if routeIndex > 0 {
|
|
|
|
routes[routeIndex-1], routes[routeIndex] = routes[routeIndex], routes[routeIndex-1]
|
|
|
|
}
|
|
|
|
case "move_down":
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Log("RouteSettingsUpdate: Moving down route: "+updateReq.RouteName)
|
2023-04-29 11:11:03 +00:00
|
|
|
if routeIndex < len(routes)-1 {
|
|
|
|
routes[routeIndex+1], routes[routeIndex] = routes[routeIndex], routes[routeIndex+1]
|
|
|
|
}
|
|
|
|
case "delete":
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Log("RouteSettingsUpdate: Deleting route: "+updateReq.RouteName)
|
2023-04-29 11:11:03 +00:00
|
|
|
routes = append(routes[:routeIndex], routes[routeIndex+1:]...)
|
|
|
|
case "add":
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Log("RouteSettingsUpdate: Adding route")
|
2023-04-29 11:11:03 +00:00
|
|
|
if updateReq.NewRoute == nil {
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Error("RouteSettingsUpdate: NewRoute must be provided for add operation", nil)
|
2023-04-29 11:11:03 +00:00
|
|
|
utils.HTTPError(w, "NewRoute must be provided for add operation", http.StatusBadRequest, "UR003")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
routes = append([]utils.ProxyRouteConfig{*updateReq.NewRoute}, routes...)
|
|
|
|
default:
|
2023-07-10 12:41:30 +00:00
|
|
|
utils.Error("RouteSettingsUpdate: Unsupported operation: "+updateReq.Operation, nil)
|
2023-04-29 11:11:03 +00:00
|
|
|
utils.HTTPError(w, "Unsupported operation", http.StatusBadRequest, "UR004")
|
2023-04-28 18:28:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config.HTTPConfig.ProxyConfig.Routes = routes
|
2023-07-02 13:53:14 +00:00
|
|
|
utils.SetBaseMainConfig(config)
|
2023-07-01 15:32:21 +00:00
|
|
|
|
|
|
|
utils.RestartHTTPServer()
|
2023-07-10 12:41:30 +00:00
|
|
|
|
|
|
|
if updateReq.NewRoute.Mode == "SERVAPP" {
|
|
|
|
utils.Log("RouteSettingsUpdate: Service needs update: "+updateReq.NewRoute.Target)
|
|
|
|
|
|
|
|
target := updateReq.NewRoute.Target
|
|
|
|
tokens := strings.Split(target, ":")
|
|
|
|
name := tokens[1][2:]
|
|
|
|
|
|
|
|
utils.Log("RouteSettingsUpdate: Service needs update: "+name)
|
|
|
|
|
|
|
|
utils.ReBootstrapContainer(name)
|
|
|
|
}
|
2023-04-28 18:28:01 +00:00
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
"status": "OK",
|
|
|
|
})
|
|
|
|
}
|