Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Bring it back to life
Browse files Browse the repository at this point in the history
* Add dependabot for automatic updates
* Update Github action for running the linter
* Update Node in Docker
* Update Redis in Docker
* Replace faker with @faker-js/faker
* Make Port and Redis URL configurable
* Update all dependencies
  • Loading branch information
moonglum committed Jan 15, 2023
1 parent 181f03a commit 6f2e80a
Show file tree
Hide file tree
Showing 10 changed files with 4,368 additions and 957 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
- package-ecosystem: npm
directory: /
schedule:
interval: daily
26 changes: 0 additions & 26 deletions .github/workflows/node.js.yml

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: tests
on:
- push
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 18.x
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install-test
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14.15
FROM node:18-bullseye

# Environment:
# * We will put the app in /app
Expand All @@ -10,7 +10,7 @@ RUN mkdir "$APP_HOME" && \
npm install -g npm@latest

# Copy package.json and lock
COPY package*.json "$APP_HOME"
COPY --chown=node:node package*.json "$APP_HOME"

# Execute all commands in the app directory with the non-root user from now on
WORKDIR $APP_HOME
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Goals:
## Setup

```
docker-compose up
docker compose up
```

Go to "http://localhost:8000" in a browser in Chrome (won't work in Firefox stable).
Go to <http://localhost:8000>
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ services:
volumes:
- .:/app
- /app/node_modules
environment:
PORT: 8000
REDIS_URL: redis://redis:6379
ports:
- "8000:8000"
depends_on:
- redis

redis:
image: "redis:6.0"
image: "redis:7-bullseye"
ports:
- "6379:6379"
restart: always
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cons = require('consolidate')
const faker = require('faker')
const { faker } = require('@faker-js/faker')

const Redis = require('ioredis')
Redis.Command.setArgumentTransformer('xadd', xaddArgumentTransformer)
Redis.Command.setReplyTransformer('xread', xreadResultParser)

const port = process.env.PORT || '8000'
const redisURL = process.env.REDIS_URL || 'redis://localhost:6379'

app.set('views', path.resolve('views'))
app.engine('mustache', cons.mustache)
app.set('view engine', 'mustache')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.resolve('public')))

const producer = new Redis('redis://redis:6379')
const producer = new Redis(redisURL)

// 10 is an arbitrary number
app.get('/', async function (req, res) {
// TODO: Argument transformer and result parser
// This is a weird way of getting the last 10 messages
const user = faker.name.findName()
const user = faker.name.fullName()
let messages = await producer.xrevrange('messages', '+', '-', 'COUNT', 10)
messages = messages.reverse()

Expand All @@ -42,14 +45,14 @@ app.post('/messages', function (req, res) {
producer.xadd('messages', {
id: '*', // The * means: Determine the ID yourself
text: message,
user: user
user
})
res.redirect('/')
})

// This parameter is written into the template by Node
app.get('/update-stream', async function (req, res) {
const consumer = new Redis('redis://redis:6379')
const consumer = new Redis(redisURL)

res.writeHead(200, {
'Content-Type': 'text/event-stream',
Expand Down Expand Up @@ -78,8 +81,8 @@ app.get('/update-stream', async function (req, res) {
}
})

app.listen(8000)
console.log('App listening on 8000')
app.listen(port)
console.log(`App listening on ${port}`)

// Clean up these methods
function xreadResultParser (results) {
Expand Down
Loading

0 comments on commit 6f2e80a

Please sign in to comment.