Skip to content

Commit

Permalink
Return http server on post hook
Browse files Browse the repository at this point in the history
  • Loading branch information
knor-el-snor committed Feb 28, 2019
1 parent 9c71643 commit 810309a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ treehouse.startServer(app, {
port: 3000,
title: 'My app',
pre: preFn, // function to execute before starting server (optional)
post: postFn, // function to execute after starting server (optional)
post: postFn, // function to execute after starting server (optional) - will contain the http server as first argument
https: { // optional
port: 3001,
privateKey: 'assets/ssl.key',
Expand All @@ -144,6 +144,7 @@ treehouse.startServer(app, {
Serve Swagger UI via the a provided Swagger yaml file OR folder with valid structure and yaml files.

### YAML file implementation

```javascript
const app = express();

Expand All @@ -158,6 +159,7 @@ treehouse.setSwagger(app, '/documentation', 'documentation/swagger.yml', {
### Folder implementation with valid structure
Structure
```bash
.
├── validFolderName
Expand Down
70 changes: 44 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"express-brute": "~1.0.1",
"express-brute-redis": "~0.0.1",
"express-validation": "~1.0.2",
"helmet": "~3.15.0",
"helmet": "~3.15.1",
"https": "~1.0.0",
"js-yaml": "~3.12.1",
"js-yaml": "~3.12.2",
"swagger-ui-express": "~4.0.2"
},
"devDependencies": {
Expand All @@ -38,21 +38,21 @@
"@types/express-brute": "~0.0.37",
"@types/express-brute-redis": "~0.0.1",
"@types/helmet": "~0.0.42",
"@types/jest": "^24.0.0",
"@types/joi": "^14.3.1",
"@types/jest": "^24.0.9",
"@types/joi": "^14.3.2",
"@types/js-yaml": "~3.12.0",
"@types/supertest": "^2.0.7",
"coveralls": "^3.0.2",
"coveralls": "^3.0.3",
"jest": "^24.1.0",
"joi": "^14.3.1",
"np": "^4.0.2",
"pre-commit": "^1.2.2",
"redis-mock": "^0.43.0",
"supertest": "^3.4.2",
"ts-jest": "^23.10.5",
"tslint": "^5.12.1",
"ts-jest": "^24.0.0",
"tslint": "^5.13.0",
"tslint-config-airbnb": "^5.11.1",
"typescript": "^3.3.1"
"typescript": "^3.3.3333"
},
"engines": {
"node": ">=6.0.0"
Expand Down
10 changes: 5 additions & 5 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function startServer(app: Application, options: ServerOptions): Pro
}

// Optional callback function
if (options.post) await postHook(options.post);
if (options.post) await postHook(options.post, httpServer);
} catch (error) {
console.error('An error occurred trying to start the server:\n', error.message);
throw error;
Expand All @@ -32,7 +32,7 @@ export async function startServer(app: Application, options: ServerOptions): Pro
/**
* Execute a pre-hook function
*/
export async function preHook(fn) {
export async function preHook(fn: Function) {
try {
await fn();
} catch (error) {
Expand All @@ -44,9 +44,9 @@ export async function preHook(fn) {
/**
* Execute a post-hook function
*/
export async function postHook(fn) {
export async function postHook(fn: Function, httpServer: http.Server) {
try {
await fn();
await fn(httpServer);
} catch (error) {
console.error('Error trying to execute the post-hook function');
throw error;
Expand Down Expand Up @@ -76,5 +76,5 @@ export interface ServerOptions {
certificate: string;
};
pre?: Function;
post?: Function;
post?: (server: http.Server) => void | Promise<void>;
}

0 comments on commit 810309a

Please sign in to comment.