hashimotoa961 commited on
Commit
ab54869
·
1 Parent(s): 8797caa

fix for fastapi + uvicorn

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. app.py +12 -22
  3. requirements.txt +2 -1
Dockerfile CHANGED
@@ -5,4 +5,4 @@ COPY ./requirements.txt /code/requirements.txt
5
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
6
 
7
  COPY . .
8
- CMD ["python", "app.py"]
 
5
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
6
 
7
  COPY . .
8
+ CMD ["uvicorn","app:app","--host","0.0.0.0","--port","7860"]
app.py CHANGED
@@ -1,32 +1,22 @@
1
- from flask import Flask, request, jsonify
 
2
  import requests
3
 
4
- app = Flask(__name__)
5
 
6
- # @app.route('/', methods=['GET'])
7
- # def hello():
8
- # return {'Hello' : 'World!'}
9
 
10
- @app.route('/', methods=['GET'])
11
- def api():
12
- print("get", request.args)
13
  # クライアントからのリクエストデータを取得
14
- endpoint = request.args.get('endpoint')
15
- other_params = {key: value for key, value in request.args.items() if key != 'endpoint'}
16
-
17
  if not endpoint:
18
- return jsonify({'error': 'No endpoint specified'}), 400
19
-
20
  # AWS API Gatewayにリクエストを転送
21
  print(endpoint, other_params)
22
  response = requests.get(endpoint, params=other_params)
23
-
24
  # AWSからのレスポンスをクライアントに返す
25
- return jsonify(response.json()), response.status_code
26
-
27
-
28
-
29
- if __name__ == '__main__':
30
- app.run(host='0.0.0.0', port=7860)
31
-
32
-
 
1
+ from fastapi import FastAPI, Query, HTTPException
2
+ from typing import Dict, Optional
3
  import requests
4
 
5
+ app = FastAPI()
6
 
7
+ @app.get("/")
8
+ def read_root():
9
+ return {"message": "Hello, World!"}
10
 
11
+ @app.get("/api/")
12
+ def api(endpoint: str = Query(..., alias="endpoint"), other_params: Optional[Dict[str, str]] = None):
 
13
  # クライアントからのリクエストデータを取得
 
 
 
14
  if not endpoint:
15
+ raise HTTPException(status_code=400, detail="No endpoint specified")
16
+
17
  # AWS API Gatewayにリクエストを転送
18
  print(endpoint, other_params)
19
  response = requests.get(endpoint, params=other_params)
20
+
21
  # AWSからのレスポンスをクライアントに返す
22
+ return response.json()
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
- Flask
 
2
  requests
 
1
+ fastapi
2
+ uvicorn
3
  requests