Created
October 8, 2014 19:01
-
-
Save pjagielski/e8f922c93b1cb5b89b10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@groovy.lang.Grab("com.netflix.rxnetty:rx-netty:0.3.14") | |
import io.netty.buffer.ByteBuf | |
import io.reactivex.netty.RxNetty | |
import io.reactivex.netty.protocol.http.server.HttpServer | |
import io.reactivex.netty.protocol.http.server.HttpServerPipelineConfigurator | |
import io.reactivex.netty.protocol.http.server.HttpServerRequest | |
import io.reactivex.netty.protocol.http.server.HttpServerResponse | |
import io.reactivex.netty.protocol.http.server.RequestHandler | |
import rx.functions.Func1 | |
import java.nio.charset.Charset | |
import java.util.concurrent.TimeUnit | |
def port = 8192 | |
HttpServer<ByteBuf, ByteBuf> server = RxNetty.newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() { | |
@Override | |
public rx.Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { | |
final long start = System.nanoTime() | |
return request.getContent().map(new Func1<ByteBuf, Void>() { | |
@Override | |
public Void call(ByteBuf byteBuf) { | |
long elapsed = System.nanoTime() - start | |
println "Took $elapsed nanos, " + TimeUnit.MILLISECONDS.convert(elapsed, TimeUnit.NANOSECONDS) + " ms" | |
def body = byteBuf.toString(Charset.defaultCharset()); | |
println "Got : " + body | |
response.writeString(body.toUpperCase()); | |
return null; | |
} | |
}).ignoreElements(); | |
} | |
}).pipelineConfigurator(new HttpServerPipelineConfigurator<ByteBuf, ByteBuf>()).build(); | |
System.out.println("Starting server..."); | |
server.startAndWait() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment