Move to FastAPI and add DIY
This commit is contained in:
parent
e6ec6de63f
commit
b8c35efc2c
12 changed files with 65 additions and 57 deletions
14
Dockerfile
14
Dockerfile
|
|
@ -1,14 +1,14 @@
|
||||||
FROM python:3.7-alpine
|
FROM python:3.9-rc-alpine
|
||||||
|
|
||||||
MAINTAINER Yehuda Deutsch <yeh@uda.co.il>
|
MAINTAINER Yehuda Deutsch <yeh@uda.co.il>
|
||||||
|
|
||||||
RUN echo 'manylinux1_compatible = True' > /usr/local/lib/python3.7/_manylinux.py
|
RUN apk add --virtual .build-deps gcc libc-dev make \
|
||||||
RUN apk add --virtual .build-deps gcc musl-dev \
|
&& pip install fastapi uvicorn \
|
||||||
&& pip install sanic \
|
|
||||||
&& rm -Rf ~/.cache \
|
&& rm -Rf ~/.cache \
|
||||||
&& apk --purge del .build-deps
|
&& apk --purge del .build-deps gcc libc-dev make
|
||||||
|
|
||||||
WORKDIR /code
|
WORKDIR /code
|
||||||
COPY app.py /code/
|
ENV PYTHONPATH=/code
|
||||||
|
COPY time_app/ /code/time_app/
|
||||||
|
|
||||||
CMD ["python", "app.py"]
|
CMD ["python", "time_app"]
|
||||||
|
|
|
||||||
50
app.py
50
app.py
|
|
@ -1,50 +0,0 @@
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import text, json
|
|
||||||
from sanic.views import HTTPMethodView
|
|
||||||
|
|
||||||
|
|
||||||
class IndexView(HTTPMethodView):
|
|
||||||
def get(self, request):
|
|
||||||
now = datetime.utcnow()
|
|
||||||
return text(now.strftime('%Y-%m-%d %H:%M:%S'))
|
|
||||||
|
|
||||||
|
|
||||||
class JsonView(HTTPMethodView):
|
|
||||||
def get(self, request):
|
|
||||||
now = datetime.utcnow()
|
|
||||||
return json({
|
|
||||||
'now': now.strftime('%Y-%m-%d %H:%M:%S'),
|
|
||||||
'timestamp': int(datetime.timestamp(now)),
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
class Application(object):
|
|
||||||
app = Sanic()
|
|
||||||
|
|
||||||
def __init__(self, args):
|
|
||||||
self.args = args
|
|
||||||
self.app.add_route(IndexView.as_view(), '/')
|
|
||||||
self.app.add_route(JsonView.as_view(), '/json')
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
self.app.run(host=self.args.interface, port=self.args.port)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import os
|
|
||||||
from argparse import ArgumentParser
|
|
||||||
|
|
||||||
default_host = os.environ.get('APP_HOST', '0.0.0.0')
|
|
||||||
default_port = int(os.environ.get('APP_PORT', 8080))
|
|
||||||
default_trusted = os.environ.get('TRUSTED_PROXIES') or None
|
|
||||||
|
|
||||||
parser = ArgumentParser('app.py')
|
|
||||||
parser.add_argument('--interface', '-i', type=str, help='Interface to listen on', default=default_host)
|
|
||||||
parser.add_argument('--port', '-p', type=int, help='Port to listen on', default=default_port)
|
|
||||||
parser.add_argument('--proxies', type=str, help='A list of trusted proxies, comma separated. you can use CIDRs.', default=default_trusted)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
zero_if = Application(args)
|
|
||||||
zero_if.run()
|
|
||||||
1
time_app/__init__.py
Normal file
1
time_app/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
3
time_app/__main__.py
Normal file
3
time_app/__main__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
from time_app.main import run
|
||||||
|
|
||||||
|
run()
|
||||||
0
time_app/helpers/__init__.py
Normal file
0
time_app/helpers/__init__.py
Normal file
5
time_app/helpers/datetime.py
Normal file
5
time_app/helpers/datetime.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
|
||||||
|
def real_utc_now() -> datetime:
|
||||||
|
return datetime.now(tz=timezone.utc)
|
||||||
13
time_app/main.py
Normal file
13
time_app/main.py
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import uvicorn
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import PlainTextResponse
|
||||||
|
|
||||||
|
from .views import main, api
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(main.router, tags=['text'], default_response_class=PlainTextResponse)
|
||||||
|
app.include_router(api.router, prefix='/api/v1', tags=['api'])
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
uvicorn.run("time_app.main:app", host="0.0.0.0", port=8000, log_level="info")
|
||||||
0
time_app/models/__init__.py
Normal file
0
time_app/models/__init__.py
Normal file
5
time_app/models/main.py
Normal file
5
time_app/models/main.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Now(BaseModel):
|
||||||
|
time: str
|
||||||
0
time_app/views/__init__.py
Normal file
0
time_app/views/__init__.py
Normal file
16
time_app/views/api.py
Normal file
16
time_app/views/api.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
from time_app.helpers.datetime import real_utc_now
|
||||||
|
from time_app.models.main import Now
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/', name='Now', response_model=Now)
|
||||||
|
def now_view() -> dict:
|
||||||
|
return {"time": real_utc_now().strftime('%Y-%m-%d %H:%M:%S')}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/diy', name='Day in year', response_model=Now)
|
||||||
|
def day_in_year_view() -> dict:
|
||||||
|
return {"time": real_utc_now().strftime('%y%j')[-4:]}
|
||||||
15
time_app/views/main.py
Normal file
15
time_app/views/main.py
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
from time_app.helpers.datetime import real_utc_now
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/', name='Now')
|
||||||
|
def now_view() -> str:
|
||||||
|
return real_utc_now().strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/diy', name='Day in year')
|
||||||
|
def day_in_year_view() -> str:
|
||||||
|
return real_utc_now().strftime('%y%j')[-4:]
|
||||||
Loading…
Add table
Reference in a new issue