make deployable

This commit is contained in:
41666 2017-12-29 10:12:54 -06:00
parent eaa1167f16
commit 309aee427e
15 changed files with 317 additions and 886 deletions

3
.dockerignore Normal file
View file

@ -0,0 +1,3 @@
/Server/.env
/Server/node_modules
/UI/node_modules

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
Server/\.env
/docker-compose.test.yml

13
Dockerfile Normal file
View file

@ -0,0 +1,13 @@
FROM node:9.3 AS builder
ENV NODE_ENV production
RUN npm i -g yarn
COPY . /src
RUN cd /src/UI && yarn && yarn build && cd /src/Server && yarn && mkdir public && mv /src/UI/build/* public
FROM mhart/alpine-node:9.3
ENV NODE_ENV production
WORKDIR /dist
EXPOSE 6769
RUN npm i -g pm2
COPY --from=builder /src/Server /dist
CMD pm2-docker index.js

1
Server/.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules
.data
.env
public

View file

@ -1,12 +1,14 @@
const log = new (require('../logger'))('api/index')
const glob = require('glob')
const PROD = process.env.NODE_ENV === 'production'
module.exports = async (router, ctx) => {
const apis = glob.sync('./api/**/!(index).js')
const apis = glob.sync(`./api/**/!(index).js`)
log.debug('found apis', apis)
for (let a of apis) {
if (a.endsWith('_test.js') !== null && process.env.NODE_ENV !== 'development') {
if (a.endsWith('_test.js') && PROD) {
log.debug(`skipping ${a}`)
continue
}

View file

@ -5,6 +5,7 @@ const http = require('http')
const Koa = require('koa')
const app = new Koa()
const _io = require('socket.io')
const path = require('path')
const router = require('koa-better-router')().loadMethods()
const Roleypoly = require('./Roleypoly')
@ -34,6 +35,28 @@ async function start () {
const bodyParser = require('koa-bodyparser')
app.use(bodyParser({ types: ['json'] }))
// Compress
const compress = require('koa-compress')
app.use(compress())
// SPA + Static
if (process.env.NODE_ENV === 'production') {
const pub = path.join(__dirname, 'public')
const staticFiles = require('koa-static')
app.use(staticFiles(pub, { defer: true }))
const send = require('koa-send')
app.use(async (ctx, next) => {
if (ctx.path.startsWith('/api')) {
return next()
}
await next()
send(ctx, 'index.html', { root: pub })
})
}
// Request logger
app.use(async (ctx, next) => {
let timeStart = new Date()
@ -60,15 +83,18 @@ async function start () {
app.use(session({
key: 'roleypoly:sess',
maxAge: 'session',
siteOnly: true,
store: M.ctx.sessions
}, app))
await M.mountRoutes()
// SPA server
log.info(`starting HTTP server on ${process.env.APP_PORT || 6769}`)
server.listen(process.env.APP_PORT || 6769)
}
start().catch(e => {
console.error(e)
log.fatal('app failed to start', e)
})

View file

@ -15,6 +15,8 @@ class Logger {
if (typeof data[data.length - 1] === 'number') {
process.exit(data[data.length - 1])
} else {
process.exit(1)
}
throw text

View file

@ -10,34 +10,29 @@
},
"dependencies": {
"chalk": "^2.3.0",
"commander": "^2.12.2",
"discord.js": "^11.2.1",
"dotenv": "^4.0.0",
"erlpack": "github:discordapp/erlpack",
"eslint": "^4.12.1",
"eslint-config-standard": "^10.2.1",
"eventemitter3": "^3.0.0",
"eslint": "^4.14.0",
"eslint-config-standard": "^11.0.0-beta.0",
"glob": "^7.1.2",
"immutable": "^3.8.2",
"inquirer": "^4.0.1",
"koa": "^2.4.1",
"koa-better-router": "^2.1.1",
"koa-bodyparser": "^4.2.0",
"koa-passport": "^4.0.1",
"koa-send": "^4.1.2",
"koa-session": "^5.5.1",
"koa-compress": "^2.0.0",
"koa-static": "^4.0.2",
"ksuid": "^0.4.0",
"lru-cache": "^4.1.1",
"passport-discord": "^0.1.3",
"passport-oauth2-refresh": "^1.0.0",
"pg": "^7.4.0",
"pg-hstore": "^2.3.2",
"pm2": "^2.8.0",
"sequelize": "^4.26.0",
"pm2": "^2.9.1",
"sequelize": "^4.28.6",
"socket.io": "^2.0.4",
"standard": "^10.0.3",
"superagent": "^3.8.1",
"superagent": "^3.8.2",
"uuid": "^3.1.0",
"uws": "^9.14.0",
"yargs": "^10.0.3"
"uws": "^9.14.0"
}
}

View file

@ -12,8 +12,13 @@ class DiscordService extends Service {
this.oauthCallback = process.env.OAUTH_AUTH_CALLBACK
this.botCallback = `${ctx.config.appUrl}/api/oauth/bot/callback`
this.appUrl = process.env.APP_URL
this.isBot = process.env.IS_BOT === 'true' || false
this.rootUsers = new Set((process.env.ROOT_USERS||'').split(','))
this.client = new discord.Client()
this.client.options.disableEveryone = true
this.cmds = this._cmds()
this.startBot()
}
@ -25,7 +30,11 @@ class DiscordService extends Service {
async startBot () {
await this.client.login(this.botToken)
this.client.on('message', this.handleMessage.bind(this))
// not all roleypolys are bots.
if (this.isBot) {
this.log.info('this roleypoly is a bot')
this.client.on('message', this.handleMessage.bind(this))
}
for (let server of this.client.guilds.array()) {
await this.ctx.server.ensure(server)
@ -135,15 +144,77 @@ class DiscordService extends Service {
message.channel.send(`🔰 Assign your roles here! <${this.appUrl}/s/${message.guild.id}>`, { disableEveryone: true })
}
_cmds () {
const cmds = [
{
regex: /say (.*)/,
handler (message, matches, r) {
r(matches[0])
}
},
{
regex: /set username (.*)/,
async handler (message, matches) {
const { username } = this.client.user
await this.client.user.setUsername(matches[0])
message.channel.send(`Username changed from ${username} to ${matches[0]}`)
}
},
{
regex: /stats/,
async handler (message, matches) {
const t = [
`**Stats** 📈`,
'',
`👩‍❤️‍👩 **Users Served:** ${this.client.guilds.reduce((acc, g) => acc + g.memberCount, 0)}`,
`🔰 **Servers:** ${this.client.guilds.array().length}`
]
message.channel.send(t.join('\n'))
}
}
]
// prefix regex with ^ for ease of code
.map(({regex, ...rest}) => ({ regex: new RegExp(`^${regex.source}`, regex.flags), ...rest }))
return cmds
}
async handleCommand (message) {
const cmd = message.content.replace(`<@${this.client.user.id}> `, '')
this.log.debug(`got command from ${message.author.username}`, cmd)
for (let { regex, handler } of this.cmds) {
const match = regex.exec(cmd)
if (match !== null) {
this.log.debug('command accepted', { cmd, match })
try {
await handler.call(this, message, match.slice(1))
return
} catch (e) {
this.log.error('command errored', { e, cmd, message })
message.channel.send(`❌ **An error occured.** ${e}`)
return
}
}
}
// nothing matched?
this.mentionResponse(message)
}
handleMessage (message) {
if (message.author.bot && message.channel.type !== 'text') { // drop bot messages and dms
return
}
if (message.mentions.users.has(this.client.user.id)) {
this.mentionResponse(message)
if (this.rootUsers.has(message.author.id)) {
this.handleCommand.call(this, message)
} else {
this.mentionResponse.call(this, message)
}
}
}
}
module.exports = DiscordService

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,8 @@
"dependencies": {
"color": "^2.0.1",
"custom-react-scripts": "0.2.1",
"eslint": "^4.14.0",
"history": "^4.7.2",
"immutable": "^3.8.2",
"ksuid": "^0.4.0",
"prop-types": "^15.6.0",
"react": "^16.2.0",
"react-custom-scrollbars": "^4.2.1",
@ -20,15 +18,11 @@
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-router-redux": "^5.0.0-alpha.8",
"react-transition-group": "^2.2.1",
"redux": "^3.7.2",
"redux-devtools": "^3.4.1",
"redux-devtools-dock-monitor": "^1.1.3",
"redux-devtools-log-monitor": "^1.4.0",
"redux-logger": "^3.0.6",
"redux-saga": "^0.16.0",
"redux-thunk": "^2.2.0",
"superagent": "^3.8.2"
"superagent": "^3.8.2",
"uuid": "^3.1.0"
},
"scripts": {
"start": "react-scripts start",
@ -38,11 +32,16 @@
},
"proxy": "http://localhost:6769",
"devDependencies": {
"eslint": "^4.14.0",
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-react": "^7.5.1",
"eslint-plugin-standard": "^3.0.1"
"eslint-plugin-standard": "^3.0.1",
"redux-devtools": "^3.4.1",
"redux-devtools-dock-monitor": "^1.1.3",
"redux-devtools-log-monitor": "^1.4.0"
}
}

View file

@ -1,15 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" sizes="any" href="%PUBLIC_URL%/favicon.png"/>
<title>Roleypoly</title>
<!-- <script src="https://use.typekit.net/bck0pci.js"></script>
<script>try{Typekit.load({ async: true });}catch(e){}</script> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.35/css/uikit.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.35/js/uikit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.35/js/uikit-icons.min.js"></script>
<style>body{ background-color: #453F3E; }</style>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" sizes="any" href="%PUBLIC_URL%/favicon.png"/>
<title>Roleypoly</title>
<!-- <script src="https://use.typekit.net/bck0pci.js"></script>
<script>try{Typekit.load({ async: true });}catch(e){}</script> -->
<link rel="stylesheet" defer href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.35/css/uikit.min.css" />
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.35/js/uikit.min.js"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.35/js/uikit-icons.min.js"></script>
<style>body{ background-color: #453F3E; }</style>
</head>
<noscript>
You need to enable JavaScript to run this app.
</noscript>

View file

@ -1,7 +1,7 @@
import { Set } from 'immutable'
import * as UIActions from '../../actions/ui'
import { getViewMap } from '../role-picker/actions'
import ksuid from 'ksuid'
import uuidv4 from 'uuid/v4'
import superagent from 'superagent'
export const constructView = id => (dispatch, getState) => {
@ -72,7 +72,6 @@ export const saveCategory = (id, category) => (dispatch) => {
mode: Symbol.for('drop')
}
})
}
export const openEditor = (id) => ({
@ -108,7 +107,7 @@ export const deleteCategory = (id, category) => (dispatch, getState) => {
})
}
export const createCategory = async (dispatch, getState) => {
export const createCategory = (dispatch, getState) => {
const { roleEditor } = getState()
const vm = roleEditor.get('viewMap')
@ -119,7 +118,7 @@ export const createCategory = async (dispatch, getState) => {
name = `New Category ${idx}`
}
const id = (await ksuid.random()).string
const id = uuidv4()
dispatch({
type: Symbol.for('re: set category'),

View file

@ -1,6 +1,5 @@
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import { Scrollbars } from 'react-custom-scrollbars'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'

View file

@ -1542,10 +1542,6 @@ center-align@^0.1.1:
align-text "^0.1.3"
lazy-cache "^1.0.3"
chain-function@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/chain-function/-/chain-function-1.0.0.tgz#0d4ab37e7e18ead0bdc47b920764118ce58733dc"
chalk@1.1.3, chalk@^1.0.0, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@ -1604,10 +1600,6 @@ clap@^1.0.9:
dependencies:
chalk "^1.1.3"
classnames@^2.2.5:
version "2.2.5"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.5.tgz#fb3801d453467649ef3603c7d61a02bd129bde6d"
clean-css@4.1.x:
version "4.1.9"
resolved "https://registry.yarnpkg.com/clean-css/-/clean-css-4.1.9.tgz#35cee8ae7687a49b98034f70de00c4edd3826301"
@ -2337,10 +2329,6 @@ dom-css@^2.0.0:
prefix-style "2.0.1"
to-camel-case "1.0.0"
dom-helpers@^3.2.0:
version "3.3.1"
resolved "https://registry.yarnpkg.com/dom-helpers/-/dom-helpers-3.3.1.tgz#fc1a4e15ffdf60ddde03a480a9c0fece821dd4a6"
dom-serializer@0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82"
@ -2517,7 +2505,7 @@ error-ex@^1.2.0:
dependencies:
is-arrayish "^0.2.1"
es-abstract@^1.4.3, es-abstract@^1.7.0:
es-abstract@^1.7.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.10.0.tgz#1ecb36c197842a00d8ee4c2dfd8646bb97d60864"
dependencies:
@ -4573,12 +4561,6 @@ klaw@^1.0.0:
optionalDependencies:
graceful-fs "^4.1.9"
ksuid@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/ksuid/-/ksuid-0.4.0.tgz#e41f626b9f4dfcf2673f33a7ef69edc4fab99e1b"
dependencies:
string.prototype.padstart "^3.0.0"
latest-version@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/latest-version/-/latest-version-2.0.0.tgz#56f8d6139620847b8017f8f1f4d78e211324168b"
@ -6196,17 +6178,6 @@ react-router@^4.2.0:
prop-types "^15.5.4"
warning "^3.0.0"
react-transition-group@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/react-transition-group/-/react-transition-group-2.2.1.tgz#e9fb677b79e6455fd391b03823afe84849df4a10"
dependencies:
chain-function "^1.0.0"
classnames "^2.2.5"
dom-helpers "^3.2.0"
loose-envify "^1.3.1"
prop-types "^15.5.8"
warning "^3.0.0"
react@^16.2.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
@ -6357,10 +6328,6 @@ redux-logger@^3.0.6:
dependencies:
deep-diff "^0.3.5"
redux-saga@^0.16.0:
version "0.16.0"
resolved "https://registry.yarnpkg.com/redux-saga/-/redux-saga-0.16.0.tgz#0a231db0a1489301dd980f6f2f88d8ced418f724"
redux-thunk@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.2.0.tgz#e615a16e16b47a19a515766133d1e3e99b7852e5"
@ -7075,14 +7042,6 @@ string-width@^2.0.0, string-width@^2.1.0, string-width@^2.1.1:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"
string.prototype.padstart@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/string.prototype.padstart/-/string.prototype.padstart-3.0.0.tgz#5bcfad39f4649bb2d031292e19bcf0b510d4b242"
dependencies:
define-properties "^1.1.2"
es-abstract "^1.4.3"
function-bind "^1.0.2"
string_decoder@^1.0.0, string_decoder@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"