0.1.6 • Published 4 years ago

@mcneel/compute.rhino3d.appserver v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Rhino Compute AppServer Example

This is a node.js server built with express which shows how to serve a few Grasshopper definitions and expose their remote solving via a basic API.

npm GitHub package.json version node-current (scoped)

  1. Use Cases
    1. Running this app locally
    2. Deploying this app on Heroku
    3. Installing from npm
  2. API Endpoints
  3. Example

Use Cases

You can use this project in several use cases, but they all assume you've setup a Rhino compute server and that server is accessible locally or from another computer. To test this, navigate in a browser to http://your-compute-server-address/version. For more information on setting up your compute server, please visit: https://github.com/mcneel/compute.rhino3d/blob/master/docs/installation.md

Running this app locally

(alongside a compute geometry server) 1. Open up a terminal. Clone this repository and navigate to the resulting directory:

$ git clone https://github.com/mcneel/compute.rhino3d.appserver.git
$ cd compute.rhino3d.appserver
  1. Install dependencies:
$ npm install
  1. Start the application from the terminal in development mode. This uses nodemon to restart the server in case you make changes. It also runs the linting script.
# will look for definitions in the ./files/ directory and use http://localhost:8081 as the compute server address

$ npm run dev

# alternatively, if you'd like to define a different directory for your definitions or use a different address for the compute server (check the 'dev' script in packages.json):

$ nodemon --inspect ./bin/www --definitions C:\\data\\definitions --computeUrl http://localhost:8081/  --exec \"npm run lint && node\""
  1. Navigate to http://localhost:3000/example/ to test the example included in this project.
  2. If you'd like to add any definitions, add .gh or .ghx files to the ./files/ directory.
  3. If you'd like to run this application locally without any of the development tools you can use the following:
# uses the ./files/ directory for definitions and http://localhost:8081 as the compute server url (same as "npm run start" defined in package.json)
$ node ./bin/www

# or with command line arguments (same as "npm run start-args" defined in package.json)
$ node ./bin/www --definitions C:\\data\\definitions --computeUrl http://localhost:8081/

Deploying this app on Heroku

(with Compute Geometry server running on a Windows Server)

Prerequisites

  1. You have an account on Heroku.
  2. You've downloaded and installed the Heroku CLI.

Setup

  1. If you haven't already done so, open up a terminal, clone this repository, and navigate to the resulting directory:
$ git clone https://github.com/mcneel/compute.rhino3d.appserver.git
$ cd compute.rhino3d.appserver
  1. This example comes with a few definitions in the ./files/ directory. If you would like to add any more definitions, you can do so now.
  2. From the terminal, login to Heroku:
$ heroku login
  1. From the terminal, create the Heroku application (you can specify a name here, otherwise heroku will assign a name to your app):
$ heroku create myappname

You will see that your app was created and has a domain and a git repository:

Creating myappname... done
https://myappname.herokuapp.com | https://git.heroku.com/myappname.git
  1. Add configuration variables. (This step can also be completed via the Heroku dashboard)
    • From the terminal, set the COMPUTE_URL variable. The VALUE of this should be the address to the server running compute. Ensure this address ends in a /.
      heroku config:set COMPUTE_URL=http://your-compute-server-address/
  2. Push the code to Heroku:
git push heroku master
  1. Finally, open the application. You should see your browser write some json formated information about the definitions.
heroku open
  1. Check out the example at https://myappname.herokuapp.com/example/
  2. Navigate in a browser to your Heroku dashboard. There you should see your new application. Click on your application name view it. You can view the logs by clicking on the More button and selecting View logs.

Installing from npm

You can install this app from npm either as a local or global package.

  1. In a terminal, install this application:
npm i compute-rhino3d-appserver

or globally

npm i compute-rhino3d-appserver -g
  1. Run this application with a few command line arguments to set your definitions folder and the compute geometry server URL:
compute-rhino3d-appserver --definitions C:\\data\\definitions --computeUrl http://localhost:8081/

API Endpoints

endpointmethodreturn typedescription
/GETapplication/jsonlist of the definitions on the server
/definitionName.ghGETapplication/jsondisplays information about the definition
/definitionName.ghPOSTapplication/jsonsolves a GH definition and returns json data

Example

To solve a GH definition you need to pass the definition name and input values to the appserver:

let data = {};
data.definition = 'BranchNodeRnd.gh';

// set the input values:
data.inputs = {
    'RH_IN:201:Length':document.getElementById('length').value,
    'RH_IN:201:Count':document.getElementById('count').value,
    'RH_IN:201:Radius':document.getElementById('radius').value
};

let xhr = new XMLHttpRequest();

xhr.open('POST', 'http://yourcomputeserver/' + data.definition, true);

// Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/json");

xhr.onreadystatechange = function() { // Call a function when the state changes.

    if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {

        // Request finished. Do processing here.  
        let result = JSON.parse(xhr.response);
        let data = JSON.parse(result.values[0].InnerTree['{ 0; }'][0].data);
        let mesh = rhino.CommonObject.decode(data);
            
    }
}

xhr.send(JSON.stringify(data));