Rework database storage

This commit is contained in:
Wingy 2020-11-03 20:16:23 -05:00
parent dde80814d0
commit 20a992da36
7 changed files with 960 additions and 237 deletions

3
.gitignore vendored
View file

@ -36,7 +36,8 @@ typings/
.DS_Store .DS_Store
# Database # Database
db/ data/
log.txt
# Session store # Session store
sessions/ sessions/

View file

@ -9,14 +9,15 @@ RUN npm install --production --silent
COPY . . COPY . .
ENV PORT 80 ENV PORT 80
ENV DB_EXPOSE_PORT 8080
EXPOSE 80 EXPOSE 80
RUN mkdir -p /data/db RUN mkdir -p /data/dbs
ENV DB_URL /data/db ENV DB_PREFIX /data/dbs/
RUN mkdir -p /data/sessions RUN mkdir -p /data/sessions
ENV SESSION_STORE /data/sessions ENV SESSION_STORE /data/sessions
ENV SECRET_DIRNAME /data ENV SECRET_DIRNAME /data
CMD npm start CMD Dockerstart.sh

10
Dockerstart.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
set -e
if [ -d /data/db ]; then
mkdir -p /data/dbs
mv /data/db /data/dbs/users
echo "Migrated database"
fi
npm start

View file

@ -3,9 +3,10 @@ require('dotenv').config();
const yesNo = require('yes-no'); const yesNo = require('yes-no');
module.exports = { module.exports = {
dbUrl: process.env.DB_URL || 'db', dbPrefix: process.env.DB_URL || 'dbs/',
defaultFailureRedirect: process.env.DEFAULT_FAILURE_REDIRECT || '/login', defaultFailureRedirect: process.env.DEFAULT_FAILURE_REDIRECT || '/login',
port: Number(process.env.PORT) || 3000, port: Number(process.env.PORT) || 3000,
dbExposePort: Number(process.env.DB_EXPOSE_PORT) || null,
proxyServer: process.env.PROXY_SERVER || undefined, proxyServer: process.env.PROXY_SERVER || undefined,
secret: process.env.SECRET || require('./secret'), secret: process.env.SECRET || require('./secret'),
sessionStore: process.env.SESSION_STORE || './sessions', sessionStore: process.env.SESSION_STORE || './sessions',

View file

@ -6,19 +6,26 @@ const bcrypt = require('bcrypt-nodejs');
const flash = require('connect-flash'); const flash = require('connect-flash');
const passport = require('passport'); const passport = require('passport');
const express = require('express'); const express = require('express');
const PouchDB = require('pouchdb');
const level = require('level'); const level = require('level');
const config = require('./config'); const config = require('./config');
_CC.config = config _CC.config = config
if (!config.dbPrefix.startsWith('http')) {
const mkdirp = require('mkdirp').sync
mkdirp(config.dbPrefix)
}
const PouchDB = require('pouchdb').defaults({ prefix: config.dbPrefix });
const logger = require('./logger'); const logger = require('./logger');
const { dbExposePort } = require('./config');
const app = express(); const app = express();
app.set('base', config.base) app.set('base', config.base)
app.set('trust proxy', config.trustProxy) app.set('trust proxy', config.trustProxy)
const db = new PouchDB(config.dbUrl); const db = new PouchDB('users');
passport.use('local', new LocalStrategy( passport.use('local', new LocalStrategy(
(username, password, done) => { (username, password, done) => {
@ -76,3 +83,10 @@ app.set('view engine', 'pug');
app.use(config.base, require('./routes')({ db, config })); app.use(config.base, require('./routes')({ db, config }));
app.listen(config.port, () => logger.success('express', `Express server started on port ${config.port}!`)) app.listen(config.port, () => logger.success('express', `Express server started on port ${config.port}!`))
;(() => {
if (!dbExposePort) return
const dbExposeApp = express()
dbExposeApp.use('/', require('express-pouchdb')(PouchDB, { inMemoryConfig: true }));
dbExposeApp.listen(config.dbExposePort, () => logger.success('db expose', `DB has been exposed on port ${config.dbExposePort}`))
})()

View file

@ -22,10 +22,13 @@
"connect-flash": "^0.1.1", "connect-flash": "^0.1.1",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "4.17.1", "express": "4.17.1",
"express-pouchdb": "^4.2.0",
"express-session": "^1.15.6", "express-session": "^1.15.6",
"express-session-level": "^1.0.0", "express-session-level": "^1.0.0",
"get-product-name": "1", "get-product-name": "1",
"level": "^6.0.0", "level": "^6.0.0",
"memdown": "^5.1.0",
"mkdirp": "^0.5.5",
"moment": "^2.29.1", "moment": "^2.29.1",
"nanoid": "^3.1.16", "nanoid": "^3.1.16",
"passport": "^0.4.0", "passport": "^0.4.0",

1153
yarn.lock

File diff suppressed because it is too large Load diff