Skip to content

Instantly share code, notes, and snippets.

@kognate
Created February 10, 2017 15:09
Show Gist options
  • Save kognate/d11a943a5e75a8c15c66470ec92bafd8 to your computer and use it in GitHub Desktop.
Save kognate/d11a943a5e75a8c15c66470ec92bafd8 to your computer and use it in GitHub Desktop.
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" ]
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