From 7a56ab7ba14f3b25a8986510f35d28a8fc61ebf8 Mon Sep 17 00:00:00 2001 From: Cappy Ishihara Date: Nov 23 2021 01:48:28 +0000 Subject: test skeleton code --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/config.py b/config.py deleted file mode 100644 index e69de29..0000000 --- a/config.py +++ /dev/null diff --git a/lapis-server b/lapis-server new file mode 100755 index 0000000..5ca5b47 --- /dev/null +++ b/lapis-server @@ -0,0 +1,59 @@ +#!/usr/bin/python3 + +import flask +from flask.config import Config as FlaskConfig + + +import lapis.builds +import lapis.config as config + +baseurl = config.get('baseurl') +print(config.list()) + +print(FlaskConfig.values) + +def main(): + from . import app + app.run(config) + +app = flask.Flask(__name__) +app.config["DEBUG"] = True +# set flask port to config + + +app.config['PORT'] = config.get('port') + + +@app.route(baseurl, methods=['GET']) +def home(): + return ''' + This is the Lapis Build system Backend server. Only API requests are supported. + ''' + +@app.route(baseurl + '/build', methods=['POST']) +def build(): + # get file upload from request + file = flask.request.files['file'] + # save file to disk + filename = file.filename + file.save(filename) + # run mock with file + return ''' + This is the Lapis Build system Backend server. Only API requests are supported. + ''' +# if it's a get request, return the list of builds +@app.route(baseurl + '/build' , methods=['GET']) +def get_builds(): + # return array + return builds.builds(10).to_json() + + +@app.route('/api/build/', methods=['GET']) +def get_build(build_id): + return ''' + This is the Lapis Build system Backend server. Only API requests are supported. + ''' + +print('Lapis Build system Backend server started.') + +app.run() \ No newline at end of file diff --git a/lapis-server.py b/lapis-server.py deleted file mode 100644 index c2f02c6..0000000 --- a/lapis-server.py +++ /dev/null @@ -1,6 +0,0 @@ -import HTTPServer - -def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler): - server_address = ('', 8000) - httpd = server_class(server_address, handler_class) - httpd.serve_forever() \ No newline at end of file diff --git a/lapis/builds.py b/lapis/builds.py new file mode 100644 index 0000000..40dd9a0 --- /dev/null +++ b/lapis/builds.py @@ -0,0 +1,44 @@ +# export module as lapis.builds + +# set up the database +import postgresql +import json +import os +import time + + +#JSON schema for builds +schema = { + 'id': int, + 'name': str, + 'description': str, + 'source': str, + 'status': str, + 'started_at': str, #ISO 8601 + 'finished_at': str, # ISO 8601 + 'duration': int, + 'output': [ + { + 'name': str, + 'path': str, + 'size': int, + 'mtime': str, # ISO 8601 + } + ] +} +# Postgresql schema setup +schema_sql = """ +CREATE TABLE IF NOT EXISTS builds ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL, + description varchar(255) NOT NULL, + source varchar(255) NOT NULL, + status varchar(255) NOT NULL, + started_at timestamp NOT NULL, + finished_at timestamp, + duration int, + output jsonb NOT NULL +); +""" + +#export module as lapis.builds diff --git a/lapis/config.py b/lapis/config.py new file mode 100644 index 0000000..fbef62c --- /dev/null +++ b/lapis/config.py @@ -0,0 +1,72 @@ +#default config + +import configparser +import os +import sys + +default = { + 'port': 8080, + 'debug': True, + 'host': 'localhost', + 'tempdir': '/tmp', + 'datadir': '/srv/lapis', + 'database': 'lapis', + 'database_user': 'lapis', + 'database_password': 'lapis', + 'database_host': 'localhost', + 'database_port': 5432, + 'database_type': 'postgresql', + 'database_schema': 'public', + 'database_ssl': False, + 'database_ssl_key': '', + 'database_ssl_cert': '', + 'database_ssl_ca': '', + 'baseurl': '/api', + 'secret': '', + 'logfile': '/var/log/lapis/lapis.log', + 'logfile_level': 'DEBUG', + 'logfile_max_size': 10485760, + 'logfile_max_backups': 5, + 'logfile_max_age': 7, + 'datadir': '/srv/lapis', +} + +# if --config or -c is not specified, use default config +if len(sys.argv) > 1 and (sys.argv[1] == '-c' or sys.argv[1] == '--config'): + config_file = sys.argv[2] +else: + config_file = '/etc/lapis/backend.conf' + +# load config from file with configparser +config = configparser.ConfigParser() +# if config file exists, load it +if os.path.isfile(config_file): + config.read(config_file) +# if config file does not exist, create it and write default config to it +else: + try: + # make dir if it doesn't exist + os.makedirs(os.path.dirname(config_file), exist_ok=True) + config.add_section('general') + for key, value in default.items(): + config.set('general', key, str(value)) + with open(config_file, 'w') as configfile: + config.write(configfile) + except OSError: + #throw error if config file cannot be created + raise OSError('Cannot create config file') + +#export config to global namespace +def get(key): + return config.get('general', key) + +def set(key, value): + config.set('general', key, value) + # then write config to file + with open(config_file, 'w') as configfile: + config.write(configfile) + +def list(): + #return list of all config options as dict + return {key: get(key) for key in config['general']} + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7e10602 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flask