Skip to content

Commit

Permalink
Redesigned the server setup components.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed May 1, 2018
1 parent e209313 commit 35b2c24
Show file tree
Hide file tree
Showing 28 changed files with 751 additions and 632 deletions.
2 changes: 1 addition & 1 deletion docs/examples/httpcustom.adoc
Expand Up @@ -31,7 +31,7 @@ public class Main {
App.scan();
Boot.jmx(App.setup());
Boot.adminCenter(App.adminSetup());
Boot.adminCenter(App.setup());
// continue with normal setup
On.get("/x").json("x");
Expand Down
10 changes: 10 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/util/LazyInit.java
Expand Up @@ -27,6 +27,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

@Authors("Nikolche Mihajlovski")
@Since("5.2.4")
Expand Down Expand Up @@ -118,4 +119,13 @@ public T getValue() {
public boolean isInitialized() {
return initializedValue != null;
}

public synchronized void ifPresent(Consumer<? super T> action) {
T value = initializedValue;

if (value != null) {
action.accept(value);
}
}

}
1 change: 0 additions & 1 deletion rapidoid-commons/src/main/java/org/rapidoid/util/Msc.java
Expand Up @@ -841,7 +841,6 @@ public static void reset() {
Log.reset();
Crypto.reset();
Res.reset();
AppInfo.reset();
Conf.reset();
Groups.reset();
Jobs.reset();
Expand Down
4 changes: 2 additions & 2 deletions rapidoid-commons/src/main/resources/built-in-config.yml
Expand Up @@ -3,8 +3,8 @@ on:
address: 0.0.0.0

admin:
port: ${on.port}
address: ${on.address}
port: 9090
address: 0.0.0.0

app:
contextPath: ''
Expand Down
2 changes: 1 addition & 1 deletion rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -728,6 +728,7 @@ org.rapidoid.setup.ReloadUtil
org.rapidoid.setup.ServerSetup
org.rapidoid.setup.ServiceBootstrap
org.rapidoid.setup.Setup
org.rapidoid.setup.SetupImpl
org.rapidoid.setup.Setups
org.rapidoid.setup.WatchForChanges
org.rapidoid.test.Doc
Expand All @@ -751,7 +752,6 @@ org.rapidoid.timeseries.TimeSeries
org.rapidoid.timeseries.TSValue
org.rapidoid.util.AbstractMapImpl
org.rapidoid.util.AnsiColor
org.rapidoid.util.AppInfo
org.rapidoid.util.BenchmarkOperation
org.rapidoid.util.Bufs
org.rapidoid.util.ByType
Expand Down
Expand Up @@ -23,8 +23,6 @@
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.u.U;
import org.rapidoid.util.AppInfo;
import org.rapidoid.util.Msc;

@Authors("Nikolche Mihajlovski")
Expand All @@ -33,38 +31,12 @@ public class ReqInfoUtils extends RapidoidThing {

public static String adminUrl() {
IReqInfo req = ReqInfo.get();
int appPort = AppInfo.appPort;
int adminPort = AppInfo.adminPort;
boolean appAndAdminOnSamePort = adminPort == appPort;

if (U.notEmpty(req.host())) {
String hostname = req.host().split(":")[0];

if (AppInfo.isAdminServerActive) {
String path = req.contextPath() + Msc.specialUriPrefix();
return appAndAdminOnSamePort ? path : "http://" + hostname + ":" + adminPort + path;
}
}

return null;
return req.contextPath() + Msc.specialUriPrefix();
}

public static String appUrl() {
IReqInfo req = ReqInfo.get();
int appPort = AppInfo.appPort;
int adminPort = AppInfo.adminPort;
boolean appAndAdminOnSamePort = adminPort == appPort;

if (U.notEmpty(req.host())) {
String hostname = req.host().split(":")[0];

if (AppInfo.isAppServerActive) {
String path = req.contextPath() + "/";
return appAndAdminOnSamePort ? path : "http://" + hostname + ":" + appPort + path;
}
}

return null;
return req.contextPath() + "/";
}

}
Expand Up @@ -87,7 +87,7 @@ public class ReqImpl extends RapidoidThing implements Req, Constants, HttpMetada

private final Map<String, List<Upload>> files;

private final Map<String, Object> attrs = Collections.synchronizedMap(new HashMap<String, Object>());
private final Map<String, Object> attrs = Collections.synchronizedMap(new HashMap<>());

private volatile Map<String, Object> data;

Expand Down
Expand Up @@ -45,7 +45,7 @@ public static void main(String[] args) {
App.scan();

Boot.jmx(App.setup());
Boot.adminCenter(App.adminSetup());
Boot.adminCenter(App.setup());

// continue with normal setup
On.get("/x").json("x");
Expand Down
Expand Up @@ -35,26 +35,31 @@ public class HttpAdminAndDevServerTest extends IsolatedIntegrationTest {
@Test
public void testAdminOnAppServer() {
sameSetup();
sameRequests(8080);
sendRequests(9090);

Admin.setup().halt();
}

@Test
public void testAdminServerConfig() {
int port = 8881;
Conf.section("admin").set("port", port);
Conf.ADMIN.set("port", port);

sameSetup();
sameRequests(port);
sendRequests(port);

Admin.setup().halt();
}

private void sameSetup() {
On.get("/a").html((Req x) -> "default " + U.join(":", x.uri(), x.zone(), x.contextPath()));
Admin.get("/b").roles().json((Req x) -> "admin " + U.join(":", x.uri(), x.zone(), x.contextPath()));

Admin.get("/b").zone("admin").roles().json((Req x) -> "admin " + U.join(":", x.uri(), x.zone(), x.contextPath()));
Admin.get("/c").json((Req x) -> "unauthorized");
Admin.get("/d").html((Req x) -> "unauthorized");
}

private void sameRequests(int port) {
private void sendRequests(int port) {
onlyGet("/a"); // app
onlyGet(port, "/b"); // admin
onlyGet(port, "/c"); // admin - unauthorized
Expand Down
Expand Up @@ -31,8 +31,10 @@ public class HttpAdminServerTest extends IsolatedIntegrationTest {

@Test
public void testAdminServer() {
Admin.port(9898);
Admin.port(9898).address("127.0.0.1");

Admin.get("/").json(() -> "ok");

onlyGet(9898, "/");
}

Expand Down
Expand Up @@ -88,6 +88,7 @@ public void openContext() {
My.reset();

App.resetGlobalState();

On.changes().ignore();

On.setup().activate();
Expand Down
Expand Up @@ -4,7 +4,7 @@
"address" : "127.0.0.1"
},
"admin" : {
"port" : "8080",
"port" : 9090,
"address" : "127.0.0.1"
},
"app" : {
Expand Down
Expand Up @@ -15,7 +15,7 @@
"address" : "127.0.0.1"
},
"admin" : {
"port" : "8080",
"port" : 9090,
"address" : "127.0.0.1"
},
"app" : {
Expand Down
Expand Up @@ -5,7 +5,7 @@
"address" : "127.0.0.1"
},
"admin" : {
"port" : "8080",
"port" : 9090,
"address" : "127.0.0.1"
},
"app" : {
Expand Down
Expand Up @@ -109,7 +109,7 @@ Content-Length: 15132
</tr>
<tr>
<td>port</td>
<td>8080</td>
<td>9090</td>
</tr>
<tr>
<td>address</td>
Expand Down
82 changes: 20 additions & 62 deletions rapidoid-rest/src/main/java/org/rapidoid/setup/Admin.java
Expand Up @@ -24,76 +24,65 @@
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.config.Config;
import org.rapidoid.http.HttpRoutes;
import org.rapidoid.http.ReqHandler;
import org.rapidoid.http.ReqRespHandler;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.handler.HttpHandler;
import org.rapidoid.http.impl.RouteOptions;
import org.rapidoid.security.Role;
import org.rapidoid.util.LazyInit;

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class Admin extends RapidoidThing {

private static final LazyInit<Setup> setup = new LazyInit<>(Admin::createAdminSetup);

private static Setup createAdminSetup() {
return Setups.create("admin");
}

public static synchronized Setup setup() {
return Setups.admin();
return setup.get();
}

public static synchronized OnRoute route(String verb, String path) {
return setup().on(verb, path);
return setup().on(verb, path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute any(String path) {
return setup().any(path);
return setup().any(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute get(String path) {
return setup().get(path);
return setup().get(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute post(String path) {
return setup().post(path);
return setup().post(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute put(String path) {
return setup().put(path);
return setup().put(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute delete(String path) {
return setup().delete(path);
return setup().delete(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute patch(String path) {
return setup().patch(path);
return setup().patch(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute options(String path) {
return setup().options(path);
return setup().options(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute head(String path) {
return setup().head(path);
return setup().head(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute trace(String path) {
return setup().trace(path);
return setup().trace(path).roles(Role.ADMINISTRATOR);
}

public static synchronized OnRoute page(String path) {
return setup().page(path);
}

public static synchronized Setup req(ReqHandler handler) {
return setup().req(handler);
}

public static synchronized Setup req(ReqRespHandler handler) {
return setup().req(handler);
}

public static synchronized Setup req(HttpHandler handler) {
return setup().req(handler);
return setup().page(path).roles(Role.ADMINISTRATOR);
}

public static synchronized ServerSetup port(int port) {
Expand All @@ -104,35 +93,4 @@ public static synchronized ServerSetup address(String address) {
return new ServerSetup(Conf.ADMIN).address(address);
}

public static synchronized OnError error(Class<? extends Throwable> error) {
return setup().error(error);
}

public static Setup deregister(String verb, String path) {
return setup().deregister(verb, path);
}

public static Setup deregister(Object... controllers) {
return setup().deregister(controllers);
}

public static Config config() {
return setup().config();
}

public static Customization custom() {
return setup().custom();
}

public static HttpRoutes routes() {
return setup().routes();
}

public static RouteOptions defaults() {
return setup().defaults();
}

public static OnChanges changes() {
return OnChanges.INSTANCE;
}
}
7 changes: 1 addition & 6 deletions rapidoid-rest/src/main/java/org/rapidoid/setup/App.java
Expand Up @@ -248,8 +248,7 @@ private static void resetAppStateBeforeRestart() {

Setups.reloadAll();

Conf.reset(); // reset the config again
Setups.initDefaults(); // this changes the config
Setups.initDefaults();
Conf.reset(); // reset the config again
}

Expand Down Expand Up @@ -361,10 +360,6 @@ public static Setup setup() {
return Setups.main();
}

public static Setup adminSetup() {
return Setups.admin();
}

public static Screen gui() {
return setup().gui();
}
Expand Down

0 comments on commit 35b2c24

Please sign in to comment.