-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
127 lines (115 loc) · 3.48 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { HttpServer as fastro } from "./http/server.ts";
import { Context, HttpRequest, Next, RenderOptions } from "./mod.ts";
import app from "./pages/app.tsx";
import index from "./pages/index.tsx";
const f = new fastro();
f.use((req: HttpRequest, _ctx: Context, next: Next) => {
console.log(`${req.method} ${req.url}`);
return next();
});
f.get("/api", () => {
return Response.json({ time: new Date().getTime() });
});
f.static("/static", { folder: "static" });
f.page("/app", app, (_req: HttpRequest, ctx: Context) => {
return ctx.render({
build: true,
cache: false,
props: { data: "Guest" },
html: { head: { title: "React component" } },
});
});
function denoRunCheck(req: HttpRequest) {
const regex = /^Deno\/(\d+\.\d+\.\d+)$/;
const string = req.headers.get("user-agent");
if (!string) return false;
const match = regex.exec(string);
if (!match) return false;
return true;
}
function init() {
const basePath = Deno.env.get("DENO_DEPLOYMENT_ID")
? `https://raw.githubusercontent.com/fastrodev/fastro/main/static`
: "http://localhost:8000/static";
const code = `import init, { version } from "${basePath}/init.ts";
const name = Deno.args[0] ?? 'my-project';
await init(name, version)
`;
return new Response(code, {
headers: {
"content-type": "application/typescript; charset=utf-8",
},
});
}
f.page(
"/",
index,
async (req: HttpRequest, ctx: Context) => {
const res = denoRunCheck(req);
if (res) return init();
let git: Record<string, string>;
try {
const data = await fetch(
"https://api.github.com/repos/fastrodev/fastro/releases/latest",
);
git = JSON.parse(await data.text());
} catch {
git = {};
git["name"] = "local";
}
const options: RenderOptions = {
build: false,
props: { version: git["name"] },
html: {
class: "h-100",
head: {
title: "Web Application Framework | Fastro",
descriptions: "Fast & Simple Web Application Framework",
meta: [
{ charset: "utf-8" },
{
name: "viewport",
content: "width=device-width, initial-scale=1.0",
},
{
name: "description",
content: "Fast & Simple Web Application Framework",
},
{
property: "og:image",
content: "https://fastro.dev/static/image.png",
},
],
link: [{
href:
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css",
rel: "stylesheet",
integrity:
"sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD",
crossorigin: "anonymous",
}, {
href: "/static/cover.css",
rel: "stylesheet",
}],
script: [{
src:
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js",
integrity:
"sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN",
crossorigin: "anonymous",
}],
},
body: {
class: "d-flex h-100 text-bg-dark",
rootClass:
"cover-container d-flex w-100 h-100 p-3 mx-auto flex-column",
},
},
};
return ctx.render(options);
},
);
f.onListen(({ port, hostname }) => {
console.log(`Listening on http://${hostname}:${port}`);
});
await f.serve();