koopjs / koop-cli

CLI tool to build Koop applications and plugins
Other
11 stars 6 forks source link

Support HTTPS (SSL) for "koop serve" command #70

Closed gavinr closed 3 years ago

gavinr commented 3 years ago

For development workflows where you're working on a provider and using the koop-cli to run a dev/debug environment, it would be valuable to have the option to have the server that runs when you run koop serve to support HTTPS/SSL. This is because more places (now notably the ArcGIS Online Map Viewer) are enforcing HTTPS even for development workflows.

gavinr commented 3 years ago

Workaround when you're in a provider add-in:

  1. Generate a self signed certificate: server.cert and server.key files and place them in the root directory.
  2. Create a new file test/server.js with contents:
    const Koop = require("koop");
    const fs = require("fs");
    const https = require("https");
    // require the src directory
    const testProvider = require("../src/index");
    // initiate a koop app
    const koop = new Koop();
    // register koop plugins
    koop.register(testProvider);
    // start the server
    https
      .createServer(
        {
          key: fs.readFileSync("server.key"),
          cert: fs.readFileSync("server.cert"),
        },
        koop.server
      )
      .listen(3000, function () {
        console.log(
          "Example app listening on port 3000! Go to https://localhost:3000/"
        );
      });
  3. Change the npm start command to be koop serve test/server.js --debug --watch

(thanks to @haoliangyu for this example)

haoliangyu commented 3 years ago

It is valuable to provide an optional HTTPS dev server, particularly when working with Esri products.

Since it is simply to create one with ssh cert and key files, I see it possible to add two options in the serve command:

koop serve --ssh-cert=<path-to-cert> --ssh-key=<path-to-key>

When both options are set, a HTTPS server will be started instead.

haoliangyu commented 3 years ago

@gavinr The update is out at v1.1.0.