Refactor WEBMENTION_TOKEN handling: remove file-based token retrieval and use global variable
All checks were successful
Deploy / deploy (push) Successful in 1m17s
All checks were successful
Deploy / deploy (push) Successful in 1m17s
This commit is contained in:
parent
85f58ae557
commit
7de0a815f4
3 changed files with 19 additions and 30 deletions
|
|
@ -11,9 +11,6 @@ RUN npm ci
|
|||
|
||||
COPY . .
|
||||
|
||||
RUN printf '%s' "${WEBMENTION_TOKEN}" > /app/.webmention-token
|
||||
RUN echo "TOKEN FILE:" && ls -la /app/.webmention-token && wc -c /app/.webmention-token
|
||||
|
||||
RUN npm run build
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,13 +3,25 @@
|
|||
import mdx from '@astrojs/mdx';
|
||||
import sitemap from '@astrojs/sitemap';
|
||||
import { defineConfig, fontProviders } from 'astro/config';
|
||||
import { loadEnv } from 'vite';
|
||||
|
||||
import node from '@astrojs/node';
|
||||
|
||||
const envMode = process.env.NODE_ENV === 'production' ? 'production' : 'development';
|
||||
const envVars = loadEnv(envMode, process.cwd(), '');
|
||||
const WEBMENTION_TOKEN = envVars.WEBMENTION_TOKEN || process.env.WEBMENTION_TOKEN || '';
|
||||
console.log(`[astro.config] loadEnv mode=${envMode} token-len=${WEBMENTION_TOKEN.length}`);
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
site: 'https://adrian-altner.de',
|
||||
|
||||
vite: {
|
||||
define: {
|
||||
'globalThis.__WEBMENTION_TOKEN__': JSON.stringify(WEBMENTION_TOKEN),
|
||||
},
|
||||
},
|
||||
|
||||
devToolbar: {
|
||||
enabled: false,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,32 +1,13 @@
|
|||
---
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { DEFAULT_LOCALE, type Locale } from '~/consts';
|
||||
import { getLocaleFromUrl, t } from '~/i18n/ui';
|
||||
|
||||
let fileDebug = '';
|
||||
function readTokenFromFile(): string | undefined {
|
||||
const paths = ['/app/.webmention-token', '.webmention-token'];
|
||||
const logs: string[] = [];
|
||||
logs.push(`cwd=${process.cwd()}`);
|
||||
for (const p of paths) {
|
||||
try {
|
||||
const raw = readFileSync(p, 'utf-8');
|
||||
logs.push(`${p}:ok(${raw.length})`);
|
||||
const t = raw.trim();
|
||||
if (t) {
|
||||
fileDebug = logs.join(';');
|
||||
return t;
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var __WEBMENTION_TOKEN__: string;
|
||||
}
|
||||
} catch (err) {
|
||||
logs.push(`${p}:err(${(err as Error).code ?? 'unknown'})`);
|
||||
}
|
||||
}
|
||||
fileDebug = logs.join(';');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const FILE_TOKEN = readTokenFromFile();
|
||||
const WEBMENTION_TOKEN = FILE_TOKEN ?? (import.meta.env as Record<string, string | undefined>).WEBMENTION_TOKEN;
|
||||
const tokenRaw = (globalThis as unknown as { __WEBMENTION_TOKEN__?: string }).__WEBMENTION_TOKEN__;
|
||||
const WEBMENTION_TOKEN = typeof tokenRaw === 'string' ? tokenRaw : '';
|
||||
|
||||
interface WMAuthor {
|
||||
name?: string;
|
||||
|
|
@ -58,8 +39,7 @@ interface FetchResult {
|
|||
async function fetchMentions(target: string): Promise<FetchResult> {
|
||||
const token = WEBMENTION_TOKEN;
|
||||
const tokenLen = typeof token === 'string' ? token.length : 0;
|
||||
const source = FILE_TOKEN ? 'file' : 'env';
|
||||
if (!token) return { mentions: [], debug: `no-token(src=${source},len=${tokenLen},probe=${fileDebug})` };
|
||||
if (!token) return { mentions: [], debug: `no-token(len=${tokenLen})` };
|
||||
const withSlash = target.endsWith('/') ? target : `${target}/`;
|
||||
const withoutSlash = target.replace(/\/+$/, '');
|
||||
const fetchOne = async (t: string) => {
|
||||
|
|
@ -83,7 +63,7 @@ async function fetchMentions(target: string): Promise<FetchResult> {
|
|||
}
|
||||
return {
|
||||
mentions: merged,
|
||||
debug: `ok(src=${source},len=${tokenLen},probe=${fileDebug}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`,
|
||||
debug: `ok(len=${tokenLen}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue