commit 61c3e6555232ae524ae63b7a8c7f1560f2e8bef0 Author: tijl Date: Sun Dec 15 00:30:04 2024 +0100 init diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..623b6a9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.tijl.dev/tijl/gate-editor-tool + +go 1.23.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..ca74269 --- /dev/null +++ b/main.go @@ -0,0 +1,76 @@ +package gateeditor + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +func NewClient(url string) *Client { + return &Client{ + url: url, + } +} + +type Client struct { + url string +} + +type Route struct { + Host []string `json:"host"` + Backend string `json:"backend"` +} + +func (c *Client) listRoutes() ([]Route, error) { + body := bytes.NewBufferString(`{"action": "ListRoutes"}`) + resp, err := http.Post(c.url, "application/json", body) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var response struct { + Routes []Route `json:"routes"` + Error string `json:"error"` + } + if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf("error listing routes: %s", response.Error) + } + return response.Routes, nil + +} + +func (c *Client) updateRoutes(routes []Route) error { + payload := struct { + Action string `json:"action"` + Routes []Route `json:"routes"` + }{ + Action: "UpdateRoutes", + Routes: routes, + } + payloadBytes, err := json.Marshal(payload) + if err != nil { + return err + } + resp, err := http.Post(c.url, "application/json", bytes.NewBuffer(payloadBytes)) + if err != nil { + return err + } + defer resp.Body.Close() + + var response struct { + Message string `json:"message"` + Error string `json:"error"` + } + if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { + return err + } + if response.Error != "" { + return fmt.Errorf("error updating routes: %s", response.Error) + } + return nil +} diff --git a/script.py b/script.py new file mode 100644 index 0000000..13a9170 --- /dev/null +++ b/script.py @@ -0,0 +1,49 @@ +import yaml +from flask import Flask, request, jsonify + +app = Flask(__name__) + +CONFIG_FILE = "config.yml" + + +def load_config(): + with open(CONFIG_FILE, "r") as f: + return yaml.safe_load(f) + + +def save_config(config): + with open(CONFIG_FILE, "w") as f: + yaml.dump(config, f, default_flow_style=False) + + +@app.route('/', methods=['POST']) +def api_endpoint(): + try: + data = request.get_json() + action = data.get('action') + + config = load_config() + + if action == "ListRoutes": + return jsonify({"routes": config['config']['lite']['routes']}) + elif action == "UpdateRoutes": + new_routes = data.get('routes') + if not isinstance(new_routes, list): + return jsonify({"error": "Invalid routes format"}), 400 + config['config']['lite']['routes'] = new_routes + save_config(config) + return jsonify({"message": "Routes updated successfully"}) + else: + return jsonify({"error": "Invalid action"}), 400 + + except FileNotFoundError: + return jsonify({"error": "Config file not found"}), 500 + except yaml.YAMLError as e: + return jsonify({"error": f"Error parsing YAML: {e}"}), 500 + except Exception as e: + return jsonify({"error": f"An error occurred: {e}"}), 500 + + +if __name__ == "__main__": + app.run(debug=True, host='0.0.0.0', port=5000) +