From 24b278788e63d91cdabd921b9c97aeee545ffbfe Mon Sep 17 00:00:00 2001 From: TestingPlant Date: Mon, 10 Jul 2023 14:28:15 -0500 Subject: [PATCH] feat!: zero-downtime restarts and bind to all IPs The server can now restart with zero downtime. The server also now binds to all IPv4 and IPv6 addresses, so the ADDR config value is removed. --- README.md | 6 +++++- config.ini | 3 +-- main | 12 +++++++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 605b63a..4c66f85 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,8 @@ git clone https://codeberg.org/burger-software/burgercat cd burgercat python init_db python main -``` \ No newline at end of file +``` + +### zero downtime restarts: +- launch new burgercat server +- close previous server diff --git a/config.ini b/config.ini index 24da8c8..b4471fb 100644 --- a/config.ini +++ b/config.ini @@ -1,7 +1,6 @@ [config] -HOST = 0.0.0.0 PORT = 8080 SECRET_KEY = placeholder UPLOAD_FOLDER = uploads PASSWORD_REQUIREMENT = 12 -UPLOAD_LIMIT = 4 \ No newline at end of file +UPLOAD_LIMIT = 4 diff --git a/main b/main index ff5b804..a5c1997 100644 --- a/main +++ b/main @@ -6,6 +6,7 @@ import time import json import secrets import datetime +import socket from itertools import groupby from waitress import serve from werkzeug.utils import secure_filename @@ -19,7 +20,6 @@ from flask_limiter.util import get_remote_address config = configparser.ConfigParser() config.read("config.ini") -HOST = config["config"]["HOST"] PORT = config["config"]["PORT"] SECRET_KEY = config["config"]["SECRET_KEY"] UPLOAD_FOLDER = config["config"]["UPLOAD_FOLDER"] @@ -644,6 +644,12 @@ def page_not_found(e): if __name__ == "__main__": print("[INFO] Server started") - serve(app, host=HOST, port=PORT) - #app.run(host=HOST, port=PORT, debug=True) + + with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock: + sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + sock.bind(('', int(PORT))) + serve(app, sockets=[sock]) + print("[INFO] Server stopped")