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,
|
trendingTagsController,
|
||||||
} from '@/controllers/api/trends.ts';
|
} from '@/controllers/api/trends.ts';
|
||||||
import { errorHandler } from '@/controllers/error.ts';
|
import { errorHandler } from '@/controllers/error.ts';
|
||||||
|
import { frontendController } from '@/controllers/frontend.ts';
|
||||||
import { metricsController } from '@/controllers/metrics.ts';
|
import { metricsController } from '@/controllers/metrics.ts';
|
||||||
import { indexController } from '@/controllers/site.ts';
|
import { indexController } from '@/controllers/site.ts';
|
||||||
import '@/startup.ts';
|
import '@/startup.ts';
|
||||||
|
|
@ -121,7 +122,6 @@ import { requireSigner } from '@/middleware/requireSigner.ts';
|
||||||
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
|
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
|
||||||
import { storeMiddleware } from '@/middleware/storeMiddleware.ts';
|
import { storeMiddleware } from '@/middleware/storeMiddleware.ts';
|
||||||
import { uploaderMiddleware } from '@/middleware/uploaderMiddleware.ts';
|
import { uploaderMiddleware } from '@/middleware/uploaderMiddleware.ts';
|
||||||
import { serveStaticWithOG } from './middleware/serveStaticWithOG.ts';
|
|
||||||
|
|
||||||
interface AppEnv extends HonoEnv {
|
interface AppEnv extends HonoEnv {
|
||||||
Variables: {
|
Variables: {
|
||||||
|
|
@ -315,7 +315,6 @@ app.use('/oauth/*', notImplementedController);
|
||||||
|
|
||||||
const publicFiles = serveStatic({ root: './public/' });
|
const publicFiles = serveStatic({ root: './public/' });
|
||||||
const staticFiles = serveStatic({ root: './static/' });
|
const staticFiles = serveStatic({ root: './static/' });
|
||||||
const frontendController = serveStaticWithOG({ path: './public/index.html' });
|
|
||||||
|
|
||||||
// Known frontend routes
|
// Known frontend routes
|
||||||
app.get('/@:acct', frontendController);
|
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 { html, r } from '@/utils/html.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import {
|
import {
|
||||||
|
|
@ -10,6 +8,7 @@ import {
|
||||||
OpenGraphTemplateOpts,
|
OpenGraphTemplateOpts,
|
||||||
PathParams,
|
PathParams,
|
||||||
} from '@/utils/og-metadata.ts';
|
} from '@/utils/og-metadata.ts';
|
||||||
|
import { AppMiddleware } from '@/app.ts';
|
||||||
|
|
||||||
/** Placeholder to find & replace with metadata. */
|
/** Placeholder to find & replace with metadata. */
|
||||||
const OG_META_PLACEHOLDER = '<!--server-generated-meta-->' as const;
|
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);
|
return await BLANK_META(url);
|
||||||
};
|
};
|
||||||
|
|
||||||
export function serveStaticWithOG<E extends Env>(
|
export const frontendController: AppMiddleware = async (c, next) => {
|
||||||
options: ServeStaticOptions<E>,
|
try {
|
||||||
): MiddlewareHandler {
|
const content = await Deno.readTextFile(new URL('../../public/index.html', import.meta.url));
|
||||||
// deno-lint-ignore require-await
|
if (content.includes(OG_META_PLACEHOLDER)) {
|
||||||
return async function serveStatic(c: Context, next: Next) {
|
const params = getPathParams(c.req.path);
|
||||||
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}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
if (params) {
|
||||||
};
|
const meta = await buildMetaTags(params, Conf.local(c.req.path));
|
||||||
const pathResolve = (path: string) => {
|
return c.html(content.replace(OG_META_PLACEHOLDER, meta));
|
||||||
return `./${path}`;
|
}
|
||||||
};
|
}
|
||||||
return baseServeStatic({
|
return c.html(content);
|
||||||
...options,
|
} catch (e) {
|
||||||
getContent,
|
console.log(e);
|
||||||
pathResolve,
|
await next();
|
||||||
})(c, next);
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
@ -27,6 +27,7 @@ interface StatusInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = await Storages.db();
|
const store = await Storages.db();
|
||||||
|
|
||||||
export const getInstanceName = async () => {
|
export const getInstanceName = async () => {
|
||||||
const meta = await getInstanceMetadata(store, AbortSignal.timeout(1000));
|
const meta = await getInstanceMetadata(store, AbortSignal.timeout(1000));
|
||||||
return meta?.name || 'Ditto';
|
return meta?.name || 'Ditto';
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue