Skip to content

Instantly share code, notes, and snippets.

@sylvaindethier
Created May 7, 2024 06:48
Show Gist options
  • Save sylvaindethier/d6d374da4fdfbc75fa4c2a945e5f3f33 to your computer and use it in GitHub Desktop.
Save sylvaindethier/d6d374da4fdfbc75fa4c2a945e5f3f33 to your computer and use it in GitHub Desktop.
Fastify HTTPS server with @anchordotdev/cli
//@ts-expect-error no declaration files
import {createSNICallback} from "anchor-pki/auto-cert/sni-callback";
//@ts-expect-error no declaration files
import { TermsOfServiceAcceptor } from "anchor-pki/auto-cert/terms-of-service-acceptor";
import https from "node:https";
import type { FastifyServerFactory } from "fastify";
const serverFactory: FastifyServerFactory = (handler) => {
const SNICallback = createSNICallback({
name: 'sni-callback',
tosAcceptors: TermsOfServiceAcceptor.createAny(),
cacheDir: 'tmp/acme'
// The following are defaults
//
// directoryUrl: process.env.ACME_DIRECTORY_URL,
// contact: process.env.ACME_CONTACT,
// externalAccountBinding: {
// kid: process.env.ACME_KID,
// hmacKey: process.env.ACME_HMAC_KEY
// },
});
return https.createServer({ SNICallback }, handler);
}
import Fastify from "fasitify";
import { env } from "node:process";
const fastify = Fastify({
// serverFactory only for local environment, assuming it's when NODE_ENV is "development"
...(env["NODE_ENV"] === "development" ? { serverFactory } : {})
serverFactory,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment