This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea7edbc
commit 353cedc
Showing
16 changed files
with
3,861 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
reports | ||
dist | ||
docs | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
lib | ||
tests | ||
reports | ||
docs | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "6" | ||
- "8" | ||
|
||
sudo: false | ||
|
||
matrix: | ||
fast_finish: true | ||
|
||
before_install: | ||
- npm config set spin false | ||
- npm i -g typescript | ||
|
||
install: | ||
- npm install | ||
|
||
script: | ||
- npm run build | ||
- npm run lint | ||
- npm run test:cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 onechiporenko | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Route from './route'; | ||
import Server from './server'; | ||
|
||
export {Server, Route}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as express from 'express'; | ||
import {Lair} from 'lair-db/dist'; | ||
import {CRUDOptions} from 'lair-db/dist/lair'; | ||
import methods = require('methods'); | ||
import {assert} from './utils'; | ||
|
||
function defaultNext(req: express.Request, res: express.Response, data: any) { | ||
return res.json(data); | ||
} | ||
|
||
export type Handler = (req: express.Request, res: express.Response, next: express.NextFunction, lair: Lair) => any; | ||
export type CustomNext = (req: express.Request, res: express.Response, data: any, lair: Lair) => any; | ||
|
||
export default class Route { | ||
public static createRoute(method = 'get', path = '', handler: Handler): Route { | ||
const route = new Route(); | ||
route.handler = handler; | ||
route.method = method; | ||
assert(`"${method}" is unknown method. It must be one of the [${methods.join(',')}]`, methods.indexOf(method) !== -1); | ||
route.path = path; | ||
return route; | ||
} | ||
|
||
public static get(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext): Route { | ||
const realNext = customNext ? customNext : defaultNext; | ||
return Route.createRoute('get', path, (req, res, next, lair) => { | ||
const parameters = Object.keys(req.params); | ||
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined; | ||
const result = id ? lair.getOne(modelName, id, lairOptions) : lair.getAll(modelName, lairOptions); | ||
return realNext(req, res, result, lair); | ||
}); | ||
} | ||
|
||
public static post(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext): Route { | ||
const realNext = customNext ? customNext : defaultNext; | ||
return Route.createRoute('post', path, (req, res, next, lair) => { | ||
const result = lair.createOne(modelName, req.body, lairOptions); | ||
return realNext(req, res, result, lair); | ||
}); | ||
} | ||
|
||
public static put(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext): Route { | ||
const realNext = customNext ? customNext : defaultNext; | ||
return Route.createRoute('get', path, (req, res, next, lair) => { | ||
const parameters = Object.keys(req.params); | ||
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined; | ||
assert('identifier is not provided', !!id); | ||
const result = lair.updateOne(modelName, id, req.body, lairOptions); | ||
return realNext(req, res, result, lair); | ||
}); | ||
} | ||
|
||
public static patch(path: string, modelName: string, lairOptions: CRUDOptions = {}, customNext: CustomNext): Route { | ||
return Route.put(path, modelName, lairOptions, customNext); | ||
} | ||
|
||
public static delete(path: string, modelName: string, customNext: CustomNext): Route { | ||
const realNext = customNext ? customNext : defaultNext; | ||
return Route.createRoute('del', path, (req, res, next, lair) => { | ||
const parameters = Object.keys(req.params); | ||
const id = parameters.length === 1 ? req.params[parameters[0]] : undefined; | ||
assert('identifier is not provided', !!id); | ||
const result = lair.deleteOne(modelName, id); | ||
return realNext(req, res, result, lair); | ||
}); | ||
} | ||
|
||
public handler: Handler; | ||
public method = 'get'; | ||
public path = '/'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import bodyParser = require('body-parser'); | ||
import colors = require('colors/safe'); | ||
import * as express from 'express'; | ||
import {Factory, Lair} from 'lair-db/dist'; | ||
import winston = require('winston'); | ||
import Route from './route'; | ||
import {printRoutesMap} from './utils'; | ||
|
||
export default class Server { | ||
public static getServer(): Server { | ||
if (!Server.instance) { | ||
Server.instance = new Server(); | ||
} | ||
return Server.instance; | ||
} | ||
|
||
public static cleanServer(): void { | ||
Lair.cleanLair(); | ||
Server.instance = new Server(); | ||
} | ||
|
||
private static instance: Server; | ||
|
||
public namespace = '/dlm/api'; | ||
public port = 54321; | ||
public verbose = true; | ||
public delay = 0; | ||
|
||
private expressApp: express.Application; | ||
private expressRouter: express.Router; | ||
private lair: Lair; | ||
private createRecordsQueue: Array<[string, number]> = []; | ||
|
||
private constructor() { | ||
this.expressApp = express(); | ||
this.expressRouter = express.Router(); | ||
this.lair = Lair.getLair(); | ||
} | ||
|
||
public addRoute(route: Route) { | ||
this.expressRouter[route.method]( | ||
route.path, | ||
(req, res, next) => | ||
route.handler.call(this.expressRouter, req, res, next, this.lair)); | ||
} | ||
|
||
public addRoutes(routes: Route[]) { | ||
routes.map(route => this.addRoute(route)); | ||
} | ||
|
||
public addFactory(factory: Factory, name: string) { | ||
this.lair.registerFactory(factory, name); | ||
} | ||
|
||
public addFactories(factories: Array<[Factory, string]>) { | ||
factories.map(args => this.addFactory.apply(this, args)); | ||
} | ||
|
||
public createRecords(factoryName: string, count: number) { | ||
this.createRecordsQueue.push([factoryName, count]); | ||
} | ||
|
||
public startServer() { | ||
this.lair.verbose = this.verbose; | ||
this.createRecordsQueue.map(crArgs => this.lair.createRecords.apply(this.lair, crArgs)); | ||
const app = this.expressApp; | ||
app.use(bodyParser.json()); | ||
app.use((req, res, next) => { | ||
if (this.verbose) { | ||
winston.log('info', colors.green(`${req.method} ${req.url}`)); | ||
} | ||
next(); | ||
}); | ||
app.use((req, res, next) => { | ||
if (this.delay) { | ||
setTimeout(next, this.delay); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
app.use(this.namespace, this.expressRouter); | ||
this.printRoutesMap(); | ||
app.listen(this.port); | ||
} | ||
|
||
private printRoutesMap() { | ||
if (this.verbose) { | ||
winston.info('Defined route-handlers'); | ||
this.expressApp._router.stack.forEach(printRoutesMap.bind(null, [])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import winston = require('winston'); | ||
|
||
export function assert(msg: string, condition: boolean) { | ||
if (!condition) { | ||
throw new Error(msg); | ||
} | ||
} | ||
|
||
// from https://github.com/expressjs/express/issues/3308#issuecomment-300957572 | ||
export function printRoutesMap(path, layer) { | ||
if (layer.route) { | ||
layer.route.stack.forEach(printRoutesMap.bind(null, path.concat(split(layer.route.path)))); | ||
} else if (layer.name === 'router' && layer.handle.stack) { | ||
layer.handle.stack.forEach(printRoutesMap.bind(null, path.concat(split(layer.regexp)))); | ||
} else if (layer.method) { | ||
winston.info(`${layer.method.toUpperCase()} ${path.concat(split(layer.regexp)).filter(Boolean).join('/')}`); | ||
} | ||
} | ||
|
||
function split(thing) { | ||
if (typeof thing === 'string') { | ||
return thing.split('/'); | ||
} else if (thing.fast_slash) { | ||
return ''; | ||
} else { | ||
const match = thing.toString() | ||
.replace('\\/?', '') | ||
.replace('(?=\\/|$)', '$') | ||
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//); | ||
return match | ||
? match[1].replace(/\\(.)/g, '$1').split('/') | ||
: '<complex:' + thing.toString() + '>'; | ||
} | ||
} |
Oops, something went wrong.