From 9cf41845a8580179d7abdcf549ff7eb2d6c753cf Mon Sep 17 00:00:00 2001
From: lare <lare@lare.cc>
Date: Fri, 11 Nov 2022 20:17:37 +0100
Subject: [PATCH] add sample config.json + working webserver

---
 .gitignore                     |  2 ++
 web/backend/config.sample.json | 19 ++++++++++++
 web/backend/main.py            | 56 ++++++++++++++++++++++++++++++++++
 web/backend/templates          |  1 +
 web/frontend/index.html        | 17 +++++++++++
 web/frontend/static/style.css  |  4 +++
 6 files changed, 99 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 web/backend/config.sample.json
 create mode 100644 web/backend/main.py
 create mode 120000 web/backend/templates
 create mode 100644 web/frontend/index.html
 create mode 100644 web/frontend/static/style.css

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1b0e799
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+venv
+config.json
\ No newline at end of file
diff --git a/web/backend/config.sample.json b/web/backend/config.sample.json
new file mode 100644
index 0000000..6bd54b6
--- /dev/null
+++ b/web/backend/config.sample.json
@@ -0,0 +1,19 @@
+{
+    "nodes": {
+        "<nodename>": {
+            "pub-endpoint": "<clearnet-fqdn/ip-address>",
+            "api-con": "http://<node-(internal)-ip/hostname>:<port>/",
+
+            "comment": "/* from here: data to be displayed on the webinterface */",
+            "internal-v4": "172.2x.xxx.xxx",
+            "internal-v6": "fdxx:...",
+            "internal-v4ll": "169.254.xxx.xxx",
+            "internal-v6ll": "fe80::..."
+        }
+    },
+    "MNT": "YOUR-MNT", // your MNT tag
+    "listen": "0.0.0.0",
+    "port": 8042, 
+    "flask-debug": false, // optional; default false
+    "flask-template-dir": "../frontend/" // optional; default "../frontend"
+}
\ No newline at end of file
diff --git a/web/backend/main.py b/web/backend/main.py
new file mode 100644
index 0000000..648cdf3
--- /dev/null
+++ b/web/backend/main.py
@@ -0,0 +1,56 @@
+#! /usr/bin/env python3
+
+from flask import Flask, Response, redirect, render_template, request, session, abort
+import json, os
+
+app = Flask(__name__)
+
+class Config (dict):
+    def __init__(self, configfile:str = None):
+        if configfile:
+            self.configfile = configfile
+        else:
+            if os.path.exists("./config.json"): self.configfile = "./config.json"
+            elif os.path.exists("/etc/dn42-autopeer/config.json"): self.configfile = "/etc/dn42-autopeer/config,json"
+            else: raise FileNotFoundError("no config file found in ./config.json or /etc/dn42-autopeer/config.json")
+        self.load_config()
+        self.keys = self._config.keys
+        #self.__getitem__ = self._config.__getitem__
+        super().__init__(self)
+
+    def __delitem__(self, v):
+        raise NotImplementedError()
+        super().__delitem__(self,v)
+    def __getitem__(self, k):
+        return self._config[k]
+    def load_config(self):
+        with open(self.configfile) as cf:
+            try:
+                self._config = json.load(cf)
+            except json.decoder.JSONDecodeError:
+                raise SyntaxError(f"no valid JSON found in '{cf.name}'")
+        
+        if not "flask-template-dir" in self._config:
+            self._config["flask-template-dir"] = "../frontend" 
+        
+        if not "flask-debug" in self._config:
+            self._config["flask-debug"] = False 
+
+        print(self._config)
+
+
+config = Config()
+    
+@app.route("/")
+def index():
+    print(config)
+    return render_template("index.html", config=config)
+
+def main():
+    app.static_folder= config["flask-template-dir"]+"/static/"
+    app.template_folder=config["flask-template-dir"]
+    app.run(host=config["listen"], port=config["port"], debug=config["flask-debug"], threaded=True)
+
+
+if __name__ == "__main__":
+    main()
\ No newline at end of file
diff --git a/web/backend/templates b/web/backend/templates
new file mode 120000
index 0000000..af28878
--- /dev/null
+++ b/web/backend/templates
@@ -0,0 +1 @@
+../frontend
\ No newline at end of file
diff --git a/web/frontend/index.html b/web/frontend/index.html
new file mode 100644
index 0000000..b3370b9
--- /dev/null
+++ b/web/frontend/index.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>{{config["MNT"]}} Autopeering</title>
+    <link rel="stylesheet" href="static/style.css">
+</head>
+<body>
+    <div class="content">
+        <div>{{config["MNT"]}}</div>
+        <div></div>
+        <div></div>
+    </div>
+</body>
+</html>
\ No newline at end of file
diff --git a/web/frontend/static/style.css b/web/frontend/static/style.css
new file mode 100644
index 0000000..cc56f0a
--- /dev/null
+++ b/web/frontend/static/style.css
@@ -0,0 +1,4 @@
+body {
+    color: wheat;
+    background-color: black;
+}
\ No newline at end of file