From 745023cfc2f1008051a08e19ce84469966b82aa4 Mon Sep 17 00:00:00 2001 From: Daniel Holmgren Date: Fri, 19 May 2023 10:31:39 -0500 Subject: [PATCH] Improve .env (#20) * describeFeedGenerator route + multiple feeds * tweak readme * improve env --- .env.example | 12 +++++++++++- src/index.ts | 14 ++++++++++---- src/server.ts | 11 ++--------- src/well-known.ts | 10 +++++++--- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index ebd0e2b..4cfdab3 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,14 @@ +# Whichever port you want to run this on FEEDGEN_PORT=3000 + +# Set to something like db.sqlite to store persistently FEEDGEN_SQLITE_LOCATION=":memory:" + +# Don't change unless you're working in a different environment than the primary Bluesky network FEEDGEN_SUBSCRIPTION_ENDPOINT="wss://bsky.social" -FEEDGEN_SERVICE_DID="did:example:test" + +# Set this to the hostname that you intend to run the service at +FEEDGEN_HOSTNAME="example.com" + +# Only use this if you want a service did different from did:web +# FEEDGEN_SERVICE_DID="did:plc:abcde..." \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a8486d7..4960044 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,17 @@ import FeedGenerator from './server' const run = async () => { dotenv.config() + const hostname = maybeStr(process.env.FEEDGEN_HOSTNAME) ?? 'example.com' + const serviceDid = + maybeStr(process.env.FEEDGEN_SERVICE_DID) ?? `did:web:${hostname}` const server = FeedGenerator.create({ - port: maybeInt(process.env.FEEDGEN_PORT), - sqliteLocation: maybeStr(process.env.FEEDGEN_SQLITE_LOCATION), - subscriptionEndpoint: maybeStr(process.env.FEEDGEN_SUBSCRIPTION_ENDPOINT), - serviceDid: maybeStr(process.env.FEEDGEN_SERVICE_DID), + port: maybeInt(process.env.FEEDGEN_PORT) ?? 3000, + sqliteLocation: maybeStr(process.env.FEEDGEN_SQLITE_LOCATION) ?? ':memory:', + subscriptionEndpoint: + maybeStr(process.env.FEEDGEN_SUBSCRIPTION_ENDPOINT) ?? + 'wss://bsky.social', + hostname, + serviceDid, }) await server.start() console.log( diff --git a/src/server.ts b/src/server.ts index b57dffe..4e1449c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -29,14 +29,7 @@ export class FeedGenerator { this.cfg = cfg } - static create(config?: Partial) { - const cfg: Config = { - port: config?.port ?? 3000, - hostname: config?.hostname ?? 'feed-generator.test', - sqliteLocation: config?.sqliteLocation ?? ':memory:', - subscriptionEndpoint: config?.subscriptionEndpoint ?? 'wss://bsky.social', - serviceDid: config?.serviceDid ?? 'did:example:test', - } + static create(cfg: Config) { const app = express() const db = createDb(cfg.sqliteLocation) const firehose = new FirehoseSubscription(db, cfg.subscriptionEndpoint) @@ -63,7 +56,7 @@ export class FeedGenerator { feedGeneration(server, ctx) describeGenerator(server, ctx) app.use(server.xrpc.router) - app.use(wellKnown(cfg.hostname)) + app.use(wellKnown(ctx)) return new FeedGenerator(app, db, firehose, cfg) } diff --git a/src/well-known.ts b/src/well-known.ts index b869b6c..11c2304 100644 --- a/src/well-known.ts +++ b/src/well-known.ts @@ -1,17 +1,21 @@ import express from 'express' +import { AppContext } from './config' -const makeRouter = (serverHostname: string) => { +const makeRouter = (ctx: AppContext) => { const router = express.Router() router.get('/.well-known/did.json', (_req, res) => { + if (!ctx.cfg.serviceDid.endsWith(ctx.cfg.hostname)) { + return res.sendStatus(404) + } res.json({ '@context': ['https://www.w3.org/ns/did/v1'], - id: `did:web:${serverHostname}`, + id: ctx.cfg.serviceDid, service: [ { id: '#bsky_fg', type: 'BskyFeedGenerator', - serviceEndpoint: `https://${serverHostname}`, + serviceEndpoint: `https://${ctx.cfg.hostname}`, }, ], })