commit bbd3c995f4570dc5fe72ee7794b6f30a394a2987 Author: josiah Date: Wed Apr 28 16:25:33 2021 -0500 Create basic Warren structure. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdaab25 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +env/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..022adff --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021 jlj + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..3ecf19b --- /dev/null +++ b/__init__.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +"""Listen for stuff with flask! +This will listen on two endpoints, and when data is +posted to the proper one, matching a particular format, +it will save that data to a local sqlite3 db.""" + +from datetime import datetime +import logging +import os +from pathlib import Path + +from flask import Flask, redirect, url_for, request +import json +import sqlite3 + +import pdb + +__version__ = "0.1.0" + +logging.basicConfig(level=logging.DEBUG) + +dbpath = "warren.db" +app = Flask(__name__) + + +def parse_incoming_post(body): + """given a body of a request, convert it from json to pydict""" + json_dict = json.loads(body) + return json_dict + + +def check_if_db_exists(dbpath): + """checks to see if the db exists""" + if os.path.exists(dbpath): + return True + else: + return False + + +def init_new_db(dbpath): + """when you create a DB connection with SQLite3 you + will automatically create a db file if one is not there. + This function handles explicitly creating the folder structure + and inniting the connection as required. + + You can specify your own dbpath, or you can use the default dbpath.""" + + # TODO: write directory handling shit + + # handle the DB stuff + conn = None + + try: + conn = sqlite3.connect(dbpath) + cur = conn.cursor() + cur.execute('''CREATE TABLE weir (date, hostname, metadata)''') + conn.commit() + logging.DEBUG(sqlite3.version) + logging.DEBUG("Creating new weir table.") + + conn.close() + except: + logging.error(logging.ERROR[0]) + + +def write_to_db(data, dbpath): + + pdb.set_trace() + if check_if_db_exists(dbpath) != True: + db = init_new_db(dbpath) + + conn = sqlite3.connect(dbpath) + cur = conn.cursor() + + right_now = datetime.utcnow().strftime("%Y%m%d") + sql_inputs = [(right_now, data['hostname'], data['metadata'])] + cur.executemany("INSERT INTO weir VALUES (?,?,?)", sql_inputs) + conn.commit() + logging.INFO(cur.lastrowid) + + # always make sure to close db connection after you're done + conn.close() + + +@app.route('/') +def test(): + return 'the website is up you little bitch' + + +@app.route('/weir',methods = ['POST']) +def weir(): + """""" + try: + python_dict = request.get_json() + write_to_db(python_dict, "warren.db") + return "OK" + except: + return "Error.", 400 + + + +if __name__ == '__main__': + app.run(debug = True) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0d0dbc0 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,11 @@ +[build-system] +requires = ["flit_core >=2,<4"] +build-backend = "flit_core.buildapi" + +[tool.flit.metadata] +module = "warren" +author = "jlj" +author-email = "me@jowj.net" +home-page = "https://jowj.net" +classifiers = [ "License :: OSI Approved :: MIT License",] + diff --git a/readme.org b/readme.org new file mode 100644 index 0000000..56c29f1 --- /dev/null +++ b/readme.org @@ -0,0 +1,12 @@ +* warren is the server part of weir +Listen for http posts on two routes, +** roadmap +- [X] listen for posts on a specific url +- [X] save data extracted from body of post to sqlite +- [ ] Write directory handling stuff for new db create +- [ ] Make sure logging is working as expected; i think i've done something wrong with naming with caps. +- [ ] Migrate away from flask's dev server and use a real WSGI server per the in-app warning +- [ ] ^^ but postgresql and in a managed instance +- [ ] gui front end for sql queries +** references +gonna try this new packaging paradigm: https://antonz.org/python-packaging/