Skip to content

Commit

Permalink
change database to redis
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiSiang committed Apr 8, 2021
1 parent 07b3410 commit 1c96fa6
Show file tree
Hide file tree
Showing 19 changed files with 408 additions and 632 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ NONCE_VALIDITY=60000
INITIAL_DIFFICULTY=13
BACKEND_URL="http://example.com"

DATABASE_HOST=127.0.0.1
DATABASE_PORT=6379
DATABASE_PASSWORD=

RATE_LIMIT=on
RATE_LIMIT_SAMPLE_MINUTES=60
RATE_LIMIT_SESSION_THRESHOLD=100
Expand Down
5 changes: 5 additions & 0 deletions CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ General Options
- SESSION_KEY: secret key for cookie signatures, use a unique one for security reasons, or anyone can forge your signed cookies
- BACKEND_URL: location to proxy authenticated traffic to, IP and URLs are both accepted(accepts protocol://url(:port) or protocol://ip(:port))

Database Options (Redis)
- DATABASE_HOST: (default:127.0.0.1) redis service host
- DATABASE_PORT: (default:6379) redis service port
- DATABASE_PASSWORD: (default:null) redis service password

PoW Options

- POW: (default:on) toggles PoW functionality on/off (if not temporary switched off, why use this project at all?)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ environment variables
- Ratelimiting
- Unit Testing
- WAF Implementation
- Multi-Instance Syncing (Redis)

## Todos
- [ ] Dynamic Difficulty
- [ ] Multi-Instance Syncing
- [ ] Monitoring
- [ ] Captcha

Expand Down
22 changes: 20 additions & 2 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@ cp -n .env.example .env
nano .env
```

Build and run
Build

```
npm run build
```

Run with db (redis), recommended & faster
```
# install redis first
# sudo apt-get install redis-server
npm start
```

Run without db (mock redis)

```
npm run start-standalone
```

Test functionalities(optional)

```
Expand All @@ -37,12 +49,18 @@ npm test

## Docker ([repo](https://hub.docker.com/repository/docker/ruisiang/pow-shield))

### docker run
### docker run with db (redis), recommended & faster

```
docker run -p 3000:3000 -e BACKEND_URL="http://example.com" -d ruisiang/pow-shield
```

### docker run without db (mock redis)

```
docker run -p 3000:3000 -e BACKEND_URL="http://example.com" -e NODE_ENV="standalone" -d ruisiang/pow-shield
```

### docker-compose

Configure docker-compose.example.yaml (more information in [configure section](CONFIGURE.md))
Expand Down
12 changes: 10 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import logger from 'koa-logger'
import { createProxyMiddleware } from 'http-proxy-middleware'
import c2k from 'koa2-connect'
import session from 'koa-session-minimal'

import redisStore from 'koa-redis'
import config from './service/util/config-parser'
import powRouter from './routes/pow-router'
import testRouter from './routes/test-router'
Expand All @@ -24,6 +24,14 @@ app.use(
signed: true,
sameSite: 'strict',
},
store:
process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'standalone'
? undefined
: redisStore({
host: config.database_host,
port: config.database_port,
password: config.database_password,
}),
})
)

Expand Down Expand Up @@ -58,7 +66,7 @@ app.use(async (ctx, next) => {
})

// service and routes
if (process.env.NODE_ENV === 'test') {
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'standalone') {
app.use(testRouter.routes())
}
app.use(controller)
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ services:
- NONCE_VALIDITY=60000
- INITIAL_DIFFICULTY=13
- BACKEND_URL="http://example.com"
- DATABASE_HOST=redis
- DATABASE_PORT=6379
- DATABASE_PASSWORD=
- RATE_LIMIT=on
- RATE_LIMIT_SAMPLE_MINUTES=60
- RATE_LIMIT_SESSION_THRESHOLD=100
Expand All @@ -24,3 +27,5 @@ services:
- '3000'
ports:
- '3000:3000'
redis:
image: redis:alpine
Loading

0 comments on commit 1c96fa6

Please sign in to comment.