From 550d3176ff7a78e4a8c78bac02a6c78b458395d0 Mon Sep 17 00:00:00 2001 From: brb Date: Wed, 5 Jul 2023 15:41:44 +0200 Subject: [PATCH] updated code --- app.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ dev.env | 4 +++- req-dev.txt | 9 +++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app.py create mode 100644 req-dev.txt diff --git a/app.py b/app.py new file mode 100644 index 0000000..11caef8 --- /dev/null +++ b/app.py @@ -0,0 +1,67 @@ +# from waitress import serve +# from flask import Flask, request, jsonify +import uvicorn +from fastapi import FastAPI, Request, HTTPException, status +from dotenv import load_dotenv +import logging +import os + +app = FastAPI() + +# @app.middleware('http') +# async def check_media_type(request: Request, call_next): +# """ +# Ensure only requests with content-type of application/json are handled. +# """ +# content_type = request.headers.get('content-type', default=None) +# if content_type is None or not content_type.lower().startswith('application/json'): +# raise HTTPException( +# status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, +# detail=f'Unsupported media type {content_type}' +# ) +# response = await call_next(request) +# return response + +@app.get('/') +async def root(): + return {'message': 'hello world'} + +@app.post('/test') +async def test(request: Request): + """ + Test connection to server + """ + logging.debug('Content-Type: ', request.headers.get('content-type')) + return {'status': 'success'} + +# @app.post('/execute') + +# @app.before_request +# def check_media_type(): +# """ +# Ignore requests that do not have Content-Type = application/json in header. +# """ +# content_type = request.headers.get('Content-Type') +# if content_type is None or not content_type.lower().startswith('application/json'): +# logging.error( +# f"request with wrong content type made by {request.remote_addr}" +# ) +# return jsonify(status='unsupported media type'), 415 + +# @app.route('/test', methods=['POST']) +# def test(): +# logging.debug('Content-Type: ' + request.headers.get('Content-Type')) +# logging.debug(request.get_json(force=True)) +# return jsonify(status='success', sender=request.remote_addr), 200 + +if __name__ == '__main__': + load_dotenv() + # prepare variables + # serve( + # app=app, + # host=listen_ip, + # port=listen_port + # ) + uvicorn.run( + app=app + ) \ No newline at end of file diff --git a/dev.env b/dev.env index e548382..acd3cc2 100644 --- a/dev.env +++ b/dev.env @@ -1 +1,3 @@ -LOGGER_LEVEL = debug \ No newline at end of file +LOGGER_LEVEL = debug +LISTEN_IP = 0.0.0.0 +LISTEN_PORT = 1706 \ No newline at end of file diff --git a/req-dev.txt b/req-dev.txt new file mode 100644 index 0000000..58236b5 --- /dev/null +++ b/req-dev.txt @@ -0,0 +1,9 @@ +blinker==1.6.2 +click==8.1.3 +Flask==2.3.2 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.3 +python-dotenv==1.0.0 +waitress==2.1.2 +Werkzeug==2.3.6