diff --git a/dev-requirements.txt b/dev-requirements.txt
index 452aff5..0043d73 100644
--- a/dev-requirements.txt
+++ b/dev-requirements.txt
@@ -30,10 +30,15 @@ coverage==7.4.1
# via go-ji (pyproject.toml)
flask==3.0.2
# via
+ # flask-sock
# go-ji (pyproject.toml)
# sentry-sdk
+flask-sock==0.7.0
+ # via go-ji (pyproject.toml)
gunicorn==21.2.0
# via go-ji (pyproject.toml)
+h11==0.14.0
+ # via wsproto
iniconfig==2.0.0
# via pytest
itsdangerous==2.1.2
@@ -82,6 +87,8 @@ ruff-lsp==0.0.52
# via go-ji (pyproject.toml)
sentry-sdk==1.40.4
# via go-ji (pyproject.toml)
+simple-websocket==1.0.0
+ # via flask-sock
sqlalchemy==2.0.27
# via
# alembic
@@ -97,6 +104,8 @@ werkzeug==3.0.1
# via flask
wheel==0.42.0
# via pip-tools
+wsproto==1.2.0
+ # via simple-websocket
# The following packages are considered to be unsafe in a requirements file:
# pip
diff --git a/go_ji/__init__.py b/go_ji/__init__.py
index c2c6127..de4d5ba 100644
--- a/go_ji/__init__.py
+++ b/go_ji/__init__.py
@@ -24,8 +24,9 @@ VALID_SLUG = re.compile(r"^\w[-\w]*$", re.ASCII)
CONFIG = Config("")
CONFIG["DB_URL"] = "sqlite:///go-ji.db"
-CONFIG["TESTING"] = False
CONFIG["SENTRY"] = {}
+CONFIG["SOCK_SERVER_OPTIONS"] = {"ping_interval": 0.25}
+CONFIG["TESTING"] = False
CONFIG.from_prefixed_env("GO_JI")
@@ -35,6 +36,12 @@ def create_app(config_override: dict[str, Any] = {}) -> Flask:
app.config.from_mapping(CONFIG)
app.config.from_mapping(config_override)
+ sock = None
+ if app.config["DEBUG"]: # pragma: no cover
+ from flask_sock import Sock
+
+ sock = Sock(app)
+
if sentry_config := app.config["SENTRY"]: # pragma: no cover
sentry_sdk.init(
dsn=sentry_config["DSN"],
@@ -53,8 +60,8 @@ def create_app(config_override: dict[str, Any] = {}) -> Flask:
@app.before_request
def require_user() -> None:
- # Allow anonymous requests for the healthcheck
- if request.path == "/up":
+ # Allow anonymous requests for the healthcheck and hot-reloading
+ if request.path in ["/up", "hot-reload"]:
return
# I think this is unnecessary, but it also doesn't hurt
@@ -97,7 +104,12 @@ def create_app(config_override: dict[str, Any] = {}) -> Flask:
top_shorts = db_session.scalars(
select(Short).order_by(Short.clicks.desc()).limit(10)
).all()
- return render_template("index.html", name=g.user.login, top_shorts=top_shorts)
+ return render_template(
+ "index.html",
+ name=g.user.login,
+ top_shorts=top_shorts,
+ hot_reload=bool(sock),
+ )
@app.route("/links", methods=["POST"])
def create_link() -> Response:
@@ -144,4 +156,13 @@ def create_app(config_override: dict[str, Any] = {}) -> Flask:
1 / 0 # raises an error
return "<p>Hello, World!</p>" # pragma: no cover
+ # hot-reloading - hold the websocket open forever in debug mode and when it
+ # disconnects, then we know we need to reload
+ if sock: # pragma: no cover
+
+ @sock.route("/hot-reload")
+ def hot_reload(ws) -> None:
+ while True:
+ pass
+
return app
diff --git a/go_ji/templates/hot_reload.html b/go_ji/templates/hot_reload.html
new file mode 100644
index 0000000..47a9cf6
--- /dev/null
+++ b/go_ji/templates/hot_reload.html
@@ -0,0 +1,8 @@
+<script>
+ const socket = new WebSocket('ws://' + location.host + '/hot-reload');
+ const cb = ev => {
+ setTimeout(() => { window.location.reload(); }, 500);
+ };
+ socket.onerror = cb;
+ socket.onclose = cb;
+</script>
diff --git a/go_ji/templates/index.html b/go_ji/templates/index.html
index d7e8369..736b7ce 100644
--- a/go_ji/templates/index.html
+++ b/go_ji/templates/index.html
@@ -23,5 +23,8 @@
</tr>
{% endfor %}
</table>
+ {% if hot_reload %}
+ {% include "hot_reload.html" %}
+ {% endif %}
</body>
</html>
diff --git a/pyproject.toml b/pyproject.toml
index 2a7d006..106c805 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -13,6 +13,7 @@ dependencies = [
[project.optional-dependencies]
dev = [
"coverage",
+ "flask-sock",
"pip-tools",
"pyright",
"pytest",