Grails:80 – Proxy development with node.js – Caveats



Grails:80 – Proxy development with node.js – Caveats

0 0


proxy-presentation


On Github dyercode / proxy-presentation

Grails:80

Proxy development with node.js

Created by Jon Dyer / @dyercode

Why Do Anything

  • Grails run-app starts in it's own web container
  • Multiple projects == multiple containers == multiple ports
  • And we've seen more than enough of this:
    Error Server failed to start for port 8080: Address already in use
  • Ports must be preconfigured to properly link between apps
    http://localhost:8080/
    http://localhost:9090/rewards
  • Links within the apps must specify different ports for each app
  • The same for manually changing urls
  • Multiple ports are a pain

Requires in-app solutions for development environments.

Caveats

HTTPS is tricky

Problems with HTTPS

Node.js does support https support and there are actually instructions for using it on the node-http-proxy github. The difficulty here is Grails use a Java keystore and the node implementation expects

Start of workaround

grails.tomcat.keystorePath = "/path/to/my/keystore"
grails.tomcat.keystorePassword = "kspwd"

Toolbox

NSSM Alternatives

Why NSSM?

Commons-daemon is a pain

Required information:

  • Service name: what you want to name
  • NSSM is public domain as you can see from the complicated license:
NSSM is public domain. You may unconditionally use it and/or its source code for any purpose you wish.

Configuring commons-daemon can be a pain point I wanted a simpler alternative.

Set Up Node

Binary installer found at nodejs.org

Install http-proxy plugin From the command line:

Create/navigate to directory you want to keep the proxy Install http-proxy via npm
cd C:\devel\workspace\proxy
npm install http-proxy

(a package manager for node which supposedly doesn’t stand for anything)

Basic Node Proxy Server

proxy.js

var httpProxy = require('http-proxy');

var options = {
   router: {
      'localhost/rewards': 'localhost:8081/rewards',
      'localhost/thor': 'localhost:8082/thor',
      '': 'localhost:8080'
   }
};

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);

The left (key) defines the paths (and domains) of requests to handle The right (value) is where to forward those requests to. They take regular expressions.

Basic http proxy. The options define the routes. There are a few options for syntax.

"Require" http-proxy

var httpProxy = require('http-proxy');

Like a java import

Set up the options

var options = {
   router: {
      'localhost/rewards': 'localhost:8081/rewards',
      'localhost/thor': 'localhost:8082/thor',
      '': 'localhost:8080'
   }
};

router object defines the routes to use.

There are other options such as for https or different types of routes, but we don't really need them

Start the server

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);

creates an http proxy server with the options and save it into an object. Have it listen on port 80

Running the proxy server

cd C:\devel\workspace\proxy
node proxy.js

Grails changes

  • Each app still needs to start on a fixed port matching proxy configuration

BuildConfig.groovy

grails.server.port.http=8080

If someone knows a clean way to have user specific grails configs please let me know.

Set up NSSM

Install NSSM binary. Zip with Binaries found at nssm.cc Place relevant nssm.exe in your path either by adding it’s location to the path or placing the executable in a path. Install a new service.
npm install "Node Proxy"
Application will be your path to the node executeable
C:\devel\software\node\bin\node.exe
Options will be the path to your node.js app.
C:\devel\workspace\proxy\proxy.js

I use cygwin so I just copied the executeable into the /usr/local/bin dir. A symlink or something would work too.

Optionally, you can provide the entire setup via the command line.

Setting the path in windows

To Be Added

THE END

BY Jon Dyer / dyercode.com

dyercode@gmail.com