Skip to content

Commit

Permalink
feat: add the createApp and listen app for server manual setup
Browse files Browse the repository at this point in the history
  • Loading branch information
trylovetom committed Mar 3, 2020
1 parent 1f1b33d commit 4d03482
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 24 deletions.
27 changes: 27 additions & 0 deletions packages/tonjs/example/manual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
send,
redirect,
createApp,
listen,
registerGracefulShutdown,
route
} from '../src/index'

const host = '0.0.0.0'
const port = 3000

async function main() {
const app = createApp()
route(app, 'GET', '/', (req, res) => send(res, 200, 'TonJS'))
route(app, 'GET', '/redirect', (req, res) =>
redirect(res, 302, 'https://tonjs.com')
)
const token = await listen(app, host, port)
registerGracefulShutdown(token)
// eslint-disable-next-line
console.info(
`\nyou raise me up, to listen on http://${host}:${port}\n`
)
}

main()
2 changes: 1 addition & 1 deletion packages/tonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "The Node.js Web Framework For A Ton Of Request",
"bin": "dist/bind.js",
"main": "dist/index.js",
"type": "dist/index.d.ts",
"repository": "[email protected]:AllJointTW/TonJS.git",
"author": "TZU-YEN, CHANG <[email protected]>",
"license": "GPL-3.0",
"scripts": {
"dev": "nodemon --watch '{src,example}/**/*.ts' --ignore '{src,example}/**/*.{spec,test}.ts' --exec 'ts-node' src/bin.ts",
"dev:js": "nodemon --watch '{dist,example}/**/*.js' --ignore '{dist,example}/**/*.{spec,test}.js' dist/bin.js",
"format": ":",
"lint": ":",
"build": "tsc"
Expand Down
45 changes: 22 additions & 23 deletions packages/tonjs/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import uWS from 'uWebSockets.js'
import {
createApp,
listen,
route,
TonHandler,
TonListenSocket,
Expand Down Expand Up @@ -35,32 +36,35 @@ const { argv } = yargs
ssl: {
type: 'boolean',
desc: 'Need SSL?',
implies: ['ssl-key', 'ssl-cert']
implies: ['key', 'cert']
},
'ssl-key': {
key: {
type: 'string',
desc: 'Specify the path of key of SSL'
},
'ssl-cert': {
cert: {
type: 'string',
desc: 'Specify the path of cert of SSL'
},
passphrase: {
type: 'string',
desc: 'Specify the passphrase of SSL cert'
},
dhParams: {
type: 'string',
desc: 'Specify the path of params.dh of SSL'
},
preferLowMemoryUsage: {
type: 'boolean',
desc:
'Translate SSL to buffer, not only low memory, but also low performance'
}
})

async function main() {
try {
const [entry = 'index.js'] = argv._
const app = argv.ssl
? uWS.SSLApp({
cert_file_name: argv['ssl-cert'], // eslint-disable-line
key_file_name: argv['ssl-key'], // eslint-disable-line
passphrase: argv.passphrase
})
: uWS.App()
const app = createApp(argv)
const endpoints: TonHandler | TonRoutes = (
await import(path.resolve(process.cwd(), entry))
).default
Expand All @@ -83,19 +87,14 @@ async function main() {
route(app, 'ANY', '*', endpoints as TonHandler)
}

app.listen(argv.host, argv.port, (token: TonListenSocket) => {
if (!token) {
// eslint-disable-next-line
console.info(`\nfailed to listen on ${argv.host}:${argv.port}`)
return
}
// eslint-disable-next-line
console.info(
`\nyou raise me up, to listen on http://${argv.host}:${argv.port}`
)
registerGracefulShutdown(token)
})
const token = await listen(app, argv.host, argv.port)
registerGracefulShutdown(token)
// eslint-disable-next-line
console.info(
`\nyou raise me up, to listen on http://${argv.host}:${argv.port}\n`
)
} catch (err) {
console.info(`\nfailed to listen on ${argv.host}:${argv.port}\n`) // eslint-disable-line
console.error(err) // eslint-disable-line
}
}
Expand Down
36 changes: 36 additions & 0 deletions packages/tonjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import bytes from 'bytes'
import { readable } from 'is-stream'

export type TonApp = uWS.TemplatedApp
export type TonAppOptions = {
ssl?: boolean
key?: string
cert?: string
passphrase?: string
dhParams?: string
preferLowMemoryUsage?: boolean
}
export type TonRequest = uWS.HttpRequest
export type TonResponse = uWS.HttpResponse & { aborted: boolean }
export type TonStream = stream.Readable & { size?: number }
Expand Down Expand Up @@ -294,3 +302,31 @@ export function registerGracefulShutdown(socket: TonListenSocket) {
process.on('SIGTERM', wrapper)
process.on('exit', wrapper)
}

export function createApp(options: TonAppOptions = {}): TonApp {
if (options.ssl) {
return uWS.SSLApp({
key_file_name: options.key, // eslint-disable-line
cert_file_name: options.cert, // eslint-disable-line
passphrase: options.passphrase,
dh_params_file_name: options.dhParams, // eslint-disable-line
ssl_prefer_low_memory_usage: options.preferLowMemoryUsage // eslint-disable-line
})
}
return uWS.App()
}

export function listen(
app: TonApp,
host: string,
port: number
): Promise<TonListenSocket> {
return new Promise((resolve, reject) => {
app.listen(host, port, (token: TonListenSocket) => {
if (!token) {
return reject()
}
return resolve(token)
})
})
}

0 comments on commit 4d03482

Please sign in to comment.