-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
step up the basic setup to an ExpressServer at least
- Loading branch information
Jonas Verhoelen
committed
Apr 16, 2019
1 parent
55bbb58
commit bd7c15f
Showing
7 changed files
with
95 additions
and
4 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
lib |
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
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,46 @@ | ||
import { ExpressServer } from './ExpressServer' | ||
|
||
export class Application { | ||
public static async createApplication() { | ||
const expressServer = new ExpressServer() | ||
await expressServer.setup(8000) | ||
Application.handleExit(expressServer) | ||
|
||
return expressServer | ||
} | ||
|
||
private static handleExit(express: ExpressServer) { | ||
process.on('uncaughtException', (err: Error) => { | ||
console.error('Uncaught exception', err) | ||
Application.shutdownProperly(1, express) | ||
}) | ||
process.on('unhandledRejection', (reason: {} | null | undefined) => { | ||
console.error('Unhandled Rejection at promise', reason) | ||
Application.shutdownProperly(2, express) | ||
}) | ||
process.on('SIGINT', () => { | ||
console.info('Caught SIGINT') | ||
Application.shutdownProperly(128 + 2, express) | ||
}) | ||
process.on('SIGTERM', () => { | ||
console.info('Caught SIGTERM') | ||
Application.shutdownProperly(128 + 2, express) | ||
}) | ||
process.on('exit', () => { | ||
console.info('Exiting') | ||
}) | ||
} | ||
|
||
private static shutdownProperly(exitCode: number, express: ExpressServer) { | ||
Promise.resolve() | ||
.then(() => express.kill()) | ||
.then(() => { | ||
console.info('Shutdown complete') | ||
process.exit(exitCode) | ||
}) | ||
.catch(err => { | ||
console.error('Error during shutdown', err) | ||
process.exit(1) | ||
}) | ||
} | ||
} |
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 @@ | ||
import * as express from 'express' | ||
import { Express} from 'express' | ||
import { Server } from 'http' | ||
|
||
export class ExpressServer { | ||
private server?: Express | ||
private httpServer?: Server | ||
|
||
public async setup(port: number) { | ||
const server = express() | ||
this.httpServer = this.listen(server, port) | ||
this.server = server | ||
return this.server | ||
} | ||
|
||
public listen(server: Express, port: number) { | ||
return server.listen(port) | ||
} | ||
|
||
public kill() { | ||
if (this.httpServer) this.httpServer.close() | ||
} | ||
} |
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 @@ | ||
import { Application } from './Application' | ||
|
||
Application.createApplication().then(() => { | ||
console.info('The application was started! Kill it using Ctrl + C') | ||
}) |
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,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"allowJs": false, | ||
"target": "es2017", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"strict": true, | ||
"experimentalDecorators": true, | ||
"jsx": "react", | ||
"importHelpers": true, | ||
"lib": [ "es2017", "dom" ] | ||
}, | ||
"include": [ | ||
"./service/**/*" | ||
] | ||
} |