From 8ecf30979198ea23ec48884fc0d5934fa50bb315 Mon Sep 17 00:00:00 2001
From: Sidi Liang <1467329765@qq.com>
Date: Sun, 3 Nov 2024 17:31:20 +0800
Subject: [PATCH] [web] Added an option to show execution time on the front
end, and improved server.js
---
nasal-web-app/package.json | 3 +-
nasal-web-app/public/index.html | 26 ++++++++++++++---
nasal-web-app/server.js | 51 +++++++++++++++++++++++++++------
3 files changed, 66 insertions(+), 14 deletions(-)
diff --git a/nasal-web-app/package.json b/nasal-web-app/package.json
index f582aca..b0483e1 100644
--- a/nasal-web-app/package.json
+++ b/nasal-web-app/package.json
@@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"express": "^4.21.1",
- "ffi-napi": "^4.0.3"
+ "ffi-napi": "^4.0.3",
+ "yargs": "^17.7.2"
}
}
diff --git a/nasal-web-app/public/index.html b/nasal-web-app/public/index.html
index 5b194cf..26dc8e4 100644
--- a/nasal-web-app/public/index.html
+++ b/nasal-web-app/public/index.html
@@ -30,7 +30,7 @@
}
.logo {
- margin-bottom: 20px;
+ margin-bottom: 2px;
}
.ascii-art {
@@ -400,6 +400,17 @@
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
line-height: 1.5;
}
+
+ .timing-checkbox {
+ display: inline-flex;
+ align-items: center;
+ margin-left: 10px;
+ color: #abb2bf;
+ }
+
+ .timing-checkbox input {
+ margin-right: 5px;
+ }
@@ -449,8 +460,11 @@ println(x);
-
-
+
+
+
@@ -557,6 +571,7 @@ for(var i = 0; i <= 5; i += 1) {
const runBtn = document.getElementById('runBtn');
const output = document.getElementById('output');
const status = document.getElementById('status');
+ const showTime = document.getElementById('showTime').checked;
try {
runBtn.disabled = true;
@@ -567,7 +582,10 @@ for(var i = 0; i <= 5; i += 1) {
headers: {
'Content-Type': 'application/json'
},
- body: JSON.stringify({ code })
+ body: JSON.stringify({
+ code,
+ showTime
+ })
});
const data = await response.json();
diff --git a/nasal-web-app/server.js b/nasal-web-app/server.js
index 5e69dae..18f3e17 100644
--- a/nasal-web-app/server.js
+++ b/nasal-web-app/server.js
@@ -1,6 +1,32 @@
const express = require('express');
const ffi = require('ffi-napi');
const path = require('path');
+const yargs = require('yargs/yargs');
+const { hideBin } = require('yargs/helpers');
+
+// Parse command line arguments
+const argv = yargs(hideBin(process.argv))
+ .usage('Usage: $0 [options]')
+ .option('verbose', {
+ alias: 'v',
+ type: 'boolean',
+ description: 'Run with verbose logging'
+ })
+ .option('port', {
+ alias: 'p',
+ type: 'number',
+ description: 'Port to run the server on',
+ default: 3000
+ })
+ .option('host', {
+ type: 'string',
+ description: 'Host to run the server on',
+ default: 'localhost'
+ })
+ .help()
+ .alias('help', 'h')
+ .version()
+ .argv;
const app = express();
@@ -10,41 +36,48 @@ app.use(express.static('public'));
const nasalLib = ffi.Library(path.join(__dirname, '../module/libnasal-web'), {
'nasal_init': ['pointer', []],
'nasal_cleanup': ['void', ['pointer']],
- 'nasal_eval': ['string', ['pointer', 'string']],
+ 'nasal_eval': ['string', ['pointer', 'string', 'int']],
'nasal_get_error': ['string', ['pointer']]
});
app.post('/eval', (req, res) => {
- const { code } = req.body;
+ const { code, showTime = false } = req.body;
if (!code) {
return res.status(400).json({ error: 'No code provided' });
}
+ if (argv.verbose) {
+ console.log('Received code evaluation request:', code);
+ console.log('Show time:', showTime);
+ }
+
const ctx = nasalLib.nasal_init();
try {
- const result = nasalLib.nasal_eval(ctx, code);
+ const result = nasalLib.nasal_eval(ctx, code, showTime ? 1 : 0);
const error = nasalLib.nasal_get_error(ctx);
- // Check if there's an error first
- if (error && error !== 'null' && error.trim() !== '') {
- console.log('Nasal error:', error); // For debugging
+ if (error && error !== 'null') {
+ if (argv.verbose) console.log('Nasal error:', error);
res.json({ error: error });
} else if (result && result.trim() !== '') {
- console.log('Nasal output:', result); // For debugging
+ if (argv.verbose) console.log('Nasal output:', result);
res.json({ result: result });
} else {
+ if (argv.verbose) console.log('No output or error returned');
res.json({ error: 'No output or error returned' });
}
} catch (err) {
- console.error('Server error:', err); // For debugging
+ if (argv.verbose) console.error('Server error:', err);
res.status(500).json({ error: err.message });
} finally {
+ if (argv.verbose) console.log('Cleaning up Nasal context');
nasalLib.nasal_cleanup(ctx);
}
});
-const PORT = process.env.PORT || 3000;
+const PORT = argv.port || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Visit http://localhost:${PORT} to use the Nasal interpreter`);
+ if (argv.verbose) console.log('Verbose logging enabled');
});
\ No newline at end of file