Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add push based API capabilities #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,38 @@ jobs:
- store_test_results:
path: testreports
- run: npm run codecov
- setup_remote_docker
- run:
name: Install dependencies
command: |
apk add --no-cache \
py-pip=9.0.0-r1
pip install \
docker-compose==1.12.0 \
awscli==1.11.76
- restore_cache:
keys:
- v1-{{ .Branch }}
paths:
- /caches/app.tar
- run:
name: Load Docker image layer cache
command: |
set +o pipefail
docker load -i /caches/app.tar | true
- run:
name: Build application Docker image
command: |
docker build --cache-from=app -t app .
- run:
name: Save Docker image layer cache
command: |
mkdir -p /caches
docker save -o /caches/app.tar app
- save_cache:
key: v1-{{ .Branch }}-{{ epoch }}
paths:
- /caches/app.tar

workflows:

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Base image
FROM alpine:3.12 AS base
FROM alpine:3.14.2 AS base
MAINTAINER Frank Bille-Stauner <jotajoti@frankbille.dk>

RUN apk add --no-cache nodejs-current=14.3.0-r0 npm
RUN apk add --no-cache nodejs-current npm

WORKDIR /jid-server

Expand Down
8 changes: 7 additions & 1 deletion app/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

//ExpressJS for rest API
import express from 'express';
import {createServer} from 'http';
import socketio from 'socket.io';
import cors from 'cors';
import * as Sentry from '@sentry/node';
import * as Apm from '@sentry/apm';
Expand All @@ -10,6 +12,7 @@ import * as config from './config.js';
import * as users from './users.js';
import * as stats from './stats.js';
import * as jid from './jid.js';
import {ServerPush} from "./push";

export async function startServer(args) {
if (!args) { args = {} }
Expand All @@ -20,6 +23,8 @@ export async function startServer(args) {
}

const app = express();
const http = createServer(app);
const io = socketio(http);

Sentry.init({
dsn: 'https://5810e3ec687d4e3b986eb158a0c24a8b@sentry.billestauner.dk/3',
Expand All @@ -38,6 +43,7 @@ export async function startServer(args) {
app.use(express.json());
app.use(function (req, res, next) {
res.locals.db = args.database;
res.locals.push = new ServerPush(io);
next()
});

Expand All @@ -51,7 +57,7 @@ export async function startServer(args) {

app.use(Sentry.Handlers.errorHandler());

app.listen(port, () => {
http.listen(port, () => {
if (config.isLoggingInfo()) {
console.log(`Server running on port ${port}!`);
}
Expand Down
20 changes: 19 additions & 1 deletion app/jid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import moment from 'moment';
import * as users from './users.js';
import * as config from './config.js';
import * as stats from "./stats";

const countries = new Map();

Expand Down Expand Up @@ -87,6 +88,23 @@ export async function save(req, res) {
}

res.send(result);

// Send socket message with new stats
// This code could be simpler if loading of stats was decoupled from the REST controller.
if (result.saved) {
const fakeRes = {
locals: {
db: res.locals.db
},
send(response) {
this.stats = response;
}
}

await stats.getStats(null, fakeRes);

res.locals.push.stats(fakeRes.stats);
}
}

async function getCode(database, userid, jid) {
Expand All @@ -106,4 +124,4 @@ async function loadCountries(database) {
});
}
}
}
}
10 changes: 10 additions & 0 deletions app/push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

export class ServerPush {
constructor(io) {
this._io = io;
}

stats(stats) {
this._io.sockets.emit('stats', stats);
}
}
Loading