Created
February 10, 2017 15:09
-
-
Save kognate/d11a943a5e75a8c15c66470ec92bafd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3-alpine | |
RUN pip install feedparser | |
RUN pip install flask | |
ADD my_script.py / | |
ADD owaction.py / | |
EXPOSE 5000 | |
CMD [ "python", "./my_script.py" ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import json | |
class OWAction(flask.Flask): | |
def __init__(self, name='My Openwhisk Action'): | |
flask.Flask.__init__(self,name) | |
self.add_url_rule('/init', 'init', self.initroute, methods=['POST']) | |
self.add_url_rule('/run', 'run', self.runner, methods=['POST']) | |
self._func = None | |
def initroute(self): | |
return flask.Response('{}', | |
status=200, | |
mimetype='application/json') | |
def json_dict(self, res): | |
if isinstance(res, dict): | |
return json.dumps(res) | |
else: | |
return json.dumps({'results': res}) | |
def runner(self): | |
full_params = flask.request.get_json() | |
if self._func: | |
params = {} | |
if full_params: | |
params = full_params | |
print(full_params) | |
results = self._func(params) | |
return flask.Response(response=self.json_dict(results), | |
status=200, | |
mimetype='application/json') | |
else: | |
resp_string = "{'error':'no function to run'}" | |
return flask.Response(response=resp_string, | |
status=405, | |
mimetype='application/json') | |
def action(self,func): | |
self._func = func | |
self.run(host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment