-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Jalle19/http-server-tweaks
Make web interface actually build, make web server serve it
- Loading branch information
Showing
5 changed files
with
59 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,63 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { IncomingMessage, RequestListener, ServerResponse } from 'http' | ||
import { promises as fsPromisified } from 'fs' | ||
|
||
const basePath = __dirname + '/../../webif/build' | ||
|
||
const mimeTypes = new Map<string, string>([ | ||
['.html', 'text/html'], | ||
['.js', 'text/javascript'], | ||
['.css', 'text/css'], | ||
['.json', 'application/json'], | ||
['.png', 'image/png'], | ||
]) | ||
|
||
export const httpRequestHandler: RequestListener = async (req: IncomingMessage, res: ServerResponse) => { | ||
// Main routing logic | ||
let reqUrl = req.url | ||
const filePath = resolveFilePath(req.url) | ||
|
||
// Serve 404 if file doesn't exist | ||
if (!fs.existsSync(filePath)) { | ||
res.writeHead(404) | ||
res.end('Not found') | ||
return | ||
} | ||
|
||
// File exists, try to determine MIME type | ||
const extension = path.extname(filePath).toLowerCase() | ||
const mimeType = mimeTypes.get(extension) | ||
|
||
await serveStaticFile(filePath, mimeType, res) | ||
} | ||
|
||
const resolveFilePath = (reqUrl: string | undefined): string => { | ||
// Strip query parameters | ||
if (reqUrl?.indexOf('?') !== -1) { | ||
reqUrl = reqUrl?.substring(0, reqUrl?.indexOf('?')) | ||
} | ||
|
||
switch (reqUrl) { | ||
case '/': | ||
case '/index.html': | ||
await serveStaticFile('index.html', 'text/html', res) | ||
break | ||
case '/eachwatt.css': | ||
await serveStaticFile('eachwatt.css', 'text/css', res) | ||
break | ||
case '/eachwatt.js': | ||
await serveStaticFile('eachwatt.js', 'text/javascript', res) | ||
break | ||
case '/eachwatt_logo.png': | ||
await serveStaticFile('eachwatt_logo.png', 'image/png', res) | ||
break | ||
default: | ||
res.writeHead(404) | ||
res.end('Not found') | ||
break | ||
// "Convert" to file path | ||
let filePath | ||
if (!reqUrl || reqUrl === '/') { | ||
filePath = '/index.html' | ||
} else { | ||
filePath = reqUrl | ||
} | ||
} | ||
|
||
const resolveFilePath = (file: string): string => { | ||
return __dirname + '/../../webif/' + file | ||
return basePath + filePath | ||
} | ||
|
||
const serveStaticFile = async (file: string, contentType: string, res: ServerResponse): Promise<void> => { | ||
const fileContents = await fsPromisified.readFile(resolveFilePath(file)) | ||
const serveStaticFile = async ( | ||
filePath: string, | ||
contentType: string | undefined, | ||
res: ServerResponse, | ||
): Promise<void> => { | ||
const fileContents = await fsPromisified.readFile(filePath) | ||
|
||
if (contentType !== undefined) { | ||
res.setHeader('Content-Type', contentType) | ||
} | ||
|
||
res.setHeader('Content-Type', contentType) | ||
res.writeHead(200) | ||
res.end(fileContents) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters