fix: allow setting server config from fresh config (#1889)

This commit is contained in:
Marvin Hagemeister
2023-10-09 13:59:30 +02:00
committed by GitHub
parent 4fac2a22a6
commit 32eb61c95d
11 changed files with 266 additions and 6 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ export async function dev(
config.dev = true;
config.loadSnapshot = false;
const ctx = await getServerContext(config);
await startFromContext(ctx, options);
await startFromContext(ctx, config.server);
} else {
// Legacy entry point: Back then `dev.ts` would call `main.ts` but
// this causes duplicate plugin instantiation if both `dev.ts` and
+9 -3
View File
@@ -1,8 +1,11 @@
import { ServerContext } from "./context.ts";
import { colors } from "./deps.ts";
import { ServeHandler, StartOptions } from "./types.ts";
import { ServeHandler } from "./types.ts";
export async function startFromContext(ctx: ServerContext, opts: StartOptions) {
export async function startFromContext(
ctx: ServerContext,
opts: Partial<Deno.ServeTlsOptions>,
) {
if (!opts.onListen) {
opts.onListen = (params) => {
console.log();
@@ -56,7 +59,10 @@ export async function startFromContext(ctx: ServerContext, opts: StartOptions) {
}
}
async function bootServer(handler: ServeHandler, opts: StartOptions) {
async function bootServer(
handler: ServeHandler,
opts: Partial<Deno.ServeTlsOptions>,
) {
// @ts-ignore Ignore type error when type checking with Deno versions
if (typeof Deno.serve === "function") {
// @ts-ignore Ignore type error when type checking with Deno versions
+26
View File
@@ -66,8 +66,34 @@ export async function getFreshConfigWithDefaults(
staticDir: "",
render: opts.render,
router: opts.router,
server: opts.server ?? {},
};
if (opts.cert) {
config.server.cert = opts.cert;
}
if (opts.hostname) {
config.server.hostname = opts.hostname;
}
if (opts.key) {
config.server.key = opts.key;
}
if (opts.onError) {
config.server.onError = opts.onError;
}
if (opts.onListen) {
config.server.onListen = opts.onListen;
}
if (opts.port) {
config.server.port = opts.port;
}
if (opts.reusePort) {
config.server.reusePort = opts.reusePort;
}
if (opts.signal) {
config.server.signal = opts.signal;
}
config.build.outDir = opts.build?.outDir
? parseFileOrUrl(opts.build.outDir, base)
: join(base, "_fresh");
+1 -1
View File
@@ -100,5 +100,5 @@ export async function start(routes: Manifest, opts: StartOptions = {}) {
skipSnapshot: false,
dev: false,
});
await startFromContext(ctx, opts);
await startFromContext(ctx, opts.server ?? opts);
}
+56 -1
View File
@@ -23,7 +23,7 @@ export interface DenoConfig {
// --- APPLICATION CONFIGURATION ---
export type StartOptions = Partial<Deno.ServeTlsOptions> & FreshOptions;
export type StartOptions = FreshOptions;
export interface FreshOptions {
build?: {
@@ -44,6 +44,60 @@ export interface FreshOptions {
plugins?: Plugin[];
staticDir?: string;
router?: RouterOptions;
server?: Partial<Deno.ServeTlsOptions>;
// Older versions of Fresh merged the `Deno.ServeTlsOptions` directly.
// We've moved this to `server`.
/**
* Server private key in PEM format
* @deprecated Use `server.cert` instead
*/
cert?: string;
/**
* Cert chain in PEM format
* @deprecated Use `server.key` instead
*/
key?: string;
/**
* The port to listen on.
* @default {8000}
* @deprecated Use `server.port` instead
*/
port?: number;
/**
* A literal IP address or host name that can be resolved to an IP address.
*
* __Note about `0.0.0.0`__ While listening `0.0.0.0` works on all platforms,
* the browsers on Windows don't work with the address `0.0.0.0`.
* You should show the message like `server running on localhost:8080` instead of
* `server running on 0.0.0.0:8080` if your program supports Windows.
*
* @default {"0.0.0.0"}
* @deprecated Use `server.hostname` instead
*/
hostname?: string;
/**
* An {@linkcode AbortSignal} to close the server and all connections.
* @deprecated Use `server.signal` instead
*/
signal?: AbortSignal;
/**
* Sets `SO_REUSEPORT` on POSIX systems.
* @deprecated Use `server.reusePort` instead
*/
reusePort?: boolean;
/**
* The handler to invoke when route handlers throw an error.
* @deprecated Use `server.onError` instead
*/
onError?: (error: unknown) => Response | Promise<Response>;
/**
* The callback which is called when the server starts listening.
* @deprecated Use `server.onListen` instead
*/
onListen?: (params: { hostname: string; port: number }) => void;
}
export interface InternalFreshOptions {
@@ -60,6 +114,7 @@ export interface InternalFreshOptions {
plugins: Plugin[];
staticDir: string;
router?: RouterOptions;
server: Partial<Deno.ServeTlsOptions>;
}
export interface RouterOptions {