updates
This commit is contained in:
		
							parent
							
								
									61c3e65552
								
							
						
					
					
						commit
						fc1dc109d5
					
				
							
								
								
									
										9
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					FROM python:3.9-slim-buster
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WORKDIR /app
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					COPY script.py ./
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					RUN pip install flask pyyaml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CMD ["python", "script.py"]
 | 
				
			||||||
							
								
								
									
										12
									
								
								docker-compose.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								docker-compose.yaml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					services:
 | 
				
			||||||
 | 
					  api:
 | 
				
			||||||
 | 
					    build:
 | 
				
			||||||
 | 
					      context: .
 | 
				
			||||||
 | 
					      dockerfile: Dockerfile
 | 
				
			||||||
 | 
					    environment:
 | 
				
			||||||
 | 
					      - PORT=5000
 | 
				
			||||||
 | 
					      - CONFIG_FILE=/app/config.yml
 | 
				
			||||||
 | 
					    ports:
 | 
				
			||||||
 | 
					      - "5003:5000"
 | 
				
			||||||
 | 
					    volumes:
 | 
				
			||||||
 | 
					      - ./config.yml:/app/config.yml
 | 
				
			||||||
							
								
								
									
										92
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										92
									
								
								main.go
									
									
									
									
									
								
							@ -7,14 +7,16 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewClient(url string) *Client {
 | 
					type Client struct {
 | 
				
			||||||
	return &Client{
 | 
						url    string
 | 
				
			||||||
		url: url,
 | 
						client *http.Client
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Client struct {
 | 
					func NewClient(url string) *Client {
 | 
				
			||||||
	url string
 | 
						return &Client{
 | 
				
			||||||
 | 
							url:    url,
 | 
				
			||||||
 | 
							client: &http.Client{}, //optional, use for custom timeouts etc
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Route struct {
 | 
					type Route struct {
 | 
				
			||||||
@ -22,55 +24,49 @@ type Route struct {
 | 
				
			|||||||
	Backend string   `json:"backend"`
 | 
						Backend string   `json:"backend"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Client) listRoutes() ([]Route, error) {
 | 
					type apiResponse struct {
 | 
				
			||||||
	body := bytes.NewBufferString(`{"action": "ListRoutes"}`)
 | 
						Routes  []Route `json:"routes"`
 | 
				
			||||||
	resp, err := http.Post(c.url, "application/json", body)
 | 
						Message string  `json:"message"`
 | 
				
			||||||
 | 
						Error   string  `json:"error"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) doRequest(action string, payload interface{}) (*apiResponse, error) {
 | 
				
			||||||
 | 
						data, err := json.Marshal(map[string]interface{}{
 | 
				
			||||||
 | 
							"action": action,
 | 
				
			||||||
 | 
							"routes": payload,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, fmt.Errorf("failed marshalling payload: %w", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resp, err := c.client.Post(c.url, "application/json", bytes.NewBuffer(data))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("failed to send request: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer resp.Body.Close()
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var response struct {
 | 
						var apiResp apiResponse
 | 
				
			||||||
		Routes []Route `json:"routes"`
 | 
						if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
 | 
				
			||||||
		Error  string  `json:"error"`
 | 
							return nil, fmt.Errorf("failed to decode response: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
 | 
					
 | 
				
			||||||
 | 
						if apiResp.Error != "" {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("API error: %s", apiResp.Error)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &apiResp, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) ListRoutes() ([]Route, error) {
 | 
				
			||||||
 | 
						resp, err := c.doRequest("ListRoutes", nil)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if response.Error != "" {
 | 
						return resp.Routes, nil
 | 
				
			||||||
		return nil, fmt.Errorf("error listing routes: %s", response.Error)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return response.Routes, nil
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Client) updateRoutes(routes []Route) error {
 | 
					func (c *Client) UpdateRoutes(routes []Route) error {
 | 
				
			||||||
	payload := struct {
 | 
						_, err := c.doRequest("UpdateRoutes", routes)
 | 
				
			||||||
		Action string  `json:"action"`
 | 
						return err
 | 
				
			||||||
		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
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										26
									
								
								script.py
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								script.py
									
									
									
									
									
								
							@ -1,10 +1,15 @@
 | 
				
			|||||||
 | 
					import os
 | 
				
			||||||
import yaml
 | 
					import yaml
 | 
				
			||||||
from flask import Flask, request, jsonify
 | 
					from flask import Flask, request, jsonify
 | 
				
			||||||
 | 
					from werkzeug.exceptions import HTTPException
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app = Flask(__name__)
 | 
					app = Flask(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CONFIG_FILE = "config.yml"
 | 
					CONFIG_FILE = "config.yml"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Load config from environment variable or default to config.yml
 | 
				
			||||||
 | 
					CONFIG_FILE = os.environ.get('CONFIG_FILE', 'config.yml')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def load_config():
 | 
					def load_config():
 | 
				
			||||||
    with open(CONFIG_FILE, "r") as f:
 | 
					    with open(CONFIG_FILE, "r") as f:
 | 
				
			||||||
@ -29,21 +34,32 @@ def api_endpoint():
 | 
				
			|||||||
        elif action == "UpdateRoutes":
 | 
					        elif action == "UpdateRoutes":
 | 
				
			||||||
            new_routes = data.get('routes')
 | 
					            new_routes = data.get('routes')
 | 
				
			||||||
            if not isinstance(new_routes, list):
 | 
					            if not isinstance(new_routes, list):
 | 
				
			||||||
                return jsonify({"error": "Invalid routes format"}), 400
 | 
					                # More specific exception
 | 
				
			||||||
 | 
					                raise ValueError("Invalid routes format")
 | 
				
			||||||
            config['config']['lite']['routes'] = new_routes
 | 
					            config['config']['lite']['routes'] = new_routes
 | 
				
			||||||
            save_config(config)
 | 
					            save_config(config)
 | 
				
			||||||
            return jsonify({"message": "Routes updated successfully"})
 | 
					            return jsonify({"message": "Routes updated successfully"})
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return jsonify({"error": "Invalid action"}), 400
 | 
					            raise ValueError("Invalid action")  # More specific exception
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    except FileNotFoundError:
 | 
					    except FileNotFoundError:
 | 
				
			||||||
        return jsonify({"error": "Config file not found"}), 500
 | 
					        return jsonify({"error": "Config file not found"}), 500
 | 
				
			||||||
    except yaml.YAMLError as e:
 | 
					    except yaml.YAMLError as e:
 | 
				
			||||||
        return jsonify({"error": f"Error parsing YAML: {e}"}), 500
 | 
					        return jsonify({"error": f"Error parsing YAML: {e}"}), 500
 | 
				
			||||||
 | 
					    except ValueError as e:
 | 
				
			||||||
 | 
					        # Return 400 for client-side errors
 | 
				
			||||||
 | 
					        return jsonify({"error": str(e)}), 400
 | 
				
			||||||
 | 
					    except HTTPException as e:
 | 
				
			||||||
 | 
					        # Handle HTTP exceptions gracefully
 | 
				
			||||||
 | 
					        return jsonify({"error": str(e)}), e.code
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        return jsonify({"error": f"An error occurred: {e}"}), 500
 | 
					        # Log unexpected error for debugging
 | 
				
			||||||
 | 
					        return jsonify({"error": f"An unexpected error occurred: {e}"}), 500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # This is to ensure you handle all scenarios
 | 
				
			||||||
 | 
					    finally:
 | 
				
			||||||
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    app.run(debug=True, host='0.0.0.0', port=5000)
 | 
					    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user