feat(api-express): initial commit
This commit is contained in:
parent
51350eb879
commit
e8616d852d
|
@ -0,0 +1,111 @@
|
|||
# Other
|
||||
*~
|
||||
.env~
|
||||
f
|
||||
p
|
||||
.env*
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
.env.test
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and *not* Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
|
@ -0,0 +1,29 @@
|
|||
const DEBUG=require('debug')('index');
|
||||
const HTTPS = require('https');
|
||||
const FS = require('fs');
|
||||
|
||||
DEBUG('index start...');
|
||||
const APP=require('./src/api');
|
||||
|
||||
//TODO make port available via config
|
||||
//set port
|
||||
const PORT=parseInt(process.env.PORT, 10)||65535;
|
||||
DEBUG('PORT: '+PORT);
|
||||
|
||||
//TODO make env available via config
|
||||
//pass 'APP' to server
|
||||
DEBUG('NODE_ENV: '+process.env.NODE_ENV);
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
DEBUG('development mode');
|
||||
APP.listen(PORT);
|
||||
}else{
|
||||
DEBUG('production mode');
|
||||
HTTPS.createServer({
|
||||
//TODO make key and cert available via config
|
||||
key: FS.readFileSync('./p'),
|
||||
cert: FS.readFileSync('./f')
|
||||
}, APP)
|
||||
.listen(PORT, ()=>DEBUG('listening on port '+PORT));
|
||||
}
|
||||
|
||||
DEBUG('index done.');
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"private": true,
|
||||
"name": "delfi-api",
|
||||
"description": "API to request data from database",
|
||||
"version": "0.0.1",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"public",
|
||||
"transport",
|
||||
"mobility",
|
||||
"api"
|
||||
],
|
||||
"author": "Software Ingenieur Begerad <dialog@SwIngBe.de>",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "nodemon index.js",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"compression": "1.7.4",
|
||||
"cors": "2.8.5",
|
||||
"debug": "4.3.4",
|
||||
"dotenv": "16.0.3",
|
||||
"express": "4.18.2",
|
||||
"fs": "0.0.1-security",
|
||||
"helmet": "5.1.1",
|
||||
"https": "1.0.0",
|
||||
"pg": "8.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "2.0.22"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
# init
|
||||
|
||||
```
|
||||
mkdir ~/git && cd ~/git
|
||||
git clone -b main <repository>
|
||||
cd <repository>/api
|
||||
npm i
|
||||
```
|
||||
|
||||
# dev
|
||||
|
||||
```
|
||||
DEBUG=debug,config npm run dev
|
||||
```
|
||||
|
||||
# prod
|
||||
|
||||
```
|
||||
npm run start
|
||||
```
|
||||
|
||||
# test
|
||||
|
||||
```
|
||||
http://<host>:<port>/bus-stops-count
|
||||
```
|
|
@ -0,0 +1,54 @@
|
|||
const debug=require('debug')('debug');
|
||||
|
||||
//start
|
||||
debug('api start...');
|
||||
|
||||
require('dotenv').config();
|
||||
const HELMET = require('helmet');
|
||||
const COMPRESSION = require('compression');
|
||||
|
||||
const EXPRESS = require("express");
|
||||
const CORS = require("cors");
|
||||
|
||||
const ROOTROUTER = require('./route/root');
|
||||
const BUS_STOPS_COUNT = require('./route/bus-stops-count');
|
||||
|
||||
//TODO make this list available via config
|
||||
//limit access to this origin list
|
||||
let whitelist = [
|
||||
'http(s)://foo.bar'
|
||||
];
|
||||
|
||||
const APP = EXPRESS();
|
||||
|
||||
//compress all routes
|
||||
APP.use(COMPRESSION());
|
||||
|
||||
//protect against vulnerabilities
|
||||
APP.use(HELMET());
|
||||
|
||||
//configure CORS
|
||||
APP.use(CORS({
|
||||
origin: function(origin, callback){
|
||||
// allow requests with no origin
|
||||
debug('origin: '+origin)
|
||||
if(!origin){
|
||||
return callback(null, true);
|
||||
}
|
||||
if(whitelist.indexOf(origin) === -1){
|
||||
let message = 'The CORS policy for this origin does not allow access from the particular origin: '+origin;
|
||||
return callback(new Error(message), false);
|
||||
}
|
||||
debug('origin: '+origin+' allowed by CORS');
|
||||
return callback(null, true);
|
||||
}
|
||||
}));
|
||||
|
||||
//api enable/disable?
|
||||
APP.use('/', ROOTROUTER);
|
||||
APP.use('/bus-stops-count',BUS_STOPS_COUNT);
|
||||
|
||||
module.exports=APP;
|
||||
|
||||
//end
|
||||
debug('api done..');
|
|
@ -0,0 +1,20 @@
|
|||
const DEBUG=require('debug')('config');
|
||||
DEBUG('config start...');
|
||||
|
||||
require('dotenv').config();
|
||||
|
||||
const config = {
|
||||
db: { /*TODO do not put password or any sensitive info here, done only for demo */
|
||||
host: process.env.DB_HOST || 'host',
|
||||
port: process.env.DB_PORT || '5432',
|
||||
user: process.env.DB_USER || 'usr',
|
||||
password: process.env.DB_PASSWORD || 'key',
|
||||
database: process.env.DB_NAME || 'db',
|
||||
},
|
||||
listPerPage: process.env.LIST_PER_PAGE || 10,
|
||||
};
|
||||
|
||||
DEBUG('config host: '+config.db.host);
|
||||
DEBUG('config port: '+config.db.port);
|
||||
module.exports = config;
|
||||
DEBUG('config done.');
|
|
@ -0,0 +1,16 @@
|
|||
const EXPRESS = require('express');
|
||||
const ROUTER = EXPRESS.Router();
|
||||
const BUS_STOPS_COUNT = require('../service/bus-stops-count');
|
||||
const UTILS=require('../utils');
|
||||
|
||||
//GET listing
|
||||
ROUTER.get('/', async function(req, res, next) {
|
||||
try {
|
||||
res.json(await BUS_STOPS_COUNT.get(req.query.oset, req.query.limit));
|
||||
} catch (err) {
|
||||
console.error(`Error while getting data: `, err.message);
|
||||
res.status(err.statusCode || 500).json(UTILS.MSGS.error);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = ROUTER;
|
|
@ -0,0 +1,13 @@
|
|||
const DEBUG=require('debug')('root');
|
||||
|
||||
const EXPRESS = require('express');
|
||||
const ROUTER = EXPRESS.Router();
|
||||
const UTILS=require('../utils');
|
||||
|
||||
/* GET home page. */
|
||||
ROUTER.get('/', function(req, res, next) {
|
||||
DEBUG('root msg: '+UTILS.MSGS.alive);
|
||||
res.json(UTILS.MSGS.alive);
|
||||
});
|
||||
|
||||
module.exports = ROUTER;
|
|
@ -0,0 +1,11 @@
|
|||
const db = require('./db');
|
||||
|
||||
async function get(){
|
||||
const rsp = await db.query(
|
||||
'SELECT count(id) FROM rvb.tbl_vrb_bus_stops;'
|
||||
);
|
||||
return rsp[0].count;
|
||||
};
|
||||
module.exports = {
|
||||
get
|
||||
};
|
|
@ -0,0 +1,20 @@
|
|||
const { Pool } = require('pg');
|
||||
const config = require('../config');
|
||||
const pool = new Pool(config.db);
|
||||
|
||||
/**
|
||||
* Query the database using the pool
|
||||
* @param {*} query
|
||||
* @param {*} params
|
||||
*
|
||||
* @see https://node-postgres.com/features/pooling#single-query
|
||||
*/
|
||||
async function query(query, params) {
|
||||
const {rows, fields} = await pool.query(query, params);
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
query
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
const MSGS={
|
||||
'alive': 'alive',
|
||||
'error': 'error'
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
MSGS
|
||||
}
|
Loading…
Reference in New Issue