Skip to content

Commit

Permalink
Several minor enhancements.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 2, 2015
1 parent 2e2fcf3 commit e2e5222
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 78 deletions.
3 changes: 1 addition & 2 deletions rapidoid-app/src/main/java/org/rapidoid/app/Apps.java
Expand Up @@ -34,7 +34,6 @@
import org.rapidoid.config.Conf;
import org.rapidoid.http.HTTP;
import org.rapidoid.http.HTTPServer;
import org.rapidoid.http.HttpBuiltins;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.log.Log;
import org.rapidoid.oauth.OAuth;
Expand Down Expand Up @@ -93,7 +92,7 @@ public static HTTPServer serve(Object... args) {
HTTPServer server = HTTP.server().build();

OAuth.register(server);
HttpBuiltins.register(server);
// HttpBuiltins.register(server); // TODO make it configurable

server.serve(new AppHandler());

Expand Down
13 changes: 11 additions & 2 deletions rapidoid-config/src/main/java/org/rapidoid/config/Conf.java
Expand Up @@ -163,13 +163,22 @@ public static boolean stateless() {
return is("stateless");
}

public static String oauth() {
return option("oauth", null);
}

public static boolean production() {
return has("mode", "production");
}

public static boolean dev() {
assert configureDevMode();
return has("mode", "dev");
if (production()) {
return false;
}

assert configureDevMode(); // in debug mode

return has("mode", "dev") || !production();
}

private static boolean configureDevMode() {
Expand Down
61 changes: 26 additions & 35 deletions rapidoid-http/src/main/java/org/rapidoid/http/HttpBuiltins.java
Expand Up @@ -32,40 +32,31 @@
public class HttpBuiltins {

public static void register(HTTPServer server) {

server.get("/_logout", new Handler() {
@Override
public Object handle(HttpExchange x) {
Ctx.delUser();
x.cookiepack().remove("username");
x.cookiepack().remove("name");
throw x.goBack(0);
}
});

server.get("/_debugLogin", new Handler() {
@Override
public Object handle(HttpExchange x) {
x.accessDeniedIf(!Conf.dev());

String username = x.param("user");
U.must(username.matches("\\w+"));

username += "@debug";

UserInfo user = new UserInfo();
user.username = username;
user.email = username;
user.name = U.capitalized(username);

Ctx.delUser();
x.cookiepack().put("username", user.username);
x.cookiepack().put("name", user.name);
Ctx.setUser(user);

throw x.goBack(0);
}
});

if (Conf.dev()) {
server.get("/_debugLogin", new Handler() {
@Override
public Object handle(HttpExchange x) {
x.accessDeniedIf(!Conf.dev());

String username = x.param("user");
U.must(username.matches("\\w+"));

username += "@debug";

UserInfo user = new UserInfo();
user.username = username;
user.email = username;
user.name = U.capitalized(username);

Ctx.delUser();
x.cookiepack().put("username", user.username);
x.cookiepack().put("name", user.name);
Ctx.setUser(user);

throw x.goBack(0);
}
});
}
}

}
Expand Up @@ -329,7 +329,8 @@ public synchronized HttpExchangeImpl send() {
public synchronized String toString() {
return "HttpExchange [uri=" + uri() + ", verb=" + verb() + ", path=" + path() + ", subpath=" + subpath()
+ ", query=" + query() + ", protocol=" + protocol() + ", body=" + body() + ", headers=" + headers()
+ ", params=" + params() + ", cookies=" + cookies() + ", data=" + posted() + ", files=" + files() + "]";
+ ", params=" + params() + ", cookies=" + cookies() + ", data=" + posted() + ", files="
+ files().keySet() + "]";
}

@Override
Expand Down
36 changes: 1 addition & 35 deletions rapidoid-main/src/main/java/org/rapidoid/main/Main.java
Expand Up @@ -28,14 +28,13 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.scan.Scan;
import org.rapidoid.util.U;

@Authors("Nikolche Mihajlovski")
@Since("3.1.0")
public class Main {

public static void main(String[] args) {
processHelp(args);
MainHelp.processHelp(args);

List<Class<?>> app = Scan.annotated(App.class);
if (!app.isEmpty()) {
Expand All @@ -49,37 +48,4 @@ public static void main(String[] args) {
}
}

public static void processHelp(Object[] args) {
if (args.length == 1 && args[0].equals("--help")) {
show("Usage:");
show(" java [-cp <classpath>] -jar <app>.jar [option1 option2 ...] ");

show("\nExample:");
show(" java -jar myapp.jar port=9090 address=127.0.0.1 cpus=2 workers=4 stateless");

show("\nAvailable options:");
opt("mode=(dev|production)", "configure DEV or PRODUCTION mode");
opt("secret=<SECRET>", "configure app-specific secret token for encryption");
opt("port=<P>", "listen at port P (default: 8080)");
opt("address=<ADDR>", "listen at address ADDR (default: 0.0.0.0)");
opt("stateless", "Run in stateless mode, session becomes cookiepack (default: false)");
opt("threads=<T>", "start T threads for the job executor service (default: 100)");
opt("cpus=<C>", "optimize for C number of CPUs (default: the actual number of the CPUs)");
opt("workers=<W>", "start W number of I/O workers (default: the configured number of CPUs)");
opt("nodelay", "set the TCP_NODELAY flag to disable Nagle's algorithm (default: false)");
opt("blockingAccept", "accept connection in BLOCKING mode (default: false)");
opt("bufSizeKB=<SIZE>", "TCP socket buffer size in KB (default: 16)");

System.exit(0);
}
}

private static void opt(String opt, String desc) {
show(" " + opt + U.copyNtimes(" ", 17 - opt.length()) + " - " + desc);
}

private static void show(String msg) {
System.out.println(msg);
}

}
64 changes: 64 additions & 0 deletions rapidoid-main/src/main/java/org/rapidoid/main/MainHelp.java
@@ -0,0 +1,64 @@
package org.rapidoid.main;

/*
* #%L
* rapidoid-main
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.util.U;

@Authors("Nikolche Mihajlovski")
@Since("4.0.0")
public class MainHelp {

public static void processHelp(Object[] args) {
if (args.length == 1 && args[0].equals("--help")) {
show("Usage:");
show(" java -cp <yourapp>.jar com.yourapp.Main [option1 option2 ...]");

show("\nExample:");
show(" java -cp <yourapp>.jar com.yourapp.Main port=9090 address=127.0.0.1 cpus=2 workers=4 stateless");

show("\nAvailable options:");
opt("mode=(dev|production)", "configure DEV or PRODUCTION mode");
opt("secret=<SECRET>", "configure app-specific secret token for encryption");
opt("port=<P>", "listen at port P (default: 8080)");
opt("address=<ADDR>", "listen at address ADDR (default: 0.0.0.0)");
opt("stateless", "Run in stateless mode, session becomes cookiepack (default: false)");
opt("threads=<T>", "start T threads for the job executor service (default: 100)");
opt("cpus=<C>", "optimize for C number of CPUs (default: the actual number of the CPUs)");
opt("workers=<W>", "start W number of I/O workers (default: the configured number of CPUs)");
opt("nodelay", "set the TCP_NODELAY flag to disable Nagle's algorithm (default: false)");
opt("blockingAccept", "accept connection in BLOCKING mode (default: false)");
opt("bufSizeKB=<SIZE>", "TCP socket buffer size in KB (default: 16)");

System.exit(0);
}
}

private static void opt(String opt, String desc) {
show(" " + opt + U.copyNtimes(" ", 17 - opt.length()) + " - " + desc);
}

private static void show(String msg) {
System.out.println(msg);
}

}
Expand Up @@ -35,7 +35,7 @@ public static synchronized void run(String[] args) {
U.must(!initialized, "Already initialized!");
initialized = true;

Main.processHelp(args);
MainHelp.processHelp(args);
Quick.run(args);
}

Expand Down
4 changes: 4 additions & 0 deletions rapidoid-oauth/src/main/java/org/rapidoid/oauth/OAuth.java
Expand Up @@ -46,6 +46,10 @@ public static void register(HTTPServer server, OAuthProvider... providers) {
public static void register(HTTPServer server, String oauthDomain, OAuthStateCheck stateCheck,
OAuthProvider... providers) {

if (Conf.oauth() == null) {
return;
}

oauthDomain = U.or(oauthDomain, Conf.option("oauth-domain", (String) null));

OAuth.STATE_CHECK = stateCheck;
Expand Down
Expand Up @@ -3,6 +3,6 @@ hibernate.hbm2ddl.auto=create
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
hibernate.connection.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
hibernate.format_sql=false
hibernate.show_sql=true
2 changes: 1 addition & 1 deletion rapidoid-rest/src/main/java/org/rapidoid/rest/REST.java
Expand Up @@ -47,7 +47,7 @@ public static void run() {
run(services.toArray(new Class[services.size()]));
}

public static void main(String[] args) {
public static void run(String[] args) {
Conf.args(args);
run();
}
Expand Down
20 changes: 20 additions & 0 deletions rapidoid-utils/src/main/java/org/rapidoid/util/WrapperJob.java
@@ -1,5 +1,25 @@
package org.rapidoid.util;

/*
* #%L
* rapidoid-utils
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.rapidoid.ctx.Ctx;

public class WrapperJob implements Runnable {
Expand Down

0 comments on commit e2e5222

Please sign in to comment.