Skip to content

Commit

Permalink
minimal solid-rest server
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-zucker committed Feb 26, 2024
1 parent 0043ff7 commit 154c514
Show file tree
Hide file tree
Showing 4 changed files with 9,726 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Jeff Zucker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 50 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const {SolidNodeClient} = require('solid-node-client');
const client = new SolidNodeClient();
const express = require('express');
const URL = require('url');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.text());

const port = process.argv[2] || null;
const docRoot = process.argv[3] || null;
if(!(port && docRoot)) {
console.log("Syntax: npm run start port documentRoot.");
console.log(" e.g. npm run start 3000 /home/me.");
process.exit();
}

app.all('*', async (req, res) => {
const filePath = "file://" + docRoot + req.path;
try{
const solidRestRequest = mungeRestRequest(req);
const solidRestResponse = await client.fetch(filePath,solidRestRequest);
let content = solidRestResponse.body.toString();
res = mungeRestResponse(res,solidRestResponse);
res.send(content);
}
catch(e){console.log(e)}
});
function mungeRestResponse(res,solidRestResponse){
res.status = solidRestResponse.status;
res.statusText = solidRestResponse.statusText;
res.headers = solidRestResponse.headers;
res.url = solidRestResponse.url;
res.location = solidRestResponse.location;
return res;
}
function mungeRestRequest(req){
return {
status : req.status,
headers : req.headers,
url : 'file://' + docRoot + req.path,
slug : req.slug,
body : req.body,
}
}

app.listen(port, () => {
console.log(`Server is listening at http://localhost:${port}`);
});

Loading

0 comments on commit 154c514

Please sign in to comment.