diff --git a/.gitignore b/.gitignore index cc55293..35264fb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ password.txt /.vscode data.json .env +build/ +index.out.css diff --git a/README-deploy.md b/README-deploy.md new file mode 100644 index 0000000..0d5d470 --- /dev/null +++ b/README-deploy.md @@ -0,0 +1,15 @@ +### Steps to Deploy +- move contents of frontend/ to /srv/www-csc-links/ +- create a `.env` file in backend/ with following contents: +``` +PASSWORD=... +PORT=... +``` +- run the following in backend/: +``` +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +``` +- run python app in backend/ (with the virtual env activated) +- edit the `.htaccess` file in /srv/www-csc-links/ to point to the running python application diff --git a/backend/main.py b/backend/main.py index bc80ba8..30e461c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -39,9 +39,9 @@ def get_data_from_query(query): con.close() return links_list -def regen_html(): +def regen_html(path): """Gets links from DB and outputs them in HTML""" - outfile = open(out_path, 'w') + outfile = open(path, 'w') links_list = get_data_from_query('SELECT url, name FROM links WHERE active=1 ORDER BY position') html = render_template('template.html', links_list=links_list) print(html, file=outfile) @@ -61,19 +61,21 @@ def get_links(): @app.route('/editor/links', methods = ['POST']) @auth.login_required def update_links(): + con = sqlite3.connect(DB_PATH) cur = con.cursor() try: - cur.execute("begin") - cur.execute('DELETE FROM links') - - links = [] data = request.json['links'] + items = 'url', 'name', 'clicks', 'active' for i in range(len(data)): if not(all(e in data[i] for e in items)): return "Bad request, some items missing from link object", 400 + links = [] + cur.execute("begin") + cur.execute('DELETE FROM links') + for i in range(len(data)): url = data[i]['url'] name = data[i]['name'] clicks = data[i]['clicks'] @@ -86,8 +88,7 @@ def update_links(): cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links) con.commit() con.close() - regen_html() - + regen_html(out_path) except Exception as e: cur.execute("rollback") con.close() diff --git a/backend/setup_db.py b/backend/setup_db.py index 98bf54f..0fb91c5 100644 --- a/backend/setup_db.py +++ b/backend/setup_db.py @@ -23,9 +23,9 @@ if cur.fetchone(): raise Exception('Links table already exists.') else: cur.execute('''CREATE TABLE links ( - url text NOT NULL, - name text NOT NULL, - clicks int NOT NULL, + url text NOT NULL, + name text NOT NULL, + clicks int NOT NULL, position int NOT NULL UNIQUE, active int NOT NULL, UNIQUE(url, name) diff --git a/build.bash b/build.bash index c904706..d9e996d 100755 --- a/build.bash +++ b/build.bash @@ -7,4 +7,16 @@ cd frontend/public NODE_ENV=production npx tailwindcss-cli@latest build index.in.css -o index.out.css cd .. npm run build && npm run export -mv out ../build/frontend +cd .. +mv frontend/out build/frontend + +# Backend +cd backend +source venv/bin/activate +if [ ! -f links.db ]; then + python3 setup_db.py +fi +cd .. +rsync -ax --exclude venv backend/ build/backend + +cp README-deploy.md build