diff --git a/appspec.yml b/appspec.yml new file mode 100755 index 0000000..750544d --- /dev/null +++ b/appspec.yml @@ -0,0 +1,18 @@ +version: 0.0 +os: linux +files: + - source: / + destination: /web/ +hooks: + AfterInstall: + - location: scripts/mkdir.sh + timeout: 300 + runas: root + ApplicationStart: + - location: scripts/start_flask.sh + timeout: 300 + runas: root + ApplicationStop: + - location: scripts/stop_flask1.sh + timeout: 300 + runas: root diff --git a/buildspec.yml b/buildspec.yml new file mode 100755 index 0000000..5a06297 --- /dev/null +++ b/buildspec.yml @@ -0,0 +1,24 @@ +version: 0.2 + +phases: + install: + commands: + - python -m pip install Flask + build: + commands: + - echo Build started on `date` + - echo Compiling the Python code... + - python test_app.py + post_build: + commands: + - echo Build completed on `date` +artifacts: + files: + - web.py + - appspec.yml + - templates/layout.html + - templates/test.html + - scripts/mkdir.sh + - scripts/start_flask.sh + - scripts/stop_flask1.sh + - scripts/stop_flask.py diff --git a/requirements.txt b/requirements.txt new file mode 100755 index 0000000..e3e9a71 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask diff --git a/scripts/mkdir.sh b/scripts/mkdir.sh new file mode 100755 index 0000000..63a0d54 --- /dev/null +++ b/scripts/mkdir.sh @@ -0,0 +1,5 @@ +mkdir -p /web +curl -O https://bootstrap.pypa.io/get-pip.py +python get-pip.py --user +python -m pip install Flask +#sdssdsds diff --git a/scripts/start_flask.sh b/scripts/start_flask.sh new file mode 100755 index 0000000..7cd4d53 --- /dev/null +++ b/scripts/start_flask.sh @@ -0,0 +1 @@ +python /web/web.py > /dev/null 2>&1 & diff --git a/scripts/stop_flask.py b/scripts/stop_flask.py new file mode 100755 index 0000000..9658d06 --- /dev/null +++ b/scripts/stop_flask.py @@ -0,0 +1,3 @@ +import requests + +requests.post("http://127.0.0.1/shutdown") \ No newline at end of file diff --git a/scripts/stop_flask.sh b/scripts/stop_flask.sh new file mode 100755 index 0000000..169cfd8 --- /dev/null +++ b/scripts/stop_flask.sh @@ -0,0 +1,6 @@ +#!/usr/bin/python +import requests + +requests.post("http://127.0.0.1/shutdown") + + diff --git a/scripts/stop_flask1.sh b/scripts/stop_flask1.sh new file mode 100755 index 0000000..dfe7164 --- /dev/null +++ b/scripts/stop_flask1.sh @@ -0,0 +1 @@ +python /web/scripts/stop_flask.py \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html new file mode 100755 index 0000000..d0a353a --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,25 @@ + + +
+{% endblock %}
\ No newline at end of file diff --git a/test_app.py b/test_app.py new file mode 100644 index 0000000..d8353c7 --- /dev/null +++ b/test_app.py @@ -0,0 +1,27 @@ +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: MIT-0 + +from web import myapp +import unittest + +# python -m unittest test_app + + +class TestMyApp(unittest.TestCase): + + def setUp(self): + self.app = myapp.test_client() + + def test_main(self): + rv = self.app.get('/') + assert rv.status == '200 OK' + assert b'Congratulations' in rv.data + #assert False + + def test_404(self): + rv = self.app.get('/other') + self.assertEqual(rv.status, '404 NOT FOUND') + + +if __name__ == '__main__': + unittest.main() diff --git a/web.py b/web.py new file mode 100644 index 0000000..d20632a --- /dev/null +++ b/web.py @@ -0,0 +1,32 @@ +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: MIT-0 + +from flask import Flask, flash, redirect, render_template, request, session, abort +from random import randint + + +def shutdown_server(): + func = request.environ.get('werkzeug.server.shutdown') + if func is None: + raise RuntimeError('Not running with the Werkzeug Server') + func() + + +myapp = Flask(__name__) + + +@myapp.route('/shutdown', methods=['POST']) +def shutdown(): + shutdown_server() + return 'Server shutting down...' + + +@myapp.route("/") +def hello(): + # return name + return render_template( + 'test.html', **locals()) + + +if __name__ == "__main__": + myapp.run(host='0.0.0.0', port=80)