mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Do frontendController like I want to
This commit is contained in:
parent
95ef308d0e
commit
4c83d6d1b2
3 changed files with 19 additions and 37 deletions
|
|
@ -108,6 +108,7 @@ import {
|
|||
trendingTagsController,
|
||||
} from '@/controllers/api/trends.ts';
|
||||
import { errorHandler } from '@/controllers/error.ts';
|
||||
import { frontendController } from '@/controllers/frontend.ts';
|
||||
import { metricsController } from '@/controllers/metrics.ts';
|
||||
import { indexController } from '@/controllers/site.ts';
|
||||
import '@/startup.ts';
|
||||
|
|
@ -121,7 +122,6 @@ import { requireSigner } from '@/middleware/requireSigner.ts';
|
|||
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
|
||||
import { storeMiddleware } from '@/middleware/storeMiddleware.ts';
|
||||
import { uploaderMiddleware } from '@/middleware/uploaderMiddleware.ts';
|
||||
import { serveStaticWithOG } from './middleware/serveStaticWithOG.ts';
|
||||
|
||||
interface AppEnv extends HonoEnv {
|
||||
Variables: {
|
||||
|
|
@ -315,7 +315,6 @@ app.use('/oauth/*', notImplementedController);
|
|||
|
||||
const publicFiles = serveStatic({ root: './public/' });
|
||||
const staticFiles = serveStatic({ root: './static/' });
|
||||
const frontendController = serveStaticWithOG({ path: './public/index.html' });
|
||||
|
||||
// Known frontend routes
|
||||
app.get('/@:acct', frontendController);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import { Context, Env, MiddlewareHandler, Next } from '@hono/hono';
|
||||
import { serveStatic as baseServeStatic, ServeStaticOptions } from '@hono/hono/serve-static';
|
||||
import { html, r } from '@/utils/html.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import {
|
||||
|
|
@ -10,6 +8,7 @@ import {
|
|||
OpenGraphTemplateOpts,
|
||||
PathParams,
|
||||
} from '@/utils/og-metadata.ts';
|
||||
import { AppMiddleware } from '@/app.ts';
|
||||
|
||||
/** Placeholder to find & replace with metadata. */
|
||||
const OG_META_PLACEHOLDER = '<!--server-generated-meta-->' as const;
|
||||
|
|
@ -108,37 +107,20 @@ const buildMetaTags = async (params: PathParams, url: string): Promise<string> =
|
|||
return await BLANK_META(url);
|
||||
};
|
||||
|
||||
export function serveStaticWithOG<E extends Env>(
|
||||
options: ServeStaticOptions<E>,
|
||||
): MiddlewareHandler {
|
||||
// deno-lint-ignore require-await
|
||||
return async function serveStatic(c: Context, next: Next) {
|
||||
let file = '';
|
||||
const getContent = async (path: string) => {
|
||||
try {
|
||||
if (!file) file = await Deno.readTextFile(path);
|
||||
if (!file) throw new Error(`File at ${path} was empty!`);
|
||||
if (file.includes(OG_META_PLACEHOLDER)) {
|
||||
const params = getPathParams(c.req.path);
|
||||
if (params) {
|
||||
const meta = await buildMetaTags(params, Conf.local(c.req.path));
|
||||
return file.replace(OG_META_PLACEHOLDER, meta);
|
||||
}
|
||||
}
|
||||
return file;
|
||||
} catch (e) {
|
||||
console.warn(`${e}`);
|
||||
}
|
||||
export const frontendController: AppMiddleware = async (c, next) => {
|
||||
try {
|
||||
const content = await Deno.readTextFile(new URL('../../public/index.html', import.meta.url));
|
||||
if (content.includes(OG_META_PLACEHOLDER)) {
|
||||
const params = getPathParams(c.req.path);
|
||||
|
||||
return '';
|
||||
};
|
||||
const pathResolve = (path: string) => {
|
||||
return `./${path}`;
|
||||
};
|
||||
return baseServeStatic({
|
||||
...options,
|
||||
getContent,
|
||||
pathResolve,
|
||||
})(c, next);
|
||||
};
|
||||
}
|
||||
if (params) {
|
||||
const meta = await buildMetaTags(params, Conf.local(c.req.path));
|
||||
return c.html(content.replace(OG_META_PLACEHOLDER, meta));
|
||||
}
|
||||
}
|
||||
return c.html(content);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
await next();
|
||||
}
|
||||
};
|
||||
|
|
@ -27,6 +27,7 @@ interface StatusInfo {
|
|||
}
|
||||
|
||||
const store = await Storages.db();
|
||||
|
||||
export const getInstanceName = async () => {
|
||||
const meta = await getInstanceMetadata(store, AbortSignal.timeout(1000));
|
||||
return meta?.name || 'Ditto';
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue