diff --git a/examples/tutorial/hotfunctions/http/func.go b/examples/tutorial/hotfunctions/http/go/func.go similarity index 100% rename from examples/tutorial/hotfunctions/http/func.go rename to examples/tutorial/hotfunctions/http/go/func.go diff --git a/examples/tutorial/hotfunctions/http/hotroute.json b/examples/tutorial/hotfunctions/http/go/hotroute.json similarity index 100% rename from examples/tutorial/hotfunctions/http/hotroute.json rename to examples/tutorial/hotfunctions/http/go/hotroute.json diff --git a/examples/tutorial/hotfunctions/http/python/Dockerfile b/examples/tutorial/hotfunctions/http/python/Dockerfile new file mode 100644 index 000000000..c0b70b919 --- /dev/null +++ b/examples/tutorial/hotfunctions/http/python/Dockerfile @@ -0,0 +1,9 @@ +FROM jjanzic/docker-python3-opencv + +RUN mkdir /code +ADD . /code/ +WORKDIR /code +RUN pip3 install -r requirements.txt + +WORKDIR /code/ +ENTRYPOINT ["python3", "func.py"] diff --git a/examples/tutorial/hotfunctions/http/python/func.py b/examples/tutorial/hotfunctions/http/python/func.py new file mode 100644 index 000000000..5b3189e84 --- /dev/null +++ b/examples/tutorial/hotfunctions/http/python/func.py @@ -0,0 +1,42 @@ +import asyncio +import sys +import uvloop + +from hotfn.http import request +from hotfn.http import response + + +class MyProtocol(asyncio.Protocol): + + def connection_made(self, transport): + print('pipe opened', file=sys.stderr, flush=True) + super(MyProtocol, self).connection_made(transport=transport) + + def data_received(self, data): + print('received: {!r}'.format(data), file=sys.stderr, flush=True) + data = data.decode() + req = request.RawRequest(data) + (method, url, dict_params, + headers, http_version, req_data) = req.parse_raw_request() + + rs = response.RawResponse( + http_version, 200, "OK", + response_data=req_data) + print(rs.dump(), file=sys.stdout, flush=True) + super(MyProtocol, self).data_received(data) + + def connection_lost(self, exc): + print('pipe closed', file=sys.stderr, flush=True) + super(MyProtocol, self).connection_lost(exc) + + +if __name__ == "__main__": + with open("/dev/stdin", "rb", buffering=0) as stdin: + asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + loop = asyncio.get_event_loop() + try: + stdin_pipe_reader = loop.connect_read_pipe(MyProtocol, stdin) + loop.run_until_complete(stdin_pipe_reader) + loop.run_forever() + finally: + loop.close() diff --git a/examples/tutorial/hotfunctions/http/python/func.yaml b/examples/tutorial/hotfunctions/http/python/func.yaml new file mode 100644 index 000000000..4b73ca40b --- /dev/null +++ b/examples/tutorial/hotfunctions/http/python/func.yaml @@ -0,0 +1,7 @@ +name: fnproject/hotfn-py +version: 0.0.1 +runtime: docker +type: sync +memory: 521 +format: http +path: /hot-json diff --git a/examples/tutorial/hotfunctions/http/python/requirements.txt b/examples/tutorial/hotfunctions/http/python/requirements.txt new file mode 100644 index 000000000..c4fd6ca78 --- /dev/null +++ b/examples/tutorial/hotfunctions/http/python/requirements.txt @@ -0,0 +1,2 @@ +hotfn +uvloop