Skip to content

Commit

Permalink
Merge pull request #15 from Jalle19/http-server-tweaks
Browse files Browse the repository at this point in the history
Make web interface actually build, make web server serve it
  • Loading branch information
Jalle19 authored Oct 6, 2023
2 parents 1aa88df + 2eb6daf commit 8ad395d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 54 deletions.
73 changes: 47 additions & 26 deletions src/http/server.ts
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)
}
25 changes: 6 additions & 19 deletions webif/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion webif/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prettier-check": "prettier --check src/"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.20.4",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
Expand Down
4 changes: 1 addition & 3 deletions webif/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@
return d.circuit.type === 'main' && d.circuit.sensor.type !== 'unmetered'
})
circuitSensorData = message.data.filter((d) => {
return d.circuit.type === 'circuit'
})
circuitSensorData = message.data
break
case 'configuration':
configuration = message.data
Expand Down
9 changes: 4 additions & 5 deletions webif/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adapter from '@sveltejs/adapter-auto'
import adapter from '@sveltejs/adapter-static'
import { vitePreprocess } from '@sveltejs/kit/vite'

/** @type {import('@sveltejs/kit').Config} */
Expand All @@ -8,10 +8,9 @@ const config = {
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter(),
adapter: adapter({
fallback: 'index.html'
}),
},
}

Expand Down

0 comments on commit 8ad395d

Please sign in to comment.