generated from home-assistant/addons-example
-
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.
2.0.2 fix assets serving, enable bun smol, add 404 page
- Loading branch information
Showing
11 changed files
with
88 additions
and
47 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,3 +1,3 @@ | ||
[tools] | ||
node = "20" | ||
bun = "1.1.10" | ||
bun = "1.1.12" |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export default (path: string) => ( | ||
<html lang="en"> | ||
<head> | ||
<title>Homedocs - 404 Not Found</title> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="stylesheet" href="./main.css" /> | ||
<link rel="icon" href="./favicon.svg" /> | ||
</head> | ||
<body> | ||
<div class="navbar bg-nav dark:bg-nav-dark dark:text-white justify-between"> | ||
<span class="text-xl"> | ||
<img class="h-8 mr-5" src="./favicon.svg" alt="Homedocs Logo" /> | ||
Homedocs | ||
</span> | ||
</div> | ||
<div> | ||
<div class="flex flex-col items-center justify-start"> | ||
<div class="prose py-10 px-3 max-w-screen-xl w-full"> | ||
<div class="text-4xl">404</div> | ||
{path} not found in your Homedocs | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
) |
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,56 +1,51 @@ | ||
import type { BunFile } from 'bun' | ||
import { Elysia } from 'elysia' | ||
import notFoundPage from './templates/404' | ||
import environmentVariables from './utils/environmentVariables' | ||
|
||
export const webserver = new Elysia({ | ||
serve: { | ||
hostname: '0.0.0.0', | ||
}, | ||
}) | ||
.get('*', async ({ path, set }) => { | ||
const status = 200 | ||
let responseData: BunFile | string = `${path} not found` // TODO return styled 404 page | ||
}).get('*', async ({ path, set }) => { | ||
let status = 200 | ||
let responseData: BunFile | string | Promise<string> = '' | ||
|
||
const fileName = path.split('/').pop() ?? '' | ||
if (fileName === 'main.css') { | ||
set.headers['content-type'] = 'text/css' | ||
return Bun.file('./dist/main.css') | ||
} else if (fileName === 'favicon.svg') { | ||
return Bun.file('./public/favicon.svg') | ||
} else if ( | ||
fileName.includes('.') && | ||
(fileName.split('.').pop()?.length ?? 0) > 1 | ||
) { | ||
const file = Bun.file(`${environmentVariables.docsBasePath}/${path}`) | ||
if (!(await file.exists())) { | ||
console.log('not found', `${environmentVariables.docsBasePath}/${path}`) | ||
set.status = 404 | ||
} | ||
responseData = file | ||
set.headers['content-type'] = file.type | ||
const fileName = path.split('/').pop() ?? '' | ||
if (fileName === 'main.css') { | ||
return Bun.file('./dist/main.css') | ||
} else if (fileName.startsWith('favicon')) { | ||
return Bun.file('./public/favicon.svg') | ||
} else if ( | ||
fileName.includes('.') && | ||
(fileName.split('.').pop()?.length ?? 0) > 1 | ||
) { | ||
const file = Bun.file(`${environmentVariables.docsBasePath}/${path}`) | ||
if (!(await file.exists())) { | ||
status = 404 | ||
} else { | ||
let htmlFile = Bun.file(`./dist/docs/${path}.html`) | ||
responseData = file | ||
} | ||
} else { | ||
let htmlFile = Bun.file(`./dist/docs/${path}.html`) | ||
|
||
if (!(await htmlFile.exists())) { | ||
htmlFile = Bun.file(`./dist/docs/${path}/index.html`) | ||
if (!(await htmlFile.exists())) { | ||
htmlFile = Bun.file(`./dist/docs/${path}/index.html`) | ||
if (!(await htmlFile.exists())) { | ||
set.status = 404 | ||
} else { | ||
responseData = htmlFile | ||
set.headers['content-type'] = htmlFile.type | ||
} | ||
status = 404 | ||
} else { | ||
responseData = htmlFile | ||
set.headers['content-type'] = htmlFile.type | ||
} | ||
} else { | ||
responseData = htmlFile | ||
} | ||
} | ||
|
||
set.status = status | ||
set.status = status | ||
|
||
set.headers['content-type'] = 'text/html' | ||
return responseData | ||
}) | ||
.onError(({ error, code }) => { | ||
console.log('error') | ||
console.error(error) | ||
}) | ||
if (status === 404) { | ||
responseData = notFoundPage(path) | ||
set.headers['Content-Type'] = 'text/html' | ||
} | ||
return responseData | ||
}) |