Refactored ads.txt to use .env file setting.

This commit is contained in:
David Ball 2024-07-13 05:26:55 -04:00
parent 1a8090adc2
commit f53200fe10
4 changed files with 19 additions and 3 deletions

View File

@ -1,2 +1,3 @@
GOOGLE_ADSENSE_ADS_TXT="google.com, pub-1234567890abcdef, DIRECT, fedcba9876543210"
GOOGLE_ANALYTICS_GTAG=G-1234567890
SITE_URL=http://localhost

View File

@ -1 +0,0 @@
google.com, pub-8937572456576531, DIRECT, f08c47fec0942fa0

View File

@ -11,11 +11,13 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export interface ProcessEnv {
GOOGLE_ADSENSE_ADS_TXT?: string;
GOOGLE_ANALYTICS_GTAG?: string;
SITE_URL?: string;
}
export interface Config {
GoogleAdsenseAdsTxt: string;
GoogleAnalyticsGTag: string;
siteUrl: string;
}
@ -30,10 +32,12 @@ dotEnvConfig = dotenvExpand.expand({
processEnv: process.env as dotenvExpand.DotenvParseInput
});
export const getGoogleAnalyticsGtag = () => env.GOOGLE_ANALYTICS_GTAG||'';
export const getSiteUrl = () => trimSlashes(env.SITE_URL||'http://localhost');
export const getGoogleAnalyticsGtag = () => (env.GOOGLE_ANALYTICS_GTAG||``).trim();
export const getGoogleAdsenseAdsTxt = () => (env.GOOGLE_ADSENSE_ADS_TXT||``).trim()||`google.com, pub-1234567890abcdef, DIRECT, fedcba9876543210`;
export const getSiteUrl = () => trimSlashes(env.SITE_URL||`http://localhost`);
export const config: Config = {
GoogleAnalyticsGTag: getGoogleAnalyticsGtag(),
GoogleAdsenseAdsTxt: getGoogleAdsenseAdsTxt(),
siteUrl: getSiteUrl(),
};

12
src/pages/ads.txt.ts Normal file
View File

@ -0,0 +1,12 @@
import type { APIRoute } from 'astro';
import { config } from "../config";
const adsTxt = config.GoogleAdsenseAdsTxt;
export const GET: APIRoute = () => {
return new Response(adsTxt, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
});
};