add did-web example

This commit is contained in:
dholms 2023-05-11 18:43:47 -05:00
parent f9ccf112e1
commit 050047fcd0
3 changed files with 26 additions and 0 deletions

View File

@ -9,6 +9,7 @@ export type AppContext = {
export type Config = { export type Config = {
port: number port: number
hostname: string
sqliteLocation: string sqliteLocation: string
subscriptionEndpoint: string subscriptionEndpoint: string
serviceDid: string serviceDid: string

View File

@ -7,6 +7,7 @@ import feedGeneration from './feed-generation'
import { createDb, Database, migrateToLatest } from './db' import { createDb, Database, migrateToLatest } from './db'
import { FirehoseSubscription } from './subscription' import { FirehoseSubscription } from './subscription'
import { AppContext, Config } from './config' import { AppContext, Config } from './config'
import wellKnown from './well-known'
export class FeedGenerator { export class FeedGenerator {
public app: express.Application public app: express.Application
@ -30,6 +31,7 @@ export class FeedGenerator {
static create(config?: Partial<Config>) { static create(config?: Partial<Config>) {
const cfg: Config = { const cfg: Config = {
port: config?.port ?? 3000, port: config?.port ?? 3000,
hostname: config?.hostname ?? 'feed-generator.test',
sqliteLocation: config?.sqliteLocation ?? ':memory:', sqliteLocation: config?.sqliteLocation ?? ':memory:',
subscriptionEndpoint: config?.subscriptionEndpoint ?? 'wss://bsky.social', subscriptionEndpoint: config?.subscriptionEndpoint ?? 'wss://bsky.social',
serviceDid: config?.serviceDid ?? 'did:example:test', serviceDid: config?.serviceDid ?? 'did:example:test',
@ -59,6 +61,7 @@ export class FeedGenerator {
} }
feedGeneration(server, ctx) feedGeneration(server, ctx)
app.use(server.xrpc.router) app.use(server.xrpc.router)
app.use(wellKnown(cfg.hostname))
return new FeedGenerator(app, db, firehose, cfg) return new FeedGenerator(app, db, firehose, cfg)
} }

22
src/well-known.ts Normal file
View File

@ -0,0 +1,22 @@
import express from 'express'
const makeRouter = (serverHostname: string) => {
const router = express.Router()
router.get('/.well-known/did.json', (_req, res) => {
res.json({
'@context': ['https://www.w3.org/ns/did/v1'],
id: `did:web:${serverHostname}`,
service: [
{
id: '#bsky_fg',
type: 'BskyFeedGenerator',
serviceEndpoint: `https://${serverHostname}`,
},
],
})
})
return router
}
export default makeRouter