Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
STATS_MODE="STATS_FILE"
STATS_FILE_PATH="/usr/src/app/data/stats.json"
RESET_TOKEN="your_reset_token_here"
STATS_MODE=STATS_FILE
STATS_FILE_PATH=/usr/src/app/data/stats.json
RESET_TOKEN=your_token
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ EXPOSE 3000

# Define environment variables (optional, can be set at runtime)
# ENV PORT=3000
# ENV RESET_TOKEN=supersecret


# Run the app when the container launches
CMD [ "npm", "start" ]
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@ It is based on the example [nodejs-quiz-app-l5hig](https://codesandbox.io/p/sand

Before running the application, you can configure it using a `.env` file in the root of the project. Create a `.env` file by copying the example:


```bash
cp .env.example .env
```

Then, edit the `.env` file to set your desired configuration:

### 🔐 Reset Token Behavior

If `RESET_TOKEN` is not provided, the application will:

- Generate a random human-readable token (e.g., `HappyHorse123`)
- Print it in the console logs at startup

Make sure to note this token and share it with the operator/admin if reset functionality is needed.

- `STATS_MODE`: Can be `STATS_FILE` to save statistics to a file or `IN_MEMORY` to keep them in memory (they will be lost on restart). Defaults to `STATS_FILE`.
- `STATS_FILE_PATH`: The path to the JSON file where statistics will be stored if `STATS_MODE` is `STATS_FILE`. Defaults to `./data/stats.json`.
- `RESET_TOKEN`: A secret token required to reset the statistics via the `/reset` endpoint. If not set, the reset functionality will be disabled.
- `RESET_TOKEN`: A secret token required to reset the statistics via the `/reset` endpoint.
If not set, the application will automatically generate a human-readable token (e.g., `HappyHorse123`) and print it in the logs.
Share this token with the operator/admin.

### Using npm 📦

Expand Down Expand Up @@ -70,7 +82,7 @@ Then, edit the `.env` file to set your desired configuration:
To override the default port (3000) or reset token: 🔑

```bash
RESET_TOKEN=supersecret PORT=4000 npm start
PORT=4000 npm start
```

5. Visit [http://localhost:3000](http://localhost:3000) (or your specified port) in your browser. 🌐
Expand Down Expand Up @@ -135,7 +147,7 @@ This will generate the `public/styles.css` file.
To override the default port (3000) or reset token (ensure your start script or application handles these environment variables): 🔑

```bash
RESET_TOKEN=supersecret PORT=4000 bun start
PORT=4000 bun start
```

5. Visit [http://localhost:3000](http://localhost:3000) (or your specified port) in your browser. 🌐
Expand All @@ -150,7 +162,7 @@ distrobox enter quiz
git clone https://github.com/openSUSE/quiz.git
cd quiz
npm install
npm start # For port overriding: RESET_TOKEN=supersecret PORT=4000 npm start
npm start # For port overriding: PORT=4000 npm start
```

Visit [http://localhost:3000](http://localhost:3000) in your browser. 🌐
Expand All @@ -169,7 +181,7 @@ docker build -t opensuse-quiz .
Then, run the container: ▶️

```bash
docker run -p 3000:3000 -e RESET_TOKEN=supersecret -e PORT=3000 opensuse-quiz
docker run -p 3000:3000 -e PORT=3000 opensuse-quiz
```

You can change the `RESET_TOKEN` and `PORT` environment variables as needed. ⚙️
Expand All @@ -187,7 +199,7 @@ At an agreed time, we call out winners in one of two ways: 🏆

1. Based on [http://localhost:3000/stats](http://localhost:3000/stats)
2. Go to [http://localhost:3000/bingo](http://localhost:3000/bingo) and pick three winners 🎰
3. Visit [http://localhost:3000/reset?token=supersecret](http://localhost:3000/reset?token=supersecret) or restart the service to reset stats. 🔄
3. Visit [http://localhost:3000/reset?token=YOUR_TOKEN](http://localhost:3000/reset?token=YOUR_TOKEN) or restart the service to reset stats. 🔄

## Contributing 📝

Expand Down Expand Up @@ -289,7 +301,7 @@ git push
## 🦎 Test data

Copy our stats.json.sample In case you need quickly some test data.
Please avoid pushing changes to public/stats.json. We have it in .gitignore for a reson. 💚
Please avoid pushing changes to public/stats.json. We have it in .gitignore for a reason. 💚

```bash
cd quiz
Expand Down
26 changes: 22 additions & 4 deletions src/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,28 @@ let resolvedStatsFilePath =
process.env.STATS_FILE_PATH || path.join(projectRoot, "data", "stats.json");
const STATS_FILE_PATH = resolvedStatsFilePath;

// RESET_TOKEN defaults to "supersecret" if not set in .env or if .env doesn't exist.
const RESET_TOKEN = envFileExists
? process.env.RESET_TOKEN || ""
: "supersecret";
function generateReadableToken() {
const words1 = ["Happy", "Blue", "Fast", "Lucky"];
const words2 = ["Horse", "Chicken", "Tiger", "Eagle"];
const num = Math.floor(Math.random() * 1000);

return (
words1[Math.floor(Math.random() * words1.length)] +
words2[Math.floor(Math.random() * words2.length)] +
num
);
}
let RESET_TOKEN = process.env.RESET_TOKEN;

if (!RESET_TOKEN) {
RESET_TOKEN = generateReadableToken();

console.log("=================================");
console.log("⚠️ No RESET_TOKEN provided!");
console.log("Generated RESET_TOKEN:", RESET_TOKEN);
console.log("Share this token with the operator/admin.");
console.log("=================================");
}

// controls used logo of the quiz
const THEME = process.env.THEME || "openSUSE";
Expand Down
Loading