Skip to content

Commit

Permalink
Refactor with native deno serve
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Sep 5, 2024
1 parent 465f78a commit 144211b
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 213 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ WORKDIR /deno-dir
COPY . .

ENTRYPOINT ["/bin/deno"]
CMD ["--allow-net", "https://deno.land/std/examples/echo_server.ts"]
CMD ["run", "serve"]
11 changes: 7 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"tasks": {
"dev": "deno --allow-net --watch source/app.js",
"serve": "deno serve source/app.ts",
"dev": "deno serve --watch source/app.ts",
"bump": "deno add npm:simple-icons@latest",
"update-lockfile": "deno cache --reload source/app.js source/colors.js source/icon.js source/utils.js test/viewbox.test.js"
"update-lockfile": "deno cache --reload source/app.ts source/colors.ts source/icon.ts source/utils.ts test/viewbox.test.ts"
},
"fmt": {
"useTabs": true,
"singleQuote": true,
"exclude": ["README.md"]
"exclude": [
"README.md"
]
},
"imports": {
"@oak/oak": "jsr:@oak/oak@^16.1.0",
"@std/http": "jsr:@std/http@^1.0.5",
"simple-icons": "npm:simple-icons@^13.8.0",
"svg-path-bbox": "npm:svg-path-bbox@^2.1.0",
"svgpath": "npm:svgpath@^2.6.0"
Expand Down
137 changes: 32 additions & 105 deletions deno.lock

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

6 changes: 3 additions & 3 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ primary_region = 'sjc'
[build]

[env]
PORT = '8080'
PORT = '8000'

[processes]
app = '--allow-net ./source/app.js'
app = 'run serve'

[http_service]
internal_port = 8080
internal_port = 8000
force_https = true
auto_stop_machines = 'off'
auto_start_machines = true
Expand Down
69 changes: 0 additions & 69 deletions source/app.js

This file was deleted.

81 changes: 81 additions & 0 deletions source/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Route, route } from '@std/http';
import { getIconSvg, getSimpleIcon } from './icon.ts';

const cacheForOneYearHeader =
'public, max-age=31536000, s-maxage=31536000, immutable';
const cacheForSevenDaysHeader =
'public, max-age=86400, s-maxage=31536000, stale-while-revalidate=604800';

const routes: Route[] = [
{
pattern: new URLPattern({ pathname: '/' }),
handler: () => {
return new Response(
null,
{
headers: {
'Cache-Control': cacheForOneYearHeader,
'Location': 'https://github.com/LitoMore/simple-icons-cdn',
},
status: 307,
},
);
},
},
{
pattern: new URLPattern({ pathname: '/favicon.ico' }),
handler: () => {
return new Response(null, {
headers: {
'Cache-Control': cacheForOneYearHeader,
},
status: 204,
});
},
},
{
pattern: new URLPattern({ pathname: '/:iconSlug/:color?/:darkModeColor?' }),
handler: (request, _info, params) => {
const url = new URL(request.url);
const viewbox = url.searchParams.get('viewbox') || undefined;
const size = url.searchParams.get('size') || undefined;
const iconSlug = params?.pathname.groups.iconSlug;
const color = params?.pathname.groups.color;
const darkModeColor = params?.pathname.groups.darkModeColor;
const icon = getSimpleIcon(iconSlug);
if (icon) {
const iconSvg = getIconSvg(icon, color, darkModeColor, viewbox, size);
return new Response(iconSvg, {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'image/svg+xml',
'Cache-Control': cacheForSevenDaysHeader,
},
});
}

return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Cache-Control': cacheForSevenDaysHeader,
},
status: 404,
});
},
},
];

const defaultHandler = () => {
return new Response(null, {
headers: { 'Cache-Control': cacheForSevenDaysHeader },
status: 405,
});
};

const handler = route(routes, defaultHandler);

export default {
fetch(req) {
return handler(req);
},
} satisfies Deno.ServeDefaultExport;
2 changes: 1 addition & 1 deletion source/colors.js → source/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ export const cssKeywords = {
whitesmoke: '#f5f5f5',
yellow: '#ffff00',
yellowgreen: '#9acd32',
};
} as const;
Loading

0 comments on commit 144211b

Please sign in to comment.