Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #36 from nickdex/release/v0.3.1
Browse files Browse the repository at this point in the history
v0.3.1
  • Loading branch information
nickdex committed Feb 19, 2019
2 parents f209b16 + 3014c48 commit 9c89093
Show file tree
Hide file tree
Showing 39 changed files with 3,279 additions and 2,778 deletions.
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.git
.gitignore
node_modules
npm-debug.log
dist
app.log
*.sh
.vscode
.github
README.md
LICENSE
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Hello! If you're filing a bug, please include every step so as to help us reproduce it
on our machines. If you're unsure about how to file an issue, use the issue template. If
you need any help regarding usage of AIDA-Server, check out the documentation or hit us up
on chat.
You can ignore or delete this text, it is commented and won't appear when the issue is submitted or previewed.


Chat: t.me/nickdex
Issue Template: https://github.com/nickdex/AIDA-Server/.github/blob/master/ISSUE_TEMPLATE
Documentation: https://github.com/nickdex/AIDA-Server/blob/master/README.md
-->
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Thanks for your contribution!

Please take a quick look at those things down there. They're quite important.
Really! We wrote them for you. Yes you! With utmost care. Read them.
-->

**For short term contributors:** We understand that getting your commits well
defined like we require is a hard task and takes some learning. If you
look to help without wanting to contribute long term there's no need
for you to learn this. Just drop us a message and we'll take care of brushing
up your stuff for merge!

### Checklist

- [ ] I read the
[guidelines](https://github.com/nickdex/AIDA-Server/blob/master/README.md#contributing) and
I've followed them.

- [ ] I ran `yarn-lint` and it produced no errors.

- [ ] I ran `yarn-build` and it produced no errors.


5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dist/
# Optional REPL history
.node_repl_history

# Output of 'npm pack'
# Output of 'npm pack', also includes .yarn-cache.tgz
*.tgz

# Yarn Integrity file
Expand All @@ -62,3 +62,6 @@ env/.env

# Database file
data/

# QEMU binary for arm arch, for rpi docker builds
qemu-arm-static
16 changes: 13 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\server.js",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}"
},
{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 5858,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app",
"protocol": "inspector"
}
]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"cSpell.words": [
"autoload",
"cloudmqtt",
"mqtt",
"feathersjs",
"gcmapi",
"nedbdevice",
Expand Down
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node

WORKDIR /app

# Copy cache contents (if any) from local machine
ADD .yarn-cache.tgz package.json yarn.lock /

# Install packages
# Container Specific node packages at root (for remote debug)
RUN cd / && yarn && ln -s /node_modules /app/node_modules

ADD . /app

EXPOSE 80

CMD [ "yarn", "start" ]
18 changes: 18 additions & 0 deletions Rpi.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM arm32v7/node

COPY ./qemu-arm-static /usr/bin/qemu-arm-static

WORKDIR /app

# Copy cache contents (if any) from local machine
ADD .yarn-cache.tgz package.json yarn.lock /

# Install packages
# Container Specific node packages at root (for remote debug)
RUN cd / && yarn && ln -s /node_modules /app/node_modules

ADD . /app

EXPOSE 80

CMD [ "yarn", "start" ]
27 changes: 27 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

BUILD_MACHINE=$1
IMAGE_TAG="aida-server"
DOCKERFILE="Dockerfile"

# Init empty cache file
if [ ! -f .yarn-cache.tgz ]; then
echo "Init empty .yarn-cache.tgz"
tar cvzf .yarn-cache.tgz --files-from /dev/null
fi

if [ "$BUILD_MACHINE" == 'rpi' ]; then
IMAGE_TAG="$IMAGE_TAG-rpi"
DOCKERFILE="Rpi.Dockerfile"
echo "Rpi detected. Tag name - $IMAGE_TAG and Dockerfile - $DOCKERFILE"
fi

docker build -t nickdex/$IMAGE_TAG -f $DOCKERFILE .

docker run --rm --entrypoint cat nickdex/$IMAGE_TAG:latest /yarn.lock > /tmp/yarn.lock
if ! diff -q yarn.lock /tmp/yarn.lock > /dev/null 2>&1; then
echo "Saving Yarn cache"
docker run --rm --entrypoint tar nickdex/$IMAGE_TAG:latest czf - /usr/local/share/.cache/yarn/v4/ > .yarn-cache.tgz
echo "Saving yarn.lock"
cp /tmp/yarn.lock yarn.lock
fi
1 change: 1 addition & 0 deletions debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run -d --name server --network aidanetwork -p 3000:3000 -p 5858:5858 -v `pwd`:/app nickdex/aida-server yarn dev
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2'

services:
aida-server:
build: .
command: /node_modules/ts-node-dev/bin/ts-node-dev --inspect=0.0.0.0:5858 ./src/server.ts
volumes:
- .:/app
ports:
- '3000:3000'
- '5858:5858'
90 changes: 40 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,76 +1,66 @@
{
"name": "aida-server",
"version": "2.0.3",
"version": "0.3.1",
"private": true,
"description": "Server to control all requests directed to my IoT devices in home",
"main": "./dist/server.js",
"author": "Nikhil Warke",
"main": "src/server.ts",
"author": "Nikhil Warke <[email protected]>",
"license": "MIT",
"repository": "https://github.com/nickdex/AIDA-Server.git",
"bugs": {
"url": "https://github.com/nickdex/AIDA-Server/issues"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"start": "node dist/server.js",
"start": "ts-node src/server.ts",
"build": "tsc -p tsconfig.release.json",
"dev": "yarn run build && node dist/server.js",
"local": "yarn run build && yarn run serve",
"serve": "forever start dist/server.js ",
"stop": "forever stop dist/server.js",
"clean": "rimraf coverage dist tmp",
"watch": "tsc -w -p tsconfig.release.json",
"dev": "ts-node-dev --inspect=0.0.0.0:5858 ./src/server.ts",
"lint": "tslint -t stylish --project \"tsconfig.json\"",
"pretest": "yarn run lint",
"postinstall": "npm run build",
"test": "yarn run test-only",
"pretest": "yarn lint",
"test": "yarn test-only",
"test-only": "jest --coverage",
"test:watch": "jest --watch"
},
"dependencies": {
"@feathersjs/express": "^1.2.0",
"@feathersjs/feathers": "^3.1.7",
"axios": "^0.18.0",
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"dotenv": "^6.0.0",
"express": "^4.16.2",
"feathers-nedb": "^3.0.0",
"lodash": "^4.17.4",
"morgan": "^1.9.0",
"mqtt": "^2.18.1",
"nedb": "^1.8.0",
"pug": "^2.0.0-rc.4",
"@feathersjs/express": "^1.3.1",
"@feathersjs/feathers": "^3.3.1",
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"feathers-pouchdb": "alukito/feathers-pouchdb",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
"mqtt": "^2.18.8",
"pouchdb-adapter-http": "^7.0.0",
"pouchdb-core": "^7.0.0",
"pouchdb-find": "^7.0.0",
"triple-beam": "^1.3.0",
"tslib": "~1.9.3",
"typescript": "^2.9.2",
"web-push": "^3.3.2",
"winston": "^3.0.0"
"typescript": "^3.3.3",
"web-push": "^3.3.3",
"winston": "^3.2.1"
},
"devDependencies": {
"@types/body-parser": "^1.16.8",
"@types/body-parser": "^1.17.0",
"@types/cors": "^2.8.4",
"@types/dotenv": "^4.0.2",
"@types/express": "^4.0.35",
"@types/feathersjs__express": "^1.1.2",
"@types/feathersjs__feathers": "^3.0.0",
"@types/jest": "^23.1.1",
"@types/lodash": "^4.14.110",
"@types/dotenv": "^6.1.0",
"@types/feathersjs__express": "^1.1.4",
"@types/feathersjs__feathers": "^3.1.1",
"@types/jest": "^24.0.0",
"@types/lodash": "^4.14.120",
"@types/morgan": "^1.7.35",
"@types/nedb": "^1.8.5",
"@types/node": "^10.3.5",
"@types/shelljs": "^0.8.0",
"@types/node": "^10.12.24",
"@types/pouchdb": "^6.3.2",
"@types/pouchdb-adapter-http": "^6.1.2",
"@types/pouchdb-core": "^7.0.1",
"@types/pouchdb-find": "^6.3.3",
"@types/triple-beam": "^1.3.0",
"concurrently": "^3.6.0",
"forever": "^0.15.3",
"jest": "^23.1.0",
"nodemon": "^1.13.0",
"shelljs": "^0.8.1",
"ts-jest": "^22.0.1",
"ts-node": "^7.0.0",
"tslint": "^5.8.0",
"tslint-microsoft-contrib": "~5.0.2",
"tsutils": "^2.21.0"
"jest": "^24.1.0",
"ts-jest": "^23.10.5",
"ts-node": "^8.0.2",
"ts-node-dev": "^1.0.0-pre.32",
"tslint": "^5.12.1",
"tslint-microsoft-contrib": "~6.0.0"
}
}
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run -d --name=server --net=aidanet --restart=always --env PORT=80 --env MQTT_URL='http://mqtt:1883' --env DATABASE_URL='http://database' --env CORS_CLIENT_WHITELIST_URLS='["http://dashboard", "http://192.168.0.100"]' nickdex/aida-server
41 changes: 27 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express, { rest } from '@feathersjs/express';
// tslint:disable-next-line match-default-export-name
import express from '@feathersjs/express';
import feathers from '@feathersjs/feathers';
import * as cors from 'cors';
import * as dotenv from 'dotenv';
import * as morgan from 'morgan';
import * as path from 'path';

import { logger } from './logger';

Expand All @@ -17,40 +17,49 @@ import { Mqtt } from './iot/mqtt';
import * as agentController from './controllers/agent';
import { PushController } from './controllers/push';

// Services
import { clientHooks, ClientService } from './services/client';
import { iotDeviceHooks, IotDeviceService } from './services/iot-device';
import { UserService } from './services/user';
// Hooks
import { clientHooks } from './client-device/client-hook';
import { iotDeviceHooks } from './iot-device/iot-device-hook';

// Database
import { databaseService } from './database';

// Create Express server
const app = express(feathers());
logger.verbose('Express app created using feathers');

const whitelist = JSON.parse(process.env.CORS_CLIENT_WHITELIST_URLS);

// #region Express configuration
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'pug');
app.use(morgan('dev', { stream: { write: msg => logger.info(msg) } }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.errorHandler());
app.use(
cors({
origin: process.env.CORS_CLIENT_WHITELIST_URL
origin: (origin, callback) => {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(undefined, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
})
);
// #endregion

app.configure(rest());
app.configure(express.rest());

Mqtt.init();
logger.info('Mqtt Initialized');

// #region Service Registration
app.use('/clients', new ClientService());
app.use('/devices', new IotDeviceService());
app.use('/users', new UserService());
app.use('/clients', databaseService('users'));
app.use('/users', databaseService('users'));

app.use('/devices', databaseService('devices'));
app.use('/groups', databaseService('groups'));

app.service('clients').hooks(clientHooks);
app.service('devices').hooks(iotDeviceHooks);
Expand All @@ -67,4 +76,8 @@ app.post('/push/click', PushController.click);
app.post('/agent', agentController.agent);
// #endregion

app.get('/', (req, res) => {
res.send('Hello from AIDA-Server');
});

export = app;
Loading

0 comments on commit 9c89093

Please sign in to comment.