Appearance
webserver
Provides a HTTP server that can be extended by other scripts.
Description
This script uses the Hono web application framework to provide an extensible HTTP server.
The server is started once all scripts have called their respective $.connect()
function. Until then routes can be added and the port number changed
Service
typescript
app: Hono;
enableLogging(): void;
printUrls: boolean;
setPort(port: number): void;
Example
Config:
typescript
export default defineConfig({
connections: [{
kind: 'serialPort',
path: '/dev/tty.usbmodem',
}],
scripts: [
import('../scripts/webserver'),
import('../scripts/webserver/get-device-information-api'),
defineScript(async ($) => {
const webserverScript = await import('../scripts/webserver');
const webserverService = await webserverScript.default.requireService();
webserverService.app.get('/', c => {
return c.text('Hello world!\n');
});
await $.connect();
}),
],
});
Running it in the toolbox:
❯ bin/resol-vbus-core-toolbox.ts --config configs/above-config-saved-to-file.ts
The webserver is available under:
- http://192.168.xxx.xxx:3000/
- ...
In a separate terminal:
❯ curl http://127.0.0.1:3000/
Hello world!
❯ curl http://127.0.0.1:3000/cgi-bin/get_resol_device_information
vendor = "RESOL"
product = "DL2Plus"
serial = "000000000000"
version = "0.0.0"
build = "SNAPSHOT"
name = "DL2Plus-000000000000"
features = "vbus"