Move to express as we need a bit more finess

This commit is contained in:
Andrea Maria Piana 2018-06-28 08:47:06 +02:00
parent d44fcb3efe
commit 155354c588
16 changed files with 3279 additions and 20 deletions

View File

@ -1,3 +1,7 @@
Dockerfile
.dockerignore
README.md
tests/
node_modules
npm-debug.log
.git

26
.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
# OS X
.DS_Store*
Icon?
._*
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
.directory
*~
# npm
node_modules
*.log
*.gz
# Coveralls
coverage
# Benchmarking
benchmarks/graphs

View File

@ -1,4 +1,14 @@
FROM nginx
FROM node:9-alpine
ADD etc/nginx.conf /etc/nginx/nginx.conf
ADD src /var/www
RUN mkdir -p /srv
ADD package.json /srv
ADD package-lock.json /srv
WORKDIR /srv
RUN npm install
ADD . /srv
CMD npm start

39
app.js Normal file
View File

@ -0,0 +1,39 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;

90
bin/www Executable file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('universal-links-handler:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

View File

@ -1,7 +1,11 @@
version: '3'
services:
app:
image: nginx
image: node:9-alpine
working_dir: /srv
volumes:
- ./etc/nginx.conf:/etc/nginx/nginx.conf
- ./src:/var/www
- ./:/srv
- node_modules:/srv/node_modules
command: npm run watch
volumes:
node_modules:

View File

@ -3,5 +3,7 @@ services:
app:
build:
context: .
environment:
PORT: 80
ports:
- 8080:80

View File

@ -1,14 +0,0 @@
events {
worker_connections 1024;
}
http {
server {
root /var/www;
include /etc/nginx/mime.types;
location / {
}
}
}

3016
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "universal-links-handler",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"watch": "nodemon ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"ejs": "~2.5.7",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"morgan": "~1.9.0"
},
"devDependencies": {
"nodemon": "^1.17.5"
}
}

View File

@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}

26
routes/index.js Normal file
View File

@ -0,0 +1,26 @@
var express = require('express');
var router = express.Router();
var assetLinks = require('../resources/assetlinks.json');
router.get('/.well-known/assetlinks.json', function(req, res) {
res.json(assetLinks);
});
router.get('/chat/:chatType/:chatId', function(req, res, next) {
res.render('index', {
title: 'Status.im join ' + req.params.chatId + ' chat',
chatId: req.params.chatId,
path: req.originalUrl
});
});
router.get('/user/:userId', function(req, res, next) {
res.render('index', {
title: 'Status.im view ' + req.params.userId + ' profile',
chatId: req.params.userId,
path: req.originalUrl
});
});
module.exports = router;

View File

@ -5,3 +5,9 @@ shakedown GET /.well-known/assetlinks.json
status 200
content_type 'application/json'
contains 'sha256_cert_fingerprints'
shakedown GET /chat/public/abc
status 200
shakedown GET /user/blah
status 200

3
views/error.ejs Normal file
View File

@ -0,0 +1,3 @@
<h1><%= message %></h1>
<h2><%= error.status %></h2>
<pre><%= error.stack %></pre>

19
views/index.ejs Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<meta property="al:ios:url" content="app://get.status.im<%= path %>" />
<meta property="al:ios:app_store_id" content="1178893006" />
<meta property="al:ios:app_name" content="Status — Ethereum. Anywhere" />
<meta property="al:android:url" content="app://get.status.im<%= path %>" />
<meta property="al:android:package" content="im.status.ethereum" />
<meta property="al:android:app_name" content="Status — Ethereum. Anywhere" />
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
</body>
</html>