Skip to content

Commit

Permalink
2.0.2 fix assets serving, enable bun smol, add 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
wendevlin committed Jun 1, 2024
1 parent 6a5e291 commit 7acd520
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 47 deletions.
2 changes: 1 addition & 1 deletion homedocs/.mise.toml
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"
6 changes: 6 additions & 0 deletions homedocs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->

## 2.0.2

- Fixed serving of assets
- enabled bun smol mode to reduce memory usage
- added styled 404 page

## 2.0.1

- Use pre built image
Expand Down
4 changes: 2 additions & 2 deletions homedocs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ RUN if [[ $(uname -m) == "aarch64" ]] ; \
fi

# Install Bun
RUN npm install -g bun
RUN npm install -g bun@1.1.12

# Copy starlight data
# Copy server into image
COPY server /server
WORKDIR /server

Expand Down
2 changes: 1 addition & 1 deletion homedocs/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build_from:
# i386: "ghcr.io/home-assistant/i386-base:3.19"
labels:
org.opencontainers.image.title: "Homedocs"
org.opencontainers.image.description: "Document stuff with Markdown based on Astro Starlight"
org.opencontainers.image.description: "Document stuff with Markdown"
org.opencontainers.image.source: "https://github.com/masterwendu/homeassistant-addons/tree/main/homedocs"
org.opencontainers.image.licenses: "Apache License 2.0"
args:
Expand Down
2 changes: 1 addition & 1 deletion homedocs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Homedocs

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config
description: Document stuff with Markdown
version: "2.0.1"
version: "2.0.2"
slug: homedocs
init: false
url: https://github.com/masterwendu/homeassistant-addons/tree/main/homedocs
Expand Down
2 changes: 1 addition & 1 deletion homedocs/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ -z "$(ls -A /config/homedocs)" ]; then
mkdir -p /config/homedocs
echo "created /config/homedocs directory."

# copy the example contents of the "/starlight/src/content/docs" directory to the "/config/homedocs" directory.
# copy the example contents of the "/server/docs" directory to the "/config/homedocs" directory.
cp -r /server/docs/* /config/homedocs
echo "copied example content to /config/homedocs."
else
Expand Down
5 changes: 4 additions & 1 deletion homedocs/server/bunfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ smol = true
telemetry = false

[install]
exact = true
exact = true

# Reduce memory usage at the cost of performance
smol = true
11 changes: 10 additions & 1 deletion homedocs/server/docs/Downloads/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ If you want to save for example some manuals you can create download links with
<a href="../files/sample.pdf" download>here</a>
```

Download the file <a href="../files/sample.pdf" download>here</a>

Download the file <a href="../files/sample.pdf" download>here</a>

or open direct link via markdown:

```markdown
[Open the file](../files/sample.pdf)
```

[Open the file](../files/sample.pdf)
3 changes: 2 additions & 1 deletion homedocs/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "bun run --watch src/index.ts",
"start": "bun src/index.ts",
"start:production": "NODE_ENV=production bun src/index.ts",
"build": "bun run --bun scripts/build.ts"
"build": "bun run --bun scripts/build.ts",
"biome:fix": "biome check --apply src"
},
"dependencies": {
"@kitajs/html": "4.1.0",
Expand Down
27 changes: 27 additions & 0 deletions homedocs/server/src/templates/404.tsx
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>
)
71 changes: 33 additions & 38 deletions homedocs/server/src/webserver.ts
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
})

0 comments on commit 7acd520

Please sign in to comment.