feat(api): add api folder

This commit is contained in:
dancingCycle 2023-05-12 14:49:07 +02:00
parent 1a8806f0a9
commit 7e7ed73a4d
8 changed files with 2519 additions and 0 deletions

110
api/.gitignore vendored Normal file
View File

@ -0,0 +1,110 @@
# 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

29
api/index.js Normal file
View File

@ -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.');

2248
api/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

39
api/package.json Normal file
View File

@ -0,0 +1,39 @@
{
"private": true,
"name": "delfi-api",
"description": "API to request DELFI data from postgres database",
"version": "0.0.1",
"main": "index.js",
"keywords": [
"public",
"transport",
"mobility",
"postgresql",
"gtfs",
"delfi",
"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"
}
}

20
api/readme.md Normal file
View File

@ -0,0 +1,20 @@
# init
```
mkdir ~/git && cd ~/git
git clone -b main <repository>
cd <repository>
npm i
```
# dev
```
DEBUG=debug npm run dev
```
# prod
```
npm run start
```

52
api/src/api.js Normal file
View File

@ -0,0 +1,52 @@
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');
//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);
module.exports=APP;
//end
debug('api done..');

13
api/src/route/root.js Normal file
View File

@ -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;

8
api/src/utils.js Normal file
View File

@ -0,0 +1,8 @@
const MSGS={
'alive': 'alive',
'error': 'error'
};
module.exports = {
MSGS
}