mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
reworked html serving, added new permissions for api/auth, updated adapters
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { createClient } from "@libsql/client/node";
|
||||
import { App, type BkndConfig, type CreateAppConfig } from "./src";
|
||||
@@ -6,61 +5,6 @@ import { LibsqlConnection } from "./src/data";
|
||||
import { StorageLocalAdapter } from "./src/media/storage/adapters/StorageLocalAdapter";
|
||||
import { registries } from "./src/modules/registries";
|
||||
|
||||
async function getHtml() {
|
||||
return readFile("index.html", "utf8");
|
||||
}
|
||||
function addViteScripts(html: string) {
|
||||
return html.replace(
|
||||
"<head>",
|
||||
`<script type="module">
|
||||
import RefreshRuntime from "/@react-refresh"
|
||||
RefreshRuntime.injectIntoGlobalHook(window)
|
||||
window.$RefreshReg$ = () => {}
|
||||
window.$RefreshSig$ = () => (type) => type
|
||||
window.__vite_plugin_react_preamble_installed__ = true
|
||||
</script>
|
||||
<script type="module" src="/@vite/client"></script>
|
||||
`
|
||||
);
|
||||
}
|
||||
|
||||
function createApp(config: BkndConfig, env: any) {
|
||||
const create_config = typeof config.app === "function" ? config.app(env) : config.app;
|
||||
return App.create(create_config as CreateAppConfig);
|
||||
}
|
||||
|
||||
function setAppBuildListener(app: App, config: BkndConfig, html: string) {
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
async () => {
|
||||
await config.onBuilt?.(app as any);
|
||||
app.module.server.setAdminHtml(html);
|
||||
app.module.server.client.get("/assets/!*", serveStatic({ root: "./" }));
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
|
||||
export async function serveFresh(config: BkndConfig, _html?: string) {
|
||||
let html = _html;
|
||||
if (!html) {
|
||||
html = await getHtml();
|
||||
}
|
||||
|
||||
html = addViteScripts(html);
|
||||
|
||||
return {
|
||||
async fetch(request: Request, env: any) {
|
||||
const app = createApp(config, env);
|
||||
|
||||
setAppBuildListener(app, config, html);
|
||||
await app.build();
|
||||
|
||||
return app.fetch(request, env);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
registries.media.add("local", {
|
||||
cls: StorageLocalAdapter,
|
||||
schema: StorageLocalAdapter.prototype.getSchema()
|
||||
@@ -72,11 +16,35 @@ const connection = new LibsqlConnection(
|
||||
})
|
||||
);
|
||||
|
||||
const app = await serveFresh({
|
||||
function createApp(config: BkndConfig, env: any) {
|
||||
const create_config = typeof config.app === "function" ? config.app(env) : config.app;
|
||||
return App.create(create_config as CreateAppConfig);
|
||||
}
|
||||
|
||||
export async function serveFresh(config: BkndConfig) {
|
||||
return {
|
||||
async fetch(request: Request, env: any) {
|
||||
const app = createApp(config, env);
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
async () => {
|
||||
await config.onBuilt?.(app as any);
|
||||
app.registerAdminController();
|
||||
app.module.server.client.get("/assets/*", serveStatic({ root: "./" }));
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
await app.build();
|
||||
|
||||
return app.fetch(request, env);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default await serveFresh({
|
||||
app: {
|
||||
connection
|
||||
},
|
||||
setAdminHtml: true
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
Reference in New Issue
Block a user