Skip to content

Instantly share code, notes, and snippets.

@bibarsov
Created January 21, 2020 10:27
Show Gist options
  • Save bibarsov/10fa78b9c47cd2b898127ace22f5a89f to your computer and use it in GitHub Desktop.
Save bibarsov/10fa78b9c47cd2b898127ace22f5a89f to your computer and use it in GitHub Desktop.
Nginx-Closure Example
import nginx.clojure.java.ArrayMap;
import nginx.clojure.java.NginxJavaRingHandler;
import java.io.IOException;
import java.util.Map;
import static nginx.clojure.MiniConstants.CONTENT_TYPE;
import static nginx.clojure.MiniConstants.NGX_HTTP_OK;
public class TestRingHandler implements NginxJavaRingHandler {
private int counter = 0;
@Override
public Object[] invoke(Map<String, Object> req) throws IOException {
return new Object[]{
NGX_HTTP_OK, //http status 200
ArrayMap.create(
CONTENT_TYPE, "text/plain",
"X-Header", "foobar"
), //headers map
"Hello, Java & Nginx! Counter: " + (counter++)
};
}
}
@bibarsov
Copy link
Author

bibarsov commented Jan 21, 2020

Nginx.conf

        user USERNAME GROUPNAME; #should be declared to provide access to jars

        location /java {
            handler_type 'java';
            handler_name 'TestRingHandler';
        }
  
        #......
         jvm_path "/Library/Java/JavaVirtualMachines/jdkXXXXX.jdk/Contents/Home/jre/lib/server/libjvm.dylib";
         jvm_classpath "jars/nginx-clojure-XXX.jar:libs/app.jar"; #app.jar contains TestRingHandler
  

build.gradle for app.jar

//....

repositories {
    mavenLocal()
    mavenCentral()
    maven { url = "https://repo.clojars.org" }
}

dependencies {
    implementation 'nginx-clojure:nginx-clojure:0.5.1'
    //...other deps
}
task fatJar(type: Jar) {
    from {
        configurations.runtimeClasspath.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    with jar
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment