From 03e431a2ce8de92eccc2ababb0c97c60897238b8 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Mon, 2 May 2016 09:30:27 -0600 Subject: [PATCH] Get basic debug Flask application working Now I can serve up an API endpoint that indicates the version. yay --- bin/vanth | 9 +++++++++ setup.py | 2 ++ vanth/api/about.py | 18 ++++++++++++++++++ vanth/config.py | 9 +++++++++ vanth/main.py | 34 ++++++++++++++++++++++++++++++++++ vanth/server.py | 21 +++++++++++++++++++++ 6 files changed, 93 insertions(+) mode change 100644 => 100755 bin/vanth create mode 100644 vanth/api/about.py create mode 100644 vanth/config.py create mode 100644 vanth/main.py create mode 100644 vanth/server.py diff --git a/bin/vanth b/bin/vanth old mode 100644 new mode 100755 index e69de29..042d2f7 --- a/bin/vanth +++ b/bin/vanth @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +import vanth.main + + +def run(): + vanth.main.main() + +if __name__ == '__main__': + run() diff --git a/setup.py b/setup.py index 95c7204..078a8d3 100644 --- a/setup.py +++ b/setup.py @@ -99,6 +99,8 @@ def main(): }, install_requires = [ 'chryso==1.3', + 'Flask==0.10.1', + 'flask-login==0.3.2', 'sepiida==5.27', ], extras_require = { diff --git a/vanth/api/about.py b/vanth/api/about.py new file mode 100644 index 0000000..974343a --- /dev/null +++ b/vanth/api/about.py @@ -0,0 +1,18 @@ +import json + +import flask +import sepiida.endpoints +import sepiida.fields + +from vanth.version import VERSION + + +class About(sepiida.endpoints.APIEndpoint): + ENDPOINT = "/about/" + SIGNATURE = sepiida.fields.JSONObject(s={ + 'version' : sepiida.fields.String() + }) + PUBLIC_METHODS = ['list'] + @staticmethod + def list(): + return flask.make_response(json.dumps({'version': VERSION}), 200) diff --git a/vanth/config.py b/vanth/config.py new file mode 100644 index 0000000..f3e2206 --- /dev/null +++ b/vanth/config.py @@ -0,0 +1,9 @@ +import sepiida.config + +SPECIFICATION = { + 'api_token' : sepiida.config.Option(str, 'api_token'), + 'secret_key' : sepiida.config.Option(str, 'some-secret-key'), + 'session_cookie_domain' : sepiida.config.Option(str, None), + 'db' : sepiida.config.Option(str, 'postgres://vanth_dev:letmein@localhost:5432/vanth_test'), + 'debug' : sepiida.config.Option(bool, True), +} diff --git a/vanth/main.py b/vanth/main.py new file mode 100644 index 0000000..fe4429f --- /dev/null +++ b/vanth/main.py @@ -0,0 +1,34 @@ +import logging +import os + +import chryso.connection +import sepiida.config +import sepiida.log + +import vanth.config +import vanth.server +import vanth.tables + +LOGGER = logging.getLogger(__name__) + +def create_application(config): + sepiida.log.setup_logging() + engine = chryso.connection.Engine(config.db, vanth.tables) + chryso.connection.store(engine) + + LOGGER.info("Starting up vanth version %s", vanth.version.VERSION) + application = vanth.server.create_app(config) + return application + +def main(): + logging.getLogger().setLevel(logging.DEBUG) + logging.basicConfig() + + config = sepiida.config.load('/etc/vanth.yaml', vanth.config.SPECIFICATION) + application = create_application(config) + try: + host = os.getenv('HOST', 'localhost') + port = int(os.getenv('PORT', 4545)) + application.run(host, port) + except KeyboardInterrupt: + LOGGER.info('Shutting down') diff --git a/vanth/server.py b/vanth/server.py new file mode 100644 index 0000000..07e64ed --- /dev/null +++ b/vanth/server.py @@ -0,0 +1,21 @@ +from flask import Flask +from flask_uuid import FlaskUUID +from sepiida import endpoints + +import vanth.api.about + + +def create_app(config): + app = Flask('vanth') + + FlaskUUID(app) + app.config.update( + API_TOKEN = config.api_token, + DEBUG = config.debug, + SECRET_KEY = config.secret_key, + SESSION_COOKIE_DOMAIN = config.session_cookie_domain, + ) + + endpoints.add_resource(app, vanth.api.about.About, endpoint='about') + + return app