-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'william/project-structure'
- Loading branch information
Showing
26 changed files
with
594 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
.idea | ||
node_modules | ||
# Build output: | ||
/lib/ | ||
/logs/ | ||
/serverConfig.json | ||
/pushServerConfig.json | ||
|
||
build | ||
serverConfig.json | ||
tsconfig.tsbuildinfo | ||
logs | ||
# Package managers: | ||
node_modules/ | ||
npm-debug.log | ||
package-lock.json | ||
yarn-error.log | ||
|
||
# Editors: | ||
.DS_Store | ||
.idea/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,54 @@ | ||
edge-notifications | ||
# edge-push-server | ||
|
||
This server sends push notifications to Edge client apps. It contains an HTTP server that clients can use to register for notifications, and a background process that checks for price changes and actually sends the messages. | ||
|
||
## Setup | ||
|
||
This server requires a working copies of Node.js, Yarn, PM2, and CouchDB. We also recommend using Caddy to terminate SSL connections. | ||
|
||
### Set up logging | ||
|
||
Run these commands as a server admin: | ||
|
||
```sh | ||
touch /var/log/pushServer.log | ||
touch /var/log/priceDaemon.log | ||
chown edgy /var/log/pushServer.log /var/log/priceDaemon.log | ||
cp ./docs/logrotate /etc/logrotate.d/pushServer | ||
``` | ||
|
||
### Manage server using `pm2` | ||
|
||
First, tell pm2 how to run the server script: | ||
|
||
```sh | ||
# install: | ||
pm2 start pm2.json | ||
pm2 save | ||
|
||
# check status: | ||
pm2 monit | ||
tail -f /var/log/pushServer.log | ||
tail -f /var/log/priceDaemon.log | ||
|
||
# manage: | ||
pm2 reload pm2.json | ||
pm2 restart pm2.json | ||
pm2 stop pm2.json | ||
|
||
pm2 restart pushServer // Just the HTTP server | ||
pm2 restart priceDaemon // Just the price checker | ||
``` | ||
|
||
### Updating | ||
|
||
To update the code running on the production server, use the following procedure: | ||
|
||
```sh | ||
git pull | ||
yarn | ||
yarn prepare | ||
pm2 restart pm2.json | ||
``` | ||
|
||
Each deployment should come with its own version bump, changelog update, and git tag. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# /etc/logrotate.d/pushServer | ||
|
||
/var/log/pushServer.log { | ||
copytruncate | ||
daily | ||
missingok | ||
notifempty | ||
rotate 10 | ||
} | ||
|
||
/var/log/priceDaemon.log { | ||
copytruncate | ||
daily | ||
missingok | ||
notifempty | ||
rotate 10 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apps": [ | ||
{ | ||
"name": "pushServer", | ||
"script": "./lib/server/index.js", | ||
"out_file": "/var/log/pushServer.log" | ||
}, | ||
{ | ||
"name": "priceDaemon", | ||
"script": "./lib/price-script/index.js", | ||
"out_file": "/var/log/priceDaemon.log" | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { asArray, asMaybe, asNumber, asObject, asString } from 'cleaners' | ||
import { | ||
asReplicatorSetupDocument, | ||
DatabaseSetup, | ||
setupDatabase, | ||
SetupDatabaseOptions, | ||
syncedDocument | ||
} from 'edge-server-tools' | ||
import { ServerScope } from 'nano' | ||
|
||
import { serverConfig } from './serverConfig' | ||
|
||
// --------------------------------------------------------------------------- | ||
// Synced documents | ||
// --------------------------------------------------------------------------- | ||
|
||
/** | ||
* Live-updating server options stored in the `push-settings` database. | ||
*/ | ||
const asSettings = asObject({ | ||
apiKeys: asMaybe( | ||
asArray( | ||
asObject({ | ||
name: asString, | ||
apiKey: asString | ||
}) | ||
), | ||
[] | ||
), | ||
priceCheckInMinutes: asMaybe(asNumber, 5) | ||
}) | ||
|
||
export const syncedReplicators = syncedDocument( | ||
'replicators', | ||
asReplicatorSetupDocument | ||
) | ||
|
||
export const syncedSettings = syncedDocument('settings', asSettings.withRest) | ||
|
||
// --------------------------------------------------------------------------- | ||
// Databases | ||
// --------------------------------------------------------------------------- | ||
|
||
export const settingsSetup: DatabaseSetup = { | ||
name: 'push-settings', | ||
syncedDocuments: [syncedReplicators, syncedSettings] | ||
} | ||
|
||
const apiKeysSetup: DatabaseSetup = { name: 'db_api_keys' } | ||
|
||
const thresholdsSetup: DatabaseSetup = { name: 'db_currency_thresholds' } | ||
|
||
const devicesSetup: DatabaseSetup = { name: 'db_devices' } | ||
|
||
const usersSetup: DatabaseSetup = { | ||
name: 'db_user_settings' | ||
// documents: { | ||
// '_design/filter': makeJsDesign('by-currency', ?), | ||
// '_design/map': makeJsDesign('currency-codes', ?) | ||
// } | ||
} | ||
|
||
const defaultsSetup: DatabaseSetup = { | ||
name: 'defaults' | ||
// syncedDocuments: ['thresholds'] | ||
} | ||
|
||
// --------------------------------------------------------------------------- | ||
// Setup routine | ||
// --------------------------------------------------------------------------- | ||
|
||
export async function setupDatabases( | ||
connection: ServerScope, | ||
disableWatching: boolean = false | ||
): Promise<void> { | ||
const { currentCluster } = serverConfig | ||
const options: SetupDatabaseOptions = { | ||
currentCluster, | ||
replicatorSetup: syncedReplicators, | ||
disableWatching | ||
} | ||
|
||
await setupDatabase(connection, settingsSetup, options) | ||
await Promise.all([ | ||
setupDatabase(connection, apiKeysSetup, options), | ||
setupDatabase(connection, thresholdsSetup, options), | ||
setupDatabase(connection, devicesSetup, options), | ||
setupDatabase(connection, usersSetup, options), | ||
setupDatabase(connection, defaultsSetup, options) | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.