From 47b30cea327420ed6f17ccc6fd521ad89f4b4e4f Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 7 Mar 2021 09:59:14 +0100 Subject: Add Makefiles and implement Config Parser in server.py --- Makefile | 8 ++++++++ src/Makefile | 8 ++++++++ src/config.ini | 20 +++++++++++++++++++- src/server.py | 17 ++++++++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 src/Makefile mode change 100644 => 100755 src/server.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8814205 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +# vim: shiftwidth=4 tabstop=4 noexpandtab + +all: + echo "make all - nothing to do in this pure python project ;)" + +debug: + make -C src/ debug + diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..43ea379 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,8 @@ +# vim: shiftwidth=4 tabstop=4 noexpandtab + +all: + echo "make all - nothing to do in this pure python project ;)" + +debug: + ./server.py + diff --git a/src/config.ini b/src/config.ini index 2a67194..c91e9ce 100644 --- a/src/config.ini +++ b/src/config.ini @@ -1,5 +1,23 @@ +; This section is for the configuration of xengineerings web-template. +; You can use additional sections for other purposes. [web-template] + +; Choose a project_name containing only characters and underscores. +; This project_name is used for the systemd service of your +; application (/etc/systemd/system/.service) and the +; folder in /opt where your application is deployed +; (/opt//). project_name=my_web_server -friendly_project_name=My first web Server + +; Choose a project_description to be used for the systemd unit. +project_description=A template for Web Applications + +; Select a linux user and group for execution +user=http +group=http + +; Select an IP address and port to bind to. +address=127.0.0.1 +port=8080 diff --git a/src/server.py b/src/server.py old mode 100644 new mode 100755 index 2caddd6..37fcb51 --- a/src/server.py +++ b/src/server.py @@ -2,6 +2,7 @@ # vim: shiftwidth=4 tabstop=4 expandtab +import configparser import waitress from flask import Flask @@ -14,8 +15,22 @@ def index(): return app.send_static_file("index.html") +def load_config(): + """Load ./config.ini File and read web-template Section""" + + retval = {} + + config = configparser.ConfigParser() + config.read("config.ini") + for option in config.options("web-template"): + retval[option] = config.get("web-template", option) + + return retval # dictionary with all options from web-template section + + if __name__ == '__main__': - waitress.serve(app, listen="127.0.0.1:8080") # production server / bind to port + config = load_config() + waitress.serve(app, listen="{}:{}".format(config["address"], config["port"])) # production server / bind to port #serve(app, unix_socket='/run/web-template/unix.sock') # production server / unix domain socket #app.run() # debug server - NOT FOR PRODUCTION! -- cgit v1.2.3-70-g09d2