Initial code
This commit is contained in:
parent
f628266c2a
commit
3519df4299
5 changed files with 114 additions and 0 deletions
20
Dockerfile
Normal file
20
Dockerfile
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Then build the actual amelie docker image
|
||||
FROM python:latest
|
||||
|
||||
# Set /usr/src/app as startup working directory
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Install python requirements
|
||||
COPY requirements.txt ./
|
||||
RUN pip install --no-cache-dir --upgrade pip
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Copy sources
|
||||
COPY . .
|
||||
|
||||
# Expose the web port
|
||||
EXPOSE 8000
|
||||
|
||||
# Start the website
|
||||
CMD ["/usr/src/app/start.sh"]
|
||||
|
||||
25
http_test_server.py
Normal file
25
http_test_server.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
import os
|
||||
|
||||
PORT = int(os.getenv("PORT", "8000"))
|
||||
|
||||
class handler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type','text/html')
|
||||
self.end_headers()
|
||||
|
||||
message = "Hello, World! Here is a GET response"
|
||||
self.wfile.write(bytes(message, "utf8"))
|
||||
def do_POST(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type','application/json')
|
||||
self.end_headers()
|
||||
|
||||
message = "{\"hello\": \"response\"}"
|
||||
self.wfile.write(bytes(message, "utf8"))
|
||||
|
||||
with HTTPServer(('', PORT), handler) as server:
|
||||
server.serve_forever()
|
||||
|
||||
64
multiplexer.py
Normal file
64
multiplexer.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from flask import Flask, request, jsonify
|
||||
|
||||
import requests
|
||||
import os
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
REITTI_BASE_URL = os.getenv("REITTI_BASE_URL", None)
|
||||
REITTI_API_KEY = os.getenv("REITTI_API_KEY", None)
|
||||
|
||||
DAWARICH_BASE_URL = os.getenv("DAWARICH_BASE_URL", None)
|
||||
DAWARICH_API_KEY = os.getenv("DAWARICH_API_KEY", None)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return "OK"
|
||||
|
||||
@app.route('/ingest', methods=['POST'])
|
||||
def ingest_request():
|
||||
data = request.get_json()
|
||||
|
||||
errors = []
|
||||
|
||||
try:
|
||||
if REITTI_BASE_URL is None or REITTI_API_KEY is None:
|
||||
raise ValueError("Reitti API URL or Key is not configured!")
|
||||
|
||||
response1 = requests.post(
|
||||
REITTI_BASE_URL,
|
||||
'http://service1.com/api',
|
||||
json=data,
|
||||
headers=request.headers,
|
||||
params={'api_key': REITTI_API_KEY}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
app.logger.error(f'Error forwarding request to Reitti: {str(e)}')
|
||||
errors.append({'service': 'reitti', 'status': 'error', 'message': str(e)})
|
||||
|
||||
try:
|
||||
if DAWARICH_BASE_URL is None or DAWARICH_API_KEY is None:
|
||||
raise ValueError("Dawarich API URL or Key is not configured!")
|
||||
|
||||
response2 = requests.post(
|
||||
DAWARICH_BASE_URL,
|
||||
json=data,
|
||||
headers=request.headers,
|
||||
params={'api_key': DAWARICH_API_KEY}
|
||||
)
|
||||
|
||||
return jsonify({'status': 'success'}), 200
|
||||
|
||||
except Exception as e:
|
||||
app.logger.error(f'Error forwarding request to Dawarich: {str(e)}')
|
||||
errors.append({'service': 'dawarich', 'status': 'error', 'message': str(e)})
|
||||
|
||||
return jsonify(errors), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host="0.0.0.0", port=8000)
|
||||
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
requests
|
||||
Flask
|
||||
3
start.sh
Normal file
3
start.sh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
python ./multiplexer.py
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue