Skip to content

Commit

Permalink
Adds http.Server options as configuration server.options
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Aug 11, 2022
1 parent ddf834a commit 5f6efb5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-ports-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Adds http.Server options as configuration `server.options`
28 changes: 23 additions & 5 deletions packages/core/src/scripts/run/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import type { ListenOptions } from 'net';
import url from 'url';
import util from 'util';
import express from 'express';
Expand Down Expand Up @@ -115,7 +116,7 @@ exports.default = function (req, res) { return res.send(x.toString()) }
// so we shouldn't log something like "hey, we reloaded your config"
// because it would go off at times when the user didn't change their config

const version = await fetch(`http://localhost:${port}/api/__keystone_api_build`).then(x =>
const version = await fetch(`http://localhost:${httpOptions.port}/api/__keystone_api_build`).then(x =>
x.text()
);
if (lastVersion !== version) {
Expand Down Expand Up @@ -231,19 +232,36 @@ exports.default = function (req, res) { return res.send(x.toString()) }
res.sendFile(devLoadingHTMLFilepath);
});

const port = config.server?.port || process.env.PORT || 3000;
let initKeystonePromiseResolve: () => void | undefined;
let initKeystonePromiseReject: (err: any) => void | undefined;
let initKeystonePromise = new Promise<void>((resolve, reject) => {
initKeystonePromiseResolve = resolve;
initKeystonePromiseReject = reject;
});
const server = app.listen(port, (err?: any) => {

const httpOptions: ListenOptions = {
port: 3000,
};

// preference env.PORT if supplied
if (config?.server && 'port' in config.server) {
httpOptions.port = config.server.port;
}

if (config?.server && 'options' in config.server && typeof config.server.options === 'object') {
Object.assign(httpOptions, config.server.options);
}

if ('PORT' in process.env) {
httpOptions.port = parseInt(process.env.PORT || '');
}

const server = app.listen(httpOptions, (err?: any) => {
if (err) throw err;
// We start initialising Keystone after the dev server is ready,
console.log(`⭐️ Dev Server Starting on http://localhost:${port}`);
console.log(`⭐️ Dev Server Starting on http://localhost:${httpOptions.port}`);
console.log(
`⭐️ GraphQL API Starting on http://localhost:${port}${
`⭐️ GraphQL API Starting on http://localhost:${httpOptions.port}${
config.graphql?.path || '/api/graphql'
}`
);
Expand Down
23 changes: 20 additions & 3 deletions packages/core/src/scripts/run/start.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import type { ListenOptions } from 'net';
import * as fs from 'fs-extra';
import { createSystem } from '../../lib/createSystem';
import { initConfig } from '../../lib/config/initConfig';
Expand Down Expand Up @@ -44,9 +45,25 @@ export const start = async (cwd: string) => {
console.log(`✅ Admin UI ready`);
}

const port = config.server?.port || process.env.PORT || 3000;
httpServer.listen(port, (err?: any) => {
const options: ListenOptions = {
port: 3000,
};

// preference env.PORT if supplied
if (config?.server && 'port' in config.server) {
options.port = config.server.port;
}

if (config?.server && 'options' in config.server && typeof config.server.options === 'object') {
Object.assign(options, config.server.options);
}

if ('PORT' in process.env) {
options.port = parseInt(process.env.PORT || '');
}

httpServer.listen(options, (err?: any) => {
if (err) throw err;
console.log(`⭐️ Server Ready on http://localhost:${port}`);
console.log(`⭐️ Server Ready on http://localhost:${options.port}`);
});
};
14 changes: 11 additions & 3 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Server } from 'http';
import type { ListenOptions } from 'net';
import type { Config } from 'apollo-server-express';
import { CorsOptions } from 'cors';
import express from 'express';
Expand Down Expand Up @@ -169,8 +170,6 @@ type HealthCheckConfig = {
export type ServerConfig<TypeInfo extends BaseKeystoneTypeInfo> = {
/** Configuration options for the cors middleware. Set to `true` to use core Keystone defaults */
cors?: CorsOptions | true;
/** Port number to start the server on. Defaults to process.env.PORT || 3000 */
port?: number;
/** Maximum upload file size allowed (in bytes) */
maxFileSize?: number;
/** Health check configuration. Set to `true` to add a basic `/_healthcheck` route, or specify the path and data explicitly */
Expand All @@ -181,7 +180,16 @@ export type ServerConfig<TypeInfo extends BaseKeystoneTypeInfo> = {
createContext: CreateRequestContext<TypeInfo>
) => void | Promise<void>;
extendHttpServer?: (server: Server, createContext: CreateRequestContext<TypeInfo>) => void;
};
} & (
| {
/** Port number to start the server on. Defaults to process.env.PORT || 3000 */
port?: number;
}
| {
/** node http.Server options */
options?: ListenOptions;
}
);

// config.graphql

Expand Down

0 comments on commit 5f6efb5

Please sign in to comment.