-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(page): use dns entries for custom domains
- Loading branch information
1 parent
bc30fbd
commit a131f51
Showing
19 changed files
with
286 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
/// <reference path="../.astro/types.d.ts" /> | ||
/// <reference types="astro/client" /> | ||
|
||
type KVNamespace = import("@cloudflare/workers-types").KVNamespace; | ||
type ENV = { | ||
CUSTOM_DOMAINS: KVNamespace; | ||
}; | ||
|
||
// use a default runtime configuration (advanced mode). | ||
type Runtime = import("@astrojs/cloudflare").Runtime<ENV>; | ||
declare namespace App { | ||
interface Locals extends Runtime {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getDnsRecords } from '@layered/dns-records'; | ||
import { get, noop } from 'lodash-es'; | ||
import type { APIContext } from 'astro'; | ||
import { getDomain } from 'tldts'; | ||
import { safeParse } from './json'; | ||
|
||
type FeedData = { feed: string | null; primary_color: string | null }; | ||
|
||
const getStore = (context: APIContext): KVNamespace => | ||
get(context, ['locals', 'runtime', 'env', 'CUSTOM_DOMAINS'], { | ||
get: async () => null, | ||
put: noop | ||
} as unknown as KVNamespace); | ||
|
||
export const extractDnsData = async (context: APIContext): Promise<FeedData> => { | ||
const domain = getDomain(context.url.hostname); | ||
const entryName = `lux.${domain}`; | ||
const store = getStore(context); | ||
|
||
let result = await store.get(entryName); | ||
|
||
if (!result) { | ||
const dnsEntry = await getDnsRecords(entryName, 'TXT'); | ||
result = get(dnsEntry, ['data', 0], null); | ||
result && store.put(entryName, result, { expirationTtl: 60 * 60 }); | ||
} | ||
|
||
const fallback = { feed: null, primary_color: null }; | ||
return safeParse<FeedData>(result || JSON.stringify(fallback), fallback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const safeParse = <T>(input: string, fallback: T) => { | ||
try { | ||
return JSON.parse(input); | ||
} catch (err) { | ||
return fallback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { defineMiddleware } from "astro:middleware"; | ||
import { getRequestParams } from "./request-param"; | ||
|
||
export const handleCustomDomain = defineMiddleware(async ({ request, rewrite, originPathname }, next) => { | ||
const { feed, episodeId } = getRequestParams(request); | ||
|
||
if (feed && episodeId && originPathname !== `/feed/${feed}/episode/${episodeId}`) { | ||
return rewrite(`/feed/${feed}/episode/${episodeId}`); | ||
} | ||
|
||
if (feed && originPathname !== '/feed') { | ||
return rewrite('/feed'); | ||
} | ||
|
||
if (!feed && originPathname !== '/search') { | ||
return rewrite('/search'); | ||
} | ||
|
||
return next(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { defineMiddleware } from 'astro:middleware'; | ||
import { get } from 'lodash-es'; | ||
import { extractDnsData } from '../lib/dns-record'; | ||
|
||
|
||
export const getRequestParams = (request: Request): { feed: string | undefined; episodeId: string | undefined; customDomain: boolean; } => { | ||
const { feed, episodeId, customDomain } = get(request, 'data') as unknown as { | ||
feed: string; | ||
episodeId: string; | ||
customDomain: boolean; | ||
}; | ||
|
||
return { | ||
feed, | ||
episodeId, | ||
customDomain | ||
} | ||
}; | ||
|
||
export const extractRequestParams = defineMiddleware(async (context, next) => { | ||
const { request, params } = context; | ||
let { feed, episodeId } = params; | ||
|
||
const dns = await extractDnsData(context); | ||
|
||
if (dns.feed) { | ||
feed = dns.feed; | ||
} | ||
|
||
(request as any).data = { feed, episodeId, customDomain: !!dns.feed }; | ||
|
||
return next(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { defineMiddleware, sequence } from 'astro:middleware'; | ||
import type { MiddlewareHandler } from 'astro'; | ||
import { defineMiddleware } from 'astro:middleware'; | ||
import multimatch from 'multimatch'; | ||
|
||
export function defineMiddlewareRouter(router: Record<string, any>) { | ||
const entries = Object.entries(router); | ||
return defineMiddleware((context, next) => | ||
sequence( | ||
...entries | ||
.filter(([path]) => multimatch(context.url.pathname, path).length > 0) | ||
.map(([_, handler]) => handler) | ||
)(context, next) | ||
); | ||
export function defineMiddlewareRouter(entries: [string, MiddlewareHandler][]): MiddlewareHandler { | ||
return defineMiddleware((context, next) => { | ||
const match = entries.find(([path]) => multimatch(context.url.pathname, path).length > 0); | ||
|
||
if (!match) { | ||
return next(); | ||
} | ||
|
||
const [, routeHandler] = match; | ||
|
||
return routeHandler(context, next); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.