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

Modularize the concept of "servers" (including the Admin) #1150

Closed
jesstelford opened this issue May 15, 2019 · 1 comment
Closed

Modularize the concept of "servers" (including the Admin) #1150

jesstelford opened this issue May 15, 2019 · 1 comment

Comments

@jesstelford
Copy link
Contributor

From https://github.com/keystonejs/keystone-5/pull/960#issuecomment-480101846

Goals

1️⃣ Maintain separation between index.js & server.js ("Custom Server").
2️⃣ You'd setup UI's/Servers in one or the other of index.js or server.js. Ie; If you have a Custom Server, you should not export servers: from index.js (technically you could, it just wouldn't do anything).
3️⃣ Reframe the Admin UI from being "something special" to "Just another frontend"
4️⃣ All the UI's / Servers follow a common API (very similar to how we already do fields!)
5️⃣ The custom server is still un-opinionated, and you can really do whatever you want there.
6️⃣ All the servers need a build step for preparing for a production deploy
7️⃣ Without a Custom Server, the CLI would become just index.servers.forEach(server => app.use(server.middleware()))

Example

index.js

const { Keystone } = require('@keystone-alpha/keystone');
const AdminUI = require('@keystone-alpha/server-admin');
const NextUI = require('@keystone-alpha/server-next');
const StaticUI = require('@keystone-alpha/server-static');

const keystone = new Keystone(/* ... */);

const admin = new AdminUI({
  path: '/admin',
  keystone,
  session: authStrategy.sessionHandler(),
});

const next = new NextUI({
  path: '/'
  session: authStrategy.sessionHandler(),
  nextConfig: { /* ... */ },
});

const staticFiles = new StaticUI({
  path: `${__dirname}/static`,
  route: '/public',
});

module.exports = {
  keystone,
  servers: [
    staticFiles,
    admin,
    next
  ],
};

- OR -

server.js

const keystone = require('@keystone-alpha/core');
const path = require('path');
const PORT = process.env.PORT || 3000;
const AdminUI = require('@keystone-alpha/server-admin');
const NextUI = require('@keystone-alpha/server-next');

const { keystone } = require('./index');

keystone.prepare().then(() => {
  const app = express();

  const admin = new AdminUI({
    path: '/admin',
    keystone,
    session: authStrategy.sessionHandler(),
  });

  const next = new NextUI({
    path: '/'
    session: authStrategy.sessionHandler(),
    nextConfig: { /* ... */ },
  });

  app.use(admin.middleware());
  app.use(next.middleware());

  app.listen(3000);
}).catch(error => {
  console.error(error);
  process.exit(1);
});

@keystone-alpha/server-next

const next = require('next');
module.exports = class {
  constructor(nextConfig) {
    this.config = nextConfig;
  }

  middleware(server) {
    const nextApp = next(nextConfig);
    server.use(nextApp.requestHandler());
  }

  build() {
    this.next.build({ out: this.config.distDir });
  }
}

@keystone-alpha/server-static

module.exports = class {
  constructor(config) {
    this.config = config;
  }

  middleware(server) {
    server.use(express.static(this.config.route, this.config.path));
  }

  build() {
    // noop
  }
}

@keystone-alpha/server-create-react-app

module.exports = class {
  constructor(craConfig) {
    /* ... */
  }

  middleware(server) {
    server.use(express.static(this.config.path, this.config.craOutDir));
  }

  build() {
    createReactApp.build({ out: this.config.craOutDir });
  }
}
jesstelford pushed a commit that referenced this issue May 20, 2019
* Move admin UI session middleware registration to admin UI internals

* Specify custom servers from within the index.js file

Closes #960
Refs #1150

* Merge core & keystone packages to make testing easier

* Rename 'servers' to 'apps'

* Pass keystone instance to AdminUIMeta generator

* Custom servers import the keystone instance directly + prepare is an instance method. Also must explicitly declare the GraphQL API instance as an app

* Static file app uses { path, src } for API

* Named exports for Apps

* Rename disableDefaultRoute option to enableDefaultRoute and default to false

* Linting & test fixes
@jesstelford
Copy link
Contributor Author

Done in #1109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant