Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial implementation for configureServer #4903

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples-next/basic/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ export default auth.withAuth(
// // store: redisSessionStore({ client: redis.createClient() }),
// secret: sessionSecret,
// }),
server: {
configureServer(app) {
app.set('trust proxy', true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting trust proxy in this place give no effect seems because express is initialized earlier default values for serve Admin UI? Or maybe this is not enough to make session cookies working.

For investigate this we can enable verbose logging of express via starting DEBUG=express:* yarn dev and I see that this code is initialized later, that express startup. Even if I place console.log('configuring trust proxy'); in first line of examples-next/basic/keystone.ts file - it will be executed after initializing of express, here is output:

server: {
      configureServer(app) {
        console.log('CONFIGURING TRUST PROXY');
        app.set('trust proxy', true);
      }

The output is:

$ DEBUG=express:* yarn dev
yarn run v1.22.10
$ keystone-next dev
🤞 Starting Keystone
  express:application set "x-powered-by" to true +0ms
  express:application set "etag" to 'weak' +1ms
  express:application set "etag fn" to [Function: generateETag] +1ms
  express:application set "env" to 'development' +1ms
  express:application set "query parser" to 'extended' +0ms
  express:application set "query parser fn" to [Function: parseExtendedQueryString] +1ms
  express:application set "subdomain offset" to 2 +0ms
  express:application set "trust proxy" to false +0ms
  express:application set "trust proxy fn" to [Function: trustNone] +0ms
  express:application booting in development mode +0ms
  express:application set "view" to [Function: View] +0ms
  express:application set "views" to '/srv/korepov/domains/ks-t1.k.dev.digiterra.pro/src/keystone/examples-next/basic/views' +0ms
  express:application set "jsonp callback name" to 'callback' +1ms
CONFIGURING TRUST PROXY
  express:router use '/' query +3s
  express:router:layer new '/' +1ms
  express:router use '/' expressInit +1ms
  express:router:layer new '/' +0ms
  express:router use '/__keystone_dev_status' <anonymous> +0ms
  express:router:layer new '/__keystone_dev_status' +0ms
  express:router use '/' <anonymous> +1ms
  express:router:layer new '/' +0ms
⭐️ Dev Server Ready on http://localhost:3000
✨ Generating graphQL schema
✨ Connecting to the database
(node:3661400) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
✨ Generating Admin UI code
✨ Creating server
  express:application set "x-powered-by" to true +1s
  express:application set "etag" to 'weak' +0ms
  express:application set "etag fn" to [Function: generateETag] +1ms
  express:application set "env" to 'development' +0ms
  express:application set "query parser" to 'extended' +0ms
  express:application set "query parser fn" to [Function: parseExtendedQueryString] +0ms
  express:application set "subdomain offset" to 2 +0ms
  express:application set "trust proxy" to false +1ms
  express:application set "trust proxy fn" to [Function: trustNone] +0ms
  express:application booting in development mode +0ms
  express:application set "view" to [Function: View] +0ms
  express:application set "views" to '/srv/korepov/domains/ks-t1.k.dev.digiterra.pro/src/keystone/examples-next/basic/views' +1ms
  express:application set "jsonp callback name" to 'callback' +0ms
✨ Preparing GraphQL Server
  express:router use '/' query +58ms

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And even more - this code seems isn't even called, because adding:

    server: {
      configureServer(app) {
        console.log('CONFIGURING TRUST PROXY');
        app.set('trust proxy', true);
      },
    },

gives no output of this line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may only work if you are using this branch

},
},
})
);
3 changes: 3 additions & 0 deletions packages-next/keystone/src/lib/createExpressServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export const createExpressServer = async (
server.use(cors(corsConfig));
}

if (config.server?.configureServer) {
config.server.configureServer(server);
}
const sessionStrategy = config.session ? config.session() : undefined;

console.log('✨ Preparing GraphQL Server');
Expand Down
1 change: 1 addition & 0 deletions packages-next/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"graphql": "^15.5.0"
},
"repository": "https://github.com/keystonejs/keystone/tree/master/packages-next/types"
Expand Down
3 changes: 3 additions & 0 deletions packages-next/types/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ConnectOptions } from 'mongoose';
import type { Express } from 'express';
import { CorsOptions } from 'cors';
import type { GraphQLSchema } from 'graphql';
import { IncomingMessage } from 'http';
Expand Down Expand Up @@ -98,6 +99,8 @@ export type ServerConfig = {
cors?: CorsOptions | true;
/** Port number to start the server on. Defaults to process.env.PORT || 3000 */
port?: number;
/** Function to configure express, similar to configureExpress from production */
configureServer?: (app: Express) => void;
};

// config.graphql
Expand Down