init
This commit is contained in:
commit
61c3e65552
76
main.go
Normal file
76
main.go
Normal file
@ -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
|
||||
}
|
49
script.py
Normal file
49
script.py
Normal file
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user