Saying Hello

A Behavior in GreenLighting is an actor in the actor model. It has a clearly defined and limited responsibility in the bigger scope of a project and can listen and respond to messages. Unlike traditional models, behaviors communicate through these messages with each other.

For our tutorial, we need two behaviors that can listen to HTTP Requests and respond with HTTP content. Let's focus on our GET request first.

Hello Behavior

Create a new package inside your project named behaviors . Add a new class named HelloBehavior to the package. Use the code below to get started:

behaviors/HelloBehavior.java
package com.ociweb.behaviors;

import com.ociweb.HelloField;
import com.ociweb.gl.api.GreenRuntime;
import com.ociweb.gl.api.HTTPRequestReader;
import com.ociweb.gl.api.HTTPResponseService;
import com.ociweb.gl.api.RestListener;
import com.ociweb.pronghorn.network.config.HTTPContentTypeDefaults;
import com.ociweb.pronghorn.pipe.StructuredReader;

public class HelloBehavior implements RestListener {
    private HTTPResponseService responseService;

    public HelloBehavior(GreenRuntime runtime) {
        responseService = runtime.newCommandChannel().newHTTPResponseService();
    }
    @Override
    public boolean restRequest(HTTPRequestReader request) {
    }
}
  • The behavior implements the RestListener interface. It provides everything we need to respond to an API request.

  • We require a HTTPResponseService object for outputting a HTTP response. It is created in the constructor using the provided runtime object.

Next, let's expand the restRequest method to actually render a plaintext response back to the browser:

behaviors/HelloBehavior.java
@Override
public boolean restRequest(HTTPRequestReader request) {
    if(!request.isVerbGet()) {
        return responseService.publishHTTPResponse(request, 404);
    }        
    StructuredReader record = request.structured();
    String name = record.readText(HelloField.NAME);

    return responseService.publishHTTPResponse(request, 200,
            HTTPContentTypeDefaults.PLAIN, w -> {
                w.append("Hello World, ").append(name).append("!");
            });
}
  • On line 3, we make sure that this is a GET request.

  • On line 6, we use a StructuredReader to parse the request URL. This maps all the known fields to the fields discovered in the request.

  • On line 7, we access the record and read the name field previously registered in our route.

  • On line 9, we use the responseService mentioned above to publish the HTTP response.

    • The status code is 200 (Success).

    • The content type we are sending back is PLAIN (simple text).

    • We use a writer lambda to create our text response. This allows for garbage-free String concatenation. First, we append the phrase "Hello World", then the name, and then an exclamation point.

At this point, we're almost done with our GET request. There's one more thing we need to do: Actually register our brand new behavior.

Open up RestServer again and edit the declareBehavior method to look like this:

RestServer.java
@Override
public void declareBehavior(GreenRuntime runtime) {
    runtime.addRestListener(new HelloBehavior(runtime))
            .includeRoutesByAssoc(HelloStruct.HELLO_WORLD);
}

We pass in runtime to our new HelloBehavior and include the HELLO_WORLD route previously associated with the /api/hello/${NAME} path.

Congratulations, you created your first GreenLightning API endpoint. If you would like, you can start your server by running main inside of GreenLightning.java and navigate to the URL output in the console. Use a utility such as Postman to test your endpoint.

On the next page, we will take it a step further and add JSON parsing and rendering to the mix.

Last updated