mirror of
https://github.com/lmg-anon/mikupad.git
synced 2026-07-09 17:19:17 +00:00
Update server.js
This commit is contained in:
parent
7c1483670f
commit
f7cf703355
4 changed files with 110 additions and 8 deletions
|
|
@ -2397,7 +2397,7 @@ export function App({ sessionStorage, useSessionState, isMikupadEndpoint }) {
|
|||
// restart right away (???)
|
||||
let cancelled = false;
|
||||
setCancel(() => () => cancelled = true);
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
if (cancelled)
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@
|
|||
"body-parser": "^1.20.2",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"sqlite3": "^5.1.7"
|
||||
"sqlite3": "^5.1.7",
|
||||
"minimist": "^1.2.8",
|
||||
"axios": "^1.6.8"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
110
server/server.js
110
server/server.js
|
|
@ -2,10 +2,20 @@ const express = require('express');
|
|||
const cors = require('cors');
|
||||
const bodyParser = require('body-parser');
|
||||
const sqlite3 = require('sqlite3');
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
const path = require('path');
|
||||
const minimist = require('minimist');
|
||||
const axios = require('axios');
|
||||
const http = require('http');
|
||||
|
||||
app.use(cors(), bodyParser.json());
|
||||
const app = express();
|
||||
|
||||
// Parse command line arguments
|
||||
const args = minimist(process.argv.slice(2));
|
||||
// Default fallbacks: command line args -> environment variables -> static defaults
|
||||
const port = args.port || process.env.MIKUPAD_PORT || 3000;
|
||||
const host = args.host || process.env.MIKUPAD_HOST || '0.0.0.0';
|
||||
|
||||
app.use(cors(), bodyParser.json({limit: "100mb"}));
|
||||
|
||||
// Open a database connection
|
||||
const db = new sqlite3.Database('./web-session-storage.db', (err) => {
|
||||
|
|
@ -20,6 +30,96 @@ const db = new sqlite3.Database('./web-session-storage.db', (err) => {
|
|||
}
|
||||
});
|
||||
|
||||
// GET route to serve Mikupad html
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '..', 'mikupad.html'));
|
||||
});
|
||||
|
||||
// Dynamic POST proxy route
|
||||
app.post('/proxy/*', async (req, res) => {
|
||||
// Capture the part of the URL after '/proxy'
|
||||
const path = req.params[0];
|
||||
|
||||
// Target server base URL
|
||||
const targetBaseUrl = req.headers['x-real-url'];
|
||||
delete req.headers['x-real-url'];
|
||||
|
||||
if ('content-length' in req.headers) {
|
||||
delete req.headers['content-length'];
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios({
|
||||
method: 'post',
|
||||
url: `${targetBaseUrl}/${path}`,
|
||||
data: req.body,
|
||||
headers: {
|
||||
...req.headers,
|
||||
'Content-Type': 'application/json',
|
||||
'Host': new URL(targetBaseUrl).hostname // Update the Host header for the target server
|
||||
},
|
||||
responseType: 'stream'
|
||||
});
|
||||
|
||||
// Proxy the headers
|
||||
res.set(response.headers);
|
||||
|
||||
// Proxy stream requests
|
||||
response.data.pipe(res);
|
||||
|
||||
// Stop stream requests if the connection is aborted on the other end
|
||||
res.on('close', () => {
|
||||
response.data.destroy();
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
if (error.response.data instanceof http.IncomingMessage) {
|
||||
error.response.data.pipe(res.status(error.response.status));
|
||||
} else {
|
||||
res.status(error.response.status).send(error.response.data);
|
||||
}
|
||||
} else if (error.request) {
|
||||
res.status(504).send('No response from target server.');
|
||||
} else {
|
||||
res.status(500).send(`Error setting up request to target server: ${error.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Dynamic GET proxy route
|
||||
app.get('/proxy/*', async (req, res) => {
|
||||
// Capture the part of the URL after '/proxy'
|
||||
const path = req.params[0];
|
||||
|
||||
// Target server base URL
|
||||
const targetBaseUrl = req.headers['x-real-url'];
|
||||
delete req.headers['x-real-url'];
|
||||
|
||||
if ('content-length' in req.body) {
|
||||
delete req.body['content-length'];
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${targetBaseUrl}/${path}`, {
|
||||
headers: {
|
||||
...req.headers,
|
||||
'Content-Type': 'application/json',
|
||||
'Host': new URL(targetBaseUrl).hostname // Update the Host header for the target server
|
||||
}
|
||||
});
|
||||
|
||||
res.send(response.data);
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
res.status(error.response.status).send(error.response.data);
|
||||
} else if (error.request) {
|
||||
res.status(504).send('No response from target server.');
|
||||
} else {
|
||||
res.status(500).send(`Error setting up request to target server: ${error.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// POST route to load data
|
||||
app.post('/load', (req, res) => {
|
||||
const { key } = req.body;
|
||||
|
|
@ -74,8 +174,8 @@ app.post('/delete', (req, res) => {
|
|||
});
|
||||
|
||||
// Start the server
|
||||
app.listen(port, '0.0.0.0', () => {
|
||||
console.log(`Server listening at http://0.0.0.0:${port}`);
|
||||
app.listen(port, host, () => {
|
||||
console.log(`Server listening at http://${host}:${port}`);
|
||||
});
|
||||
|
||||
// Close db connection on server close
|
||||
|
|
|
|||
|
|
@ -12,4 +12,4 @@ if %errorlevel% neq 0 (
|
|||
)
|
||||
|
||||
call npm install --no-audit
|
||||
call npm start
|
||||
call node server.js %*
|
||||
Loading…
Add table
Add a link
Reference in a new issue