From fcea6f68a424bb7dd76484af25f70ab999e6eaed Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Thu, 12 Jan 2023 23:22:50 +0100 Subject: [PATCH] add SIGNUP_ALLOWED switch --- README.md | 8 +++++--- src/entity/Config.ts | 4 +++- src/server.ts | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b1ce510..ad2f8cb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Something that tries to be a self-hosted alternative to Google Photos Demo: https://photos.usatiuk.com -(data is stored on tmpfs, reset every day) +(no need to enter a real email, something like asdf@asdf.com is enough, data is stored on tmpfs, reset every day) ![screenshot](docs/s1.png) @@ -30,7 +30,7 @@ Also, you need to run database migrations with Then start with `npm run dev` and visit http://localhost:1234 (Parcel dev server is listening at http://localhost:1234, and koa at http://localhost:3000) -## Actually hosting this thing +## Actually hosting this The suggested way to host this is, agian, using Docker: you can find a docker-compose example in `dockercomposeexample` folder @@ -51,7 +51,9 @@ docker-compose example in `dockercomposeexample` folder * `JWT_SECRET` - JWT secret - set it to something random -* `HTTPS` (`"yes"`/`"no"`) - whether the server enforce HTTPS or not +* `HTTPS` (`"yes"`/`"no"`) - whether the server enforces HTTPS or not + +* `SIGNUP_ALLOWED` (`"yes"`/`"no"`) - whether signups are allowed or not, persistent * `API_ROOT` diff --git a/src/entity/Config.ts b/src/entity/Config.ts index 6f0dea8..839d130 100644 --- a/src/entity/Config.ts +++ b/src/entity/Config.ts @@ -13,7 +13,7 @@ export enum ConfigKey { } export interface IDBConfig { - signupAllowed: boolean; + signupAllowed: "yes" | "no"; } const defaultValues: Record = { @@ -70,6 +70,8 @@ export async function setConfigValue( let pair = await Config.findOne({ key }); if (!pair) { pair = new Config(key, val); + } else { + pair.value = val; } await pair.save(); } diff --git a/src/server.ts b/src/server.ts index c80e7d4..8bc24e0 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,11 +1,44 @@ +import { Config, ConfigKey, setConfigValue } from "~entity/Config"; import { app } from "./app"; import { config } from "./config"; import { connect } from "./config/database"; +async function readConfig() { + if (process.env.SIGNUP_ALLOWED) { + if (process.env.SIGNUP_ALLOWED === "yes") { + await setConfigValue(ConfigKey.signupAllowed, "yes"); + console.log("Signups enabled"); + } else if (process.env.SIGNUP_ALLOWED === "no") { + await setConfigValue(ConfigKey.signupAllowed, "no"); + console.log("Signups disabled"); + } else { + await setConfigValue(ConfigKey.signupAllowed, "no"); + console.log("Signups disabled"); + } + } +} + +async function dumpConfig() { + console.log("Running with config:"); + //TODO: not print sensitive values + for (const entry of await Config.find()) { + console.log(`${entry.key} = ${entry.value}`); + } +} connect() .then((connection) => { console.log(`Connected to ${connection.name}`); - app.listen(config.port); - console.log(`Listening at ${config.port}`); + const startApp = () => { + app.listen(config.port); + console.log(`Listening at ${config.port}`); + }; + + readConfig() + .then(() => + dumpConfig() + .then(() => startApp()) + .catch((e) => console.log(e)), + ) + .catch((e) => console.log(e)); }) .catch((e) => console.log(e));