Verified Commit 12905ced authored by Aral Balkan's avatar Aral Balkan
Browse files

Update to web-routes-from-files 3.0.0; improve restarts

Also WIP: tests for DotJS parameters
parent 0f933ce3
......@@ -4,12 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [15.0.0] - In progress…
## [15.0.0] - 2020-09-01
### Added
- [TODO] (Breaking change.) DotJS routes can now specify any number of parameters and static route fragments. This is potentially a breaking change for existing sites if they used underscores in the names of dynamic routes as these have now gained special significance and will be interpreted as parameter (single) and path (double) delimiters. (#230)
### Fixed
- Dynamic routes are now reloaded on regular processes after restarts. (Server restarts now create a new Site instance and destroy the old one instead of just stopping/starting the same server instance. This brings the restart behaviour of regular processes closer to that of daemons, which simply exit the process so that systemd can restart it.)
## [14.6.3] - 2020-08-31
### Fixed
......
......@@ -267,6 +267,9 @@ class Site {
this.proxyPort = options.proxyPort
}
// Also save a copy of the options.
this.options = options
//
// Create the Express app. We will configure it later.
//
......@@ -1117,15 +1120,18 @@ class Site {
this.eventEmitter.removeAllListeners()
this.log('\n 🐁 ❨site.js❩ Restarting server…\n')
const {commandPath, args} = cli.initialise(process.argv.slice(2))
await this.serve(args)
const newSite = new Site(this.options)
await newSite.serve(args)
this.log('\n 🐁 ❨site.js❩ Server restarted.\n')
this.restartingRegularProcess = false
delete this
})
// Destroy the current server (so we do not get a port conflict on restart before
// we’ve had a chance to terminate our own process).
this.server.destroy(() => {
this.log('\n 🐁 ❨site.js❩ Server destroyed.\n')
this.server.removeAllListeners()
})
}
}
......@@ -1290,9 +1296,19 @@ class Site {
const httpsGetRoutes = getRoutes(httpsGetRoutesDirectory)
httpsGetRoutes.forEach(route => {
this.log(` 🐁 ❨site.js❩ Adding HTTPS GET route: ${route.path}`)
// Ensure we are loading a fresh copy in case it has changed.
decache(route.callback)
this.app.get(route.path, require(route.callback))
try {
this.app.get(route.path, require(route.callback))
} catch (error) {
if (error.message.includes('requires a callback function but got a [object Object]')) {
console.log(`\n ❌ ${clr('❨site.js❩ Error:', 'red')} Could not find callback in route ${route.path}\n\n ❨site.js❩ ${clr('Hint:', 'green')} Make sure your DotJS routes include a ${clr('module.exports = (request, response) => {}', 'cyan')} declaration.\n`)
} else {
console.log(`\n ❌ ${clr('❨site.js❩ Error:', 'red')} ${error}`)
}
process.exit()
}
})
}
......
{
"name": "@small-tech/site.js",
"version": "14.5.0",
"version": "15.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -466,9 +466,9 @@
"integrity": "sha512-y5McNIkqyMT8Ab9C0e1qbKqwT5D3cfCci8yE9SD+7Z/4E/uV2pxEJmp5Yt2W05z1hKWXk1B6SfHu7RIMg+XyUw=="
},
"@small-tech/web-routes-from-files": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@small-tech/web-routes-from-files/-/web-routes-from-files-2.0.0.tgz",
"integrity": "sha512-JnjQKhTy3mgnu417DcMvQdGsWA3fZoSDiHOFHSUrSf/TYZpPZm5IbilUlEMMDfdKnOFaeNnN8sjLy7GQGyeetg=="
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@small-tech/web-routes-from-files/-/web-routes-from-files-3.0.0.tgz",
"integrity": "sha512-7YIIgTozJgspXaGNSUJPkgLfC1z2V0yRcz80uFjmtEmFR1FjH2phWZ8VEKmCFb+YZlrKaijvq+o1zdFYQz317g=="
},
"@szmarczak/http-timer": {
"version": "1.1.2",
......
{
"name": "@small-tech/site.js",
"version": "14.6.3",
"version": "15.0.0",
"description": "Small Web construction set.",
"keywords": [
"web server",
......@@ -58,7 +58,7 @@
"@small-tech/node-hugo": "^1.5.0",
"@small-tech/portable-rsync-with-ssh-for-windows": "^1.0.0",
"@small-tech/rsync-with-portable-cygwin-path-support-on-windows": "^1.0.0",
"@small-tech/web-routes-from-files": "^2.0.0",
"@small-tech/web-routes-from-files": "^3.0.0",
"ansi-escape-sequences": "^4.1.0",
"bent": "^7.3.4",
"body-parser": "^1.19.0",
......
module.exports = (request, response) => {
response.json({
personId: request.params.personId,
bookId: request.params.bookId
})
}
module.exports = (request, response) => {
response.type('text').end(`The rabbit’s name is ${request.params.name}.`)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment