Skip to content

Commit 1c96fa6

Browse files
committed
change database to redis
1 parent 07b3410 commit 1c96fa6

19 files changed

+408
-632
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ NONCE_VALIDITY=60000
55
INITIAL_DIFFICULTY=13
66
BACKEND_URL="http://example.com"
77

8+
DATABASE_HOST=127.0.0.1
9+
DATABASE_PORT=6379
10+
DATABASE_PASSWORD=
11+
812
RATE_LIMIT=on
913
RATE_LIMIT_SAMPLE_MINUTES=60
1014
RATE_LIMIT_SESSION_THRESHOLD=100

CONFIGURE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ General Options
1414
- SESSION_KEY: secret key for cookie signatures, use a unique one for security reasons, or anyone can forge your signed cookies
1515
- BACKEND_URL: location to proxy authenticated traffic to, IP and URLs are both accepted(accepts protocol://url(:port) or protocol://ip(:port))
1616

17+
Database Options (Redis)
18+
- DATABASE_HOST: (default:127.0.0.1) redis service host
19+
- DATABASE_PORT: (default:6379) redis service port
20+
- DATABASE_PASSWORD: (default:null) redis service password
21+
1722
PoW Options
1823

1924
- POW: (default:on) toggles PoW functionality on/off (if not temporary switched off, why use this project at all?)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ environment variables
5555
- Ratelimiting
5656
- Unit Testing
5757
- WAF Implementation
58+
- Multi-Instance Syncing (Redis)
5859

5960
## Todos
6061
- [ ] Dynamic Difficulty
61-
- [ ] Multi-Instance Syncing
6262
- [ ] Monitoring
6363
- [ ] Captcha
6464

USAGE.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ cp -n .env.example .env
2222
nano .env
2323
```
2424

25-
Build and run
25+
Build
2626

2727
```
2828
npm run build
29+
```
30+
31+
Run with db (redis), recommended & faster
32+
```
33+
# install redis first
34+
# sudo apt-get install redis-server
2935
npm start
3036
```
3137

38+
Run without db (mock redis)
39+
40+
```
41+
npm run start-standalone
42+
```
43+
3244
Test functionalities(optional)
3345

3446
```
@@ -37,12 +49,18 @@ npm test
3749

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

40-
### docker run
52+
### docker run with db (redis), recommended & faster
4153

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

58+
### docker run without db (mock redis)
59+
60+
```
61+
docker run -p 3000:3000 -e BACKEND_URL="http://example.com" -e NODE_ENV="standalone" -d ruisiang/pow-shield
62+
```
63+
4664
### docker-compose
4765

4866
Configure docker-compose.example.yaml (more information in [configure section](CONFIGURE.md))

app.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import logger from 'koa-logger'
77
import { createProxyMiddleware } from 'http-proxy-middleware'
88
import c2k from 'koa2-connect'
99
import session from 'koa-session-minimal'
10-
10+
import redisStore from 'koa-redis'
1111
import config from './service/util/config-parser'
1212
import powRouter from './routes/pow-router'
1313
import testRouter from './routes/test-router'
@@ -24,6 +24,14 @@ app.use(
2424
signed: true,
2525
sameSite: 'strict',
2626
},
27+
store:
28+
process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'standalone'
29+
? undefined
30+
: redisStore({
31+
host: config.database_host,
32+
port: config.database_port,
33+
password: config.database_password,
34+
}),
2735
})
2836
)
2937

@@ -58,7 +66,7 @@ app.use(async (ctx, next) => {
5866
})
5967

6068
// service and routes
61-
if (process.env.NODE_ENV === 'test') {
69+
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'standalone') {
6270
app.use(testRouter.routes())
6371
}
6472
app.use(controller)

docker-compose.example.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ services:
1010
- NONCE_VALIDITY=60000
1111
- INITIAL_DIFFICULTY=13
1212
- BACKEND_URL="http://example.com"
13+
- DATABASE_HOST=redis
14+
- DATABASE_PORT=6379
15+
- DATABASE_PASSWORD=
1316
- RATE_LIMIT=on
1417
- RATE_LIMIT_SAMPLE_MINUTES=60
1518
- RATE_LIMIT_SESSION_THRESHOLD=100
@@ -24,3 +27,5 @@ services:
2427
- '3000'
2528
ports:
2629
- '3000:3000'
30+
redis:
31+
image: redis:alpine

0 commit comments

Comments
 (0)