Skip to content

Commit

Permalink
Better support for port and address config in the low-level HTTP layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Nov 1, 2015
1 parent 07294df commit 1cdbd98
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions rapidoid-demo/src/main/java/org/rapidoid/demo/http/Main.java
Expand Up @@ -34,6 +34,8 @@ public class Main {
public static void main(String[] args) {
Conf.init(args);

On.port(8080);

On.get("/plaintext").plain("Hello world!");

On.get("/json").json(new Callable<Object>() {
Expand All @@ -42,8 +44,6 @@ public Object call() throws Exception {
return new Msg("Hello, World!");
}
});

On.listen(8080);
}

}
12 changes: 5 additions & 7 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/fast/On.java
Expand Up @@ -22,8 +22,6 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.net.TCPServer;

@Authors("Nikolche Mihajlovski")
@Since("4.3.0")
Expand All @@ -47,7 +45,7 @@ public static ServerSetup custom() {
}

private static void initialize() {
listen(Conf.port());
DEFAULT_SERVER_SETUP.listen();
}

public static synchronized OnAction get(String path) {
Expand All @@ -74,12 +72,12 @@ public static synchronized OnPage page(String path) {
return setup().page(path);
}

public static synchronized TCPServer listen(int port) {
return DEFAULT_SERVER_SETUP.listen(port);
public static synchronized ServerSetup port(int port) {
return DEFAULT_SERVER_SETUP.port(port);
}

public static synchronized TCPServer listen(String address, int port) {
return DEFAULT_SERVER_SETUP.listen(address, port);
public static synchronized ServerSetup address(String address) {
return DEFAULT_SERVER_SETUP.address(address);
}

}
Expand Up @@ -2,6 +2,7 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.net.Serve;
import org.rapidoid.net.TCPServer;

Expand Down Expand Up @@ -31,12 +32,12 @@ public class ServerSetup {

private final FastHttp fastHttp = new FastHttp();

public TCPServer listen(int port) {
return listen("*", port);
}
private int port = Conf.port();

private String address = "0.0.0.0";

public TCPServer listen(String address, int port) {
TCPServer server = Serve.server().protocol(fastHttp).port(port).build();
public TCPServer listen() {
TCPServer server = Serve.server().protocol(fastHttp).address(address).port(port).build();
server.start();
return server;
}
Expand Down Expand Up @@ -69,4 +70,14 @@ private FastHttp[] httpImpls() {
return new FastHttp[] { fastHttp };
}

public ServerSetup port(int port) {
this.port = port;
return this;
}

public ServerSetup address(String address) {
this.address = address;
return this;
}

}
Expand Up @@ -32,6 +32,8 @@ public interface TCPServerBuilder extends Builder<TCPServer> {

TCPServerBuilder bufSize(int bufSize);

TCPServerBuilder address(String address);

TCPServerBuilder port(int port);

TCPServerBuilder workers(int workers);
Expand Down
Expand Up @@ -40,7 +40,7 @@ public class DynamicClientTest extends TestCommons {

@Test
public void testDynamic() {
On.get("/test-abc").html("abc-ok");
On.address("127.0.0.1").port(8989).get("/test-abc").html("abc-ok");

On.get("/nums").json("[1, 2, 3]");

Expand All @@ -58,8 +58,6 @@ public Object handle(Map<String, Object> params) throws Exception {
}
});

On.listen(8080);

eq(client.abc(), "abc-ok");
eq(client.numbers(), U.list(1, 2, 3));
eq(client.sizeOf("abcde"), 5);
Expand Down
@@ -1,19 +1,19 @@
abc:
GET: http://localhost:8080/test-abc
GET: http://localhost:8989/test-abc

numbers:
GET: http://localhost:8080/nums
GET: http://localhost:8989/nums

sizeOf:
GET: http://localhost:8080/size?s=%s
GET: http://localhost:8989/size?s=%s

asyncSizeOf:
GET: http://localhost:8080/size?s=%s
GET: http://localhost:8989/size?s=%s

theBean:
POST: http://localhost:8080/echo?aa=%s&bb=%s&cc=%s
POST: http://localhost:8989/echo?aa=%s&bb=%s&cc=%s

asyncBean:
POST: http://localhost:8080/echo?aa=%s&bb=%s&cc=%s
POST: http://localhost:8989/echo?aa=%s&bb=%s&cc=%s
data:
aa: not-implemented

0 comments on commit 1cdbd98

Please sign in to comment.