Skip to content

Commit

Permalink
More configurable app folder layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Aug 12, 2015
1 parent 57610d5 commit aaf2c93
Show file tree
Hide file tree
Showing 23 changed files with 48 additions and 29 deletions.
8 changes: 4 additions & 4 deletions rapidoid-app/src/main/java/org/rapidoid/app/AppHandler.java
Expand Up @@ -27,6 +27,7 @@


import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.dispatch.DispatchResult; import org.rapidoid.dispatch.DispatchResult;
import org.rapidoid.dispatch.PojoDispatchException; import org.rapidoid.dispatch.PojoDispatchException;
import org.rapidoid.dispatch.PojoDispatcher; import org.rapidoid.dispatch.PojoDispatcher;
Expand Down Expand Up @@ -208,12 +209,11 @@ private DispatchResult doDispatch(PojoDispatcher dispatcher, PojoRequest req) {
} }


public boolean serveDynamicPage(HttpExchangeImpl x, Object result, boolean hasEvent, Map<String, Object> config) { public boolean serveDynamicPage(HttpExchangeImpl x, Object result, boolean hasEvent, Map<String, Object> config) {
String filename = "dynamic/" + x.resourceName() + ".html"; Res resource = Res.from(Conf.templatesPath() + "/" + x.resourceName() + ".html");
Res resource = Res.from(filename);


Map<String, Object> model; Map<String, Object> model;
if (resource.exists()) { if (resource.exists()) {
model = pageModel(filename, result, resource); model = pageModel(result, resource);
} else if (result != null) { } else if (result != null) {
model = U.map("result", result, "content", result, "navbar", true); model = U.map("result", result, "content", result, "navbar", true);
} else { } else {
Expand Down Expand Up @@ -250,7 +250,7 @@ private void serveEventResponse(HttpExchangeImpl x, String html) {
} }
} }


private static Map<String, Object> pageModel(String filename, Object result, Res resource) { private static Map<String, Object> pageModel(Object result, Res resource) {
String template = U.safe(resource.getContent()); String template = U.safe(resource.getContent());


Map<String, Object> model = U.map("result", result); Map<String, Object> model = U.map("result", result);
Expand Down
23 changes: 20 additions & 3 deletions rapidoid-config/src/main/java/org/rapidoid/config/Conf.java
Expand Up @@ -168,13 +168,30 @@ public static void set(String key, Object value) {
ROOT.put(key, value); ROOT.put(key, value);
} }


public static String path() { public static String rootPath() {
String path = option("path", "rapidoid"); return cleanPath(option("root", "rapidoid"));
}

public static String configPath() {
return cleanPath(option("config", rootPath()));
}

public static String staticPath() {
return cleanPath(option("static", rootPath() + "/static"));
}


public static String dynamicPath() {
return cleanPath(option("dynamic", rootPath() + "/dynamic"));
}

public static String templatesPath() {
return cleanPath(option("templates", rootPath() + "/templates"));
}

private static String cleanPath(String path) {
if (path.endsWith("/") || path.endsWith("\\")) { if (path.endsWith("/") || path.endsWith("\\")) {
path = path.substring(0, path.length() - 1); path = path.substring(0, path.length() - 1);
} }

return path; return path;
} }


Expand Down
Expand Up @@ -937,19 +937,19 @@ public synchronized HttpExchange authorize(Class<?> clazz) {


@Override @Override
public synchronized boolean serveStaticFile() { public synchronized boolean serveStaticFile() {
if (serveStaticFile("static/" + resourceName())) { if (serveStaticFile(resourceName())) {
return true; return true;
} }


return !resourceNameHasExtension() && serveStaticFile("static/" + resourceName() + ".html"); return !resourceNameHasExtension() && serveStaticFile(resourceName() + ".html");
} }


@Override @Override
public synchronized boolean serveStaticFile(String filename) { public synchronized boolean serveStaticFile(String filename) {
Res resource = Res.from(filename); Res res = Res.from(Conf.staticPath() + "/" + filename);


if (resource.exists()) { if (res.exists()) {
sendFile(resource); sendFile(res);
return true; return true;
} else { } else {
return false; return false;
Expand Down
2 changes: 1 addition & 1 deletion rapidoid-io/src/main/java/org/rapidoid/io/IO.java
Expand Up @@ -59,7 +59,7 @@ public static ClassLoader classLoader() {
} }


public static String name(String resourceName) { public static String name(String resourceName) {
int urlPos = resourceName.indexOf("://"); int urlPos = resourceName.indexOf(":/");
if (urlPos > 0) { if (urlPos > 0) {
return resourceName; return resourceName;
} else { } else {
Expand Down
26 changes: 14 additions & 12 deletions rapidoid-io/src/main/java/org/rapidoid/io/Res.java
Expand Up @@ -95,8 +95,10 @@ protected void loadResource() {
byte[] res = load(name); byte[] res = load(name);


if (res == null) { if (res == null) {
String defaultFilename = IO.getDefaultFilename(name);
Log.debug("Trying to load the default resource", "name", defaultFilename);
// if the resource doesn't exist, try loading the default resource // if the resource doesn't exist, try loading the default resource
res = load(IO.getDefaultFilename(name)); res = load(defaultFilename);
} }


this.bytes = res; this.bytes = res;
Expand All @@ -117,28 +119,28 @@ protected void loadResource() {
} }


protected byte[] load(String filename) { protected byte[] load(String filename) {
File file = IO.file(name); File file = IO.file(filename);


if (file.exists()) { if (file.exists()) {


// a normal file on the file system // a normal file on the file system
Log.debug("File exists", "name", name); Log.debug("File exists", "file", file);


if (file.lastModified() > this.lastModified || !name.equals(cachedFileName)) { if (file.lastModified() > this.lastModified || !filename.equals(cachedFileName)) {
Log.debug("Reloading file", "name", name); Log.debug("Reloading file", "file", file);
this.lastModified = file.lastModified(); this.lastModified = file.lastModified();
this.cachedFileName = name; this.cachedFileName = filename;
return IO.loadBytes(name); return IO.loadBytes(filename);
} else { } else {
Log.debug("File not modified", "name", name); Log.debug("File not modified", "file", file);
return bytes; return bytes;
} }
} else { } else {

// it might not exist or it might be on the classpath or compressed in a JAR // it might not exist or it might be on the classpath or compressed in a JAR
Log.debug("Reloading classpath resource", "name", name); Log.debug("Trying to load classpath resource", "file", file);
this.cachedFileName = null; byte[] res = IO.loadBytes(filename);
return IO.loadBytes(name); this.cachedFileName = (res != null) ? filename : null;
return res;
} }
} }


Expand Down
4 changes: 2 additions & 2 deletions rapidoid-main/src/main/java/org/rapidoid/main/Rapidoid.java
Expand Up @@ -60,7 +60,7 @@ public static synchronized void run(WebApp app, String[] args, Object... config)
app = WebAppGroup.root(); app = WebAppGroup.root();
app.getRouter().generic(new AppHandler()); app.getRouter().generic(new AppHandler());


final Res menuRes = Res.from("menu.yaml").trackChanges(); final Res menuRes = Res.from(Conf.configPath() + "/menu.yaml").trackChanges();


final WebApp rootApp = app; final WebApp rootApp = app;
menuRes.getChangeListeners().add(new Runnable() { menuRes.getChangeListeners().add(new Runnable() {
Expand All @@ -74,7 +74,7 @@ public void run() {
} }
}); });


final Res confRes = Res.from("app.yaml").trackChanges(); final Res confRes = Res.from(Conf.configPath() + "/app.yaml").trackChanges();


confRes.getChangeListeners().add(new Runnable() { confRes.getChangeListeners().add(new Runnable() {
@Override @Override
Expand Down
Expand Up @@ -26,6 +26,7 @@


import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.io.IO; import org.rapidoid.io.IO;
import org.rapidoid.io.Res; import org.rapidoid.io.Res;
import org.rapidoid.log.Log; import org.rapidoid.log.Log;
Expand Down Expand Up @@ -113,8 +114,7 @@ public void invalidateCache() {


@Override @Override
public Reader getReader(String resourceName) { public Reader getReader(String resourceName) {
Res res = Res.from(resourceName); return Res.from(Conf.templatesPath() + "/" + resourceName).getReader();
return res.getReader();
} }


} }

0 comments on commit aaf2c93

Please sign in to comment.