Skip to content

Commit

Permalink
Added initial Aluminum Wire files
Browse files Browse the repository at this point in the history
src/wire/main.js is based upon the "fileserver" feature of the previous version of this project, homeserver.
This commit also includes a default 404 error page under defaults/errors/404.html .
API documentation will soon be added.
  • Loading branch information
ZelnickB committed Aug 23, 2020
1 parent 2444b6d commit 4399317
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
21 changes: 21 additions & 0 deletions defaults/errors/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404 Error | Aluminum Wire</title>
</head>
<body>
<h1>404 Error</h1>
<p>The requested resource $requrl$ was not found on this server. Please check the request URL.</p>
<p>
Error Message: $errmessage$<br />
Error Code: $errcode$<br />
Error Number: $errno$
</p>
<hr />
<p>Aluminum Wire on $osplatform$ ($ostype$) version $osversion$ at port $port$</p>
<div style="text-align: center">
<img src="/aluminum-internals/logo.svg" alt="Aluminum logo" style="width: 150pt" />
</div>
</body>
</html>
59 changes: 59 additions & 0 deletions src/wire/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const http = require('http') // For creating an HTTP server
// const https = require('https') // For creating an HTTPS server (capability will be added later)
const fs = require('fs') // For reading files from the server filesystem
const os = require('os') // For retrieving operating system information (for error pages)
const path = require('path') // For manipulating request paths
const mimeTypes = require('mime-types') // For sending the correct Content-Type HTTP header

const errorPage = fs.readFileSync('../../defaults/errors/404.html', 'utf8') // Default error page content
const logo = fs.readFileSync('../../logo.svg', 'utf8')

function serverHandler (req, res) {
const filename = path.join(__dirname, '..', '..', '..', path.normalize(req.url))
const mimetype = mimeTypes.lookup(filename)
const reqURLArray = req.url.split('/')
if (reqURLArray[1] === 'aluminum-internals') {
switch (reqURLArray[2]) {
case 'logo.svg':
res.writeHead(200, { 'Content-Type': 'image/svg+xml' })
res.write(logo)
return res.end()
default:
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.write('404 ERROR | NOT FOUND' +
os.EOL +
os.EOL +
'The requested internal Aluminum resource, ' +
reqURLArray.slice(2).join('/') +
', does not exist. Please check the request URL.' +
os.EOL +
'Please note that if you have files in a directory named aluminum-internals, then they will not be served. ' +
'The URL /aluminum-internals is reserved for AluminumJS internal resources.'
)
return res.end()
}
}
fs.readFile(filename, function (err, data) {
if (err) {
res.writeHead(404, { 'Content-Type': 'html' })
res.write(
errorPage.replace(/\$requrl\$/g, req.url)
.replace(/\$osplatform\$/g, os.platform())
.replace(/\$ostype\$/g, os.type())
.replace(/\$osversion\$/g, os.release())
.replace(/\$port\$/g, '80')
.replace(/\$errcode\$/, err.code)
.replace(/\$errno\$/, err.errno.toString())
.replace(/\$errmessage\$/, err.message)
)
return res.end()
}
res.writeHead(200, { 'Content-Type': mimetype })
res.write(data)
return res.end()
}
)
}

const server = http.createServer(serverHandler)
server.listen(80)

0 comments on commit 4399317

Please sign in to comment.