First Commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import type { GetRouteReturn } from "../../types";
|
||||
import AppNames from "../../utils/grab-app-names";
|
||||
import ReactDOMServer from "react-dom/server";
|
||||
|
||||
type Params = {
|
||||
url: URL;
|
||||
route: GetRouteReturn;
|
||||
req: Request;
|
||||
};
|
||||
|
||||
export default async function grabRouteContent({
|
||||
url,
|
||||
route,
|
||||
req,
|
||||
}: Params): Promise<Response> {
|
||||
const config = global.CONFIG;
|
||||
const { name } = AppNames;
|
||||
|
||||
let html = `Welcome to ${name} ...`;
|
||||
|
||||
if (route.component) {
|
||||
html = ReactDOMServer.renderToString(<route.component />);
|
||||
return new Response(html, {
|
||||
headers: {
|
||||
"Content-Type": "text/html; charset=utf-8",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(html);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import AppNames from "../../utils/grab-app-names";
|
||||
import grabAppPort from "../../utils/grab-app-port";
|
||||
import getRoute from "../router/get-route";
|
||||
import grabRouteContent from "./grab-route-content";
|
||||
|
||||
export default async function startServer() {
|
||||
const config = global.CONFIG;
|
||||
const port = grabAppPort();
|
||||
const { name } = AppNames;
|
||||
|
||||
const server = Bun.serve({
|
||||
async fetch(req) {
|
||||
try {
|
||||
const url = new URL(req.url);
|
||||
|
||||
const route = await getRoute({ route: url.pathname });
|
||||
|
||||
if (!route) {
|
||||
return new Response(`Route ${url.pathname} not Found!`, {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
|
||||
const response = await grabRouteContent({ req, route, url });
|
||||
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
return new Response(`No Response!`, {
|
||||
status: 404,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return new Response(`Server Error: ${error.message}`, {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
},
|
||||
port,
|
||||
development: true,
|
||||
});
|
||||
|
||||
global.SERVER = server;
|
||||
console.log(`${name} Server Running on Port ${server.port} ...`);
|
||||
|
||||
return server;
|
||||
}
|
||||
Reference in New Issue
Block a user