Skip to content

Commit

Permalink
Add database and jwt file locations as environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
oamaok committed Oct 17, 2021
1 parent 12c68c2 commit 6091933
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
dist
data/database.sqlite3
data/
cert
9 changes: 9 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const isProduction = process.env.NODE_ENV === 'production'
const recursiveCopy = async (srcDir, destDir) => {
const absoluteSrc = path.resolve(__dirname, srcDir)
const absoluteDest = path.resolve(__dirname, destDir)

try {
await fs.mkdir(absoluteDest)
} catch (err) {}

let entryStack = await fs.readdir(srcDir)
let entry

Expand Down Expand Up @@ -102,6 +107,10 @@ ${worklets.map((worklet) => ` ${worklet}: typeof ${worklet}`).join('\n')}
}

;(async () => {
try {
await fs.mkdir('./dist')
} catch (err) {}

try {
await Promise.all([buildWorklets(), buildClient()])
} catch (err) {
Expand Down
8 changes: 8 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const path = require('path')
const os = require('os')

module.exports = {
apps: [
{
name: 'modulate-server',
script: 'yarn',
args: 'ts-node ./server/src/index.ts',
env_production: {
NODE_ENV: 'production',
DATABASE_FILE: path.resolve(os.homedir(), 'database.sqlite3'),
JWT_KEY_FILE: path.resolve(os.homedir(), 'jwt.key'),
},
},
],
}
2 changes: 1 addition & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ mv modulate modulate.previous
mv build modulate

cd modulate
pm2 restart ecosystem.config.js
pm2 restart ecosystem.config.js --env production
13 changes: 12 additions & 1 deletion server/src/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import crypto from 'crypto'
import jwt from 'jsonwebtoken'
import fs from 'fs'
import path from 'path'
import * as validators from '../../common/validators'
import { User } from '../../common/types'

const key = process.env.JWT_KEY ?? 'development key'
const keyFile =
process.env.JWT_KEY_FILE ?? path.resolve(__dirname, '../../data/jwt.key')

try {
fs.statSync(keyFile)
} catch (err) {
fs.writeFileSync(keyFile, crypto.randomBytes(256))
}
const key = fs.readFileSync(keyFile)

export const createToken = (user: User): string => {
return jwt.sign(user, key)
Expand Down
3 changes: 2 additions & 1 deletion server/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import argon2 from 'argon2'
import { Patch, User, UserLogin, UserRegistration } from '../../common/types'

const db = new (sqlite.verbose().Database)(
path.resolve(__dirname, '../../data/database.sqlite3')
process.env.DATABASE_FILE ??
path.resolve(__dirname, '../../data/database.sqlite3')
)

export const query = (query: SQLStatement) => {
Expand Down

0 comments on commit 6091933

Please sign in to comment.