Skip to content

Commit

Permalink
#31 Predefined server configurations for "admin" and "dev" server.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 10, 2016
1 parent b64e3d0 commit d9cc008
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 90 deletions.
11 changes: 10 additions & 1 deletion rapidoid-commons/src/main/java/org/rapidoid/config/Conf.java
Expand Up @@ -100,7 +100,7 @@ private static void processArgIfSpecial(String name, Object value) {
} }
} }


public static void unconfigure(String name) { public static void remove(String name) {
ROOT.remove(name); ROOT.remove(name);
} }


Expand Down Expand Up @@ -140,6 +140,14 @@ public static int port() {
return option("port", 8888); return option("port", 8888);
} }


public static int adminPort() {
return option("admin.port", 8889);
}

public static int devPort() {
return option("dev.port", 8887);
}

public static int cpus() { public static int cpus() {
return option("cpus", Runtime.getRuntime().availableProcessors()); return option("cpus", Runtime.getRuntime().availableProcessors());
} }
Expand Down Expand Up @@ -331,4 +339,5 @@ public void run() {


return conf; return conf;
} }

} }
22 changes: 15 additions & 7 deletions rapidoid-commons/src/main/java/org/rapidoid/config/Config.java
Expand Up @@ -44,12 +44,20 @@ public Config() {
} }


public String option(String name) { public String option(String name) {
Object opt = properties.get(name); Object opt = getValue(name);
return opt != null ? U.str(opt) : null; return opt != null ? U.str(opt) : null;
} }


private Object getValue(String name) {
if (name.contains(".")) {
return nested(name.split("\\."));
} else {
return properties.get(name);
}
}

public String option(String name, String defaultValue) { public String option(String name, String defaultValue) {
Object obj = properties.get(name); Object obj = getValue(name);
return obj != null ? U.str(obj) : defaultValue; return obj != null ? U.str(obj) : defaultValue;
} }


Expand All @@ -69,7 +77,7 @@ public double option(String name, double defaultValue) {
} }


public boolean has(String name, Object value) { public boolean has(String name, Object value) {
Object val = properties.get(name); Object val = getValue(name);
return U.eq(val, value); return U.eq(val, value);
} }


Expand All @@ -82,7 +90,7 @@ public boolean is(String name) {
} }


public boolean contains(String name, Object value) { public boolean contains(String name, Object value) {
Object opt = properties.get(name); Object opt = getValue(name);


if (opt != null) { if (opt != null) {
if (opt instanceof Collection) { if (opt instanceof Collection) {
Expand All @@ -97,7 +105,7 @@ public boolean contains(String name, Object value) {


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public synchronized Config sub(String name) { public synchronized Config sub(String name) {
Map<String, Object> submap = (Map<String, Object>) properties.get(name); Map<String, Object> submap = (Map<String, Object>) getValue(name);


if (submap == null) { if (submap == null) {
submap = U.map(); submap = U.map();
Expand Down Expand Up @@ -157,12 +165,12 @@ public String toString() {


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key) { public <T> T get(String key) {
return (T) properties.get(key); return (T) getValue(key);
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getOrFail(String key, Class<T> clazz) { public <T> T getOrFail(String key, Class<T> clazz) {
T val = (T) properties.get(key); T val = (T) getValue(key);
U.must(val != null, "Cannot find the configuration entry: %s", key); U.must(val != null, "Cannot find the configuration entry: %s", key);
U.must(Cls.instanceOf(val, clazz), "The configuration entry '%s' must be of type: %s", key, U.must(Cls.instanceOf(val, clazz), "The configuration entry '%s' must be of type: %s", key,
clazz.getSimpleName()); clazz.getSimpleName());
Expand Down
88 changes: 41 additions & 47 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/On.java
Expand Up @@ -37,31 +37,17 @@
@Since("4.3.0") @Since("4.3.0")
public class On { public class On {


private static final ServerSetup DEFAULT_SERVER_SETUP = new ServerSetup(); private static final ServerSetup DEFAULT_SERVER = new ServerSetup("default", "0.0.0.0", 8888);


private static boolean initialized = false; private static final ServerSetup ADMIN_SERVER = new ServerSetup("admin", "0.0.0.0", 8889);


private static ServerSetup setup() { private static final ServerSetup DEV_SERVER = new ServerSetup("dev", "127.0.0.1", 8887);
if (!initialized) {
initialize();
initialized = true;
}


return DEFAULT_SERVER_SETUP; static {
} Log.info("Initializing Rapidoid...", "version", RapidoidInfo.version());

private static void initialize() {
Log.info("Starting Rapidoid...", "version", RapidoidInfo.version());

// print internal state
// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();


Log.info("Working directory is: " + System.getProperty("user.dir")); Log.info("Working directory is: " + System.getProperty("user.dir"));


DEFAULT_SERVER_SETUP.listen();

//Ctx ctx = Ctxs.open("web");

Jobs.execute(new Runnable() { Jobs.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
Expand All @@ -75,114 +61,122 @@ public void run() {
} }


public static synchronized OnAction get(String path) { public static synchronized OnAction get(String path) {
return setup().get(path); return DEFAULT_SERVER.get(path);
} }


public static synchronized OnAction post(String path) { public static synchronized OnAction post(String path) {
return setup().post(path); return DEFAULT_SERVER.post(path);
} }


public static synchronized OnAction put(String path) { public static synchronized OnAction put(String path) {
return setup().put(path); return DEFAULT_SERVER.put(path);
} }


public static synchronized OnAction delete(String path) { public static synchronized OnAction delete(String path) {
return setup().delete(path); return DEFAULT_SERVER.delete(path);
} }


public static synchronized OnAction patch(String path) { public static synchronized OnAction patch(String path) {
return setup().patch(path); return DEFAULT_SERVER.patch(path);
} }


public static synchronized OnAction options(String path) { public static synchronized OnAction options(String path) {
return setup().options(path); return DEFAULT_SERVER.options(path);
} }


public static synchronized OnAction head(String path) { public static synchronized OnAction head(String path) {
return setup().head(path); return DEFAULT_SERVER.head(path);
} }


public static synchronized OnAction trace(String path) { public static synchronized OnAction trace(String path) {
return setup().trace(path); return DEFAULT_SERVER.trace(path);
} }


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


public static synchronized ServerSetup error(ErrorHandler onError) { public static synchronized ServerSetup error(ErrorHandler onError) {
return setup().onError(onError); return DEFAULT_SERVER.onError(onError);
} }


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


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


public static synchronized ServerSetup req(FastHttpHandler handler) { public static synchronized ServerSetup req(FastHttpHandler handler) {
return setup().req(handler); return DEFAULT_SERVER.req(handler);
} }


public static synchronized ServerSetup req(Object... controllers) { public static synchronized ServerSetup req(Object... controllers) {
return setup().req(controllers); return DEFAULT_SERVER.req(controllers);
} }


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


public static synchronized ServerSetup address(String address) { public static synchronized ServerSetup address(String address) {
return DEFAULT_SERVER_SETUP.address(address); return DEFAULT_SERVER.address(address);
} }


public static ServerSetup path(String... path) { public static ServerSetup path(String... path) {
return DEFAULT_SERVER_SETUP.path(path); return DEFAULT_SERVER.path(path);
} }


public static String[] path() { public static String[] path() {
return DEFAULT_SERVER_SETUP.path(); return DEFAULT_SERVER.path();
} }


public static synchronized ServerSetup defaultWrap(HttpWrapper... wrappers) { public static synchronized ServerSetup defaultWrap(HttpWrapper... wrappers) {
return DEFAULT_SERVER_SETUP.defaultWrap(wrappers); return DEFAULT_SERVER.defaultWrap(wrappers);
} }


public static synchronized ServerSetup listener(FastHttpListener listener) { public static synchronized ServerSetup listener(FastHttpListener listener) {
return DEFAULT_SERVER_SETUP.listener(listener); return DEFAULT_SERVER.listener(listener);
} }


public static synchronized ServerSetup getDefaultSetup() { public static synchronized ServerSetup getDefaultSetup() {
return setup(); return DEFAULT_SERVER;
} }


public static ServerSetup createCustomSetup() { public static ServerSetup createServer(String name) {
return new ServerSetup(); return new ServerSetup(name, "0.0.0.0", 8888);
} }


public static synchronized ServerSetup staticFilesLookIn(String... possibleLocations) { public static synchronized ServerSetup staticFilesLookIn(String... possibleLocations) {
return setup().staticFilesPath(possibleLocations); return DEFAULT_SERVER.staticFilesPath(possibleLocations);
} }


public static synchronized ServerSetup render(ViewRenderer renderer) { public static synchronized ServerSetup render(ViewRenderer renderer) {
return setup().render(renderer); return DEFAULT_SERVER.render(renderer);
} }


public static ServerSetup args(String... args) { public static ServerSetup args(String... args) {
Conf.args(args); Conf.args(args);
return DEFAULT_SERVER_SETUP; return DEFAULT_SERVER;
} }


public static ServerSetup bootstrap() { public static ServerSetup bootstrap() {
return DEFAULT_SERVER_SETUP.bootstrap(); return DEFAULT_SERVER.bootstrap();
} }


@SafeVarargs @SafeVarargs
@SuppressWarnings({"varargs"}) @SuppressWarnings({"varargs"})
public static OnAnnotated annotated(Class<? extends Annotation>... annotated) { public static OnAnnotated annotated(Class<? extends Annotation>... annotated) {
return DEFAULT_SERVER_SETUP.annotated(annotated); return DEFAULT_SERVER.annotated(annotated);
}

public static ServerSetup admin() {
return ADMIN_SERVER;
}

public static ServerSetup dev() {
return DEV_SERVER;
} }


} }

0 comments on commit d9cc008

Please sign in to comment.