Skip to content

Commit 6b46afd

Browse files
committed
updating common resources
1 parent dbd73be commit 6b46afd

4 files changed

Lines changed: 46 additions & 12 deletions

File tree

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ allprojects {
2121

2222
// Using Jitpack so I need the repo name in the group to match.
2323
group = 'com.stubbornjava.StubbornJava'
24-
version = '0.1.6-SNAPSHOT'
24+
version = '0.1.7-SNAPSHOT'
2525

2626
repositories {
2727
mavenLocal()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.stubbornjava.common;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.google.common.base.Preconditions;
7+
8+
public class AssetsConfig {
9+
private static final Logger log = LoggerFactory.getLogger(AssetsConfig.class);
10+
11+
public static String assetsRoot() {
12+
if (!Configs.properties().hasPath("assets.roots")) {
13+
log.error("Configuration is missing required \"assets.roots\" config for dev mode. Did you forget to add it to application.local.conf?");
14+
Preconditions.checkNotNull(null, "Missing required \"assets.roots\" config.");
15+
}
16+
return Configs.properties().getString("assets.roots");
17+
}
18+
}

‎stubbornjava-common/src/main/java/com/stubbornjava/common/Templating.java‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
1515
import com.github.jknack.handlebars.io.FileTemplateLoader;
1616
import com.github.jknack.handlebars.io.TemplateLoader;
17-
import com.google.common.base.Preconditions;
1817
import com.google.common.collect.Lists;
1918
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
2019
// {{start:templating}}
@@ -32,14 +31,8 @@ public class Templating {
3231
builder.withCaching()
3332
.withResourceLoaders();
3433
} else {
35-
if (!Configs.properties().hasPath("assets.roots")) {
36-
log.error("Configuration is missing required \"assets.roots\" config for dev mode. Did you forget to add it to application.local.conf?");
37-
Preconditions.checkNotNull(null, "Missing required \"assets.roots\" config.");
38-
}
39-
List<String> roots = Configs.properties().getStringList("assets.roots");
40-
for (String root : roots) {
41-
builder.withLocalResourceLoaders(root);
42-
}
34+
String root = AssetsConfig.assetsRoot();
35+
builder.withLocalResourceLoaders(root);
4336
}
4437
DEFAULT = builder.build();
4538
}

‎stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/handlers/CustomHandlers.java‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.stubbornjava.common.undertow.handlers;
22

33

4+
import java.io.File;
5+
import java.util.concurrent.TimeUnit;
6+
47
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
69

10+
import com.stubbornjava.common.AssetsConfig;
11+
import com.stubbornjava.common.Env;
712
import com.stubbornjava.common.Metrics;
813
import com.stubbornjava.common.undertow.Exchange;
914
import com.stubbornjava.undertow.handlers.accesslog.Slf4jAccessLogReceiver;
@@ -17,9 +22,13 @@
1722
import io.undertow.server.handlers.encoding.ContentEncodingRepository;
1823
import io.undertow.server.handlers.encoding.EncodingHandler;
1924
import io.undertow.server.handlers.encoding.GzipEncodingProvider;
25+
import io.undertow.server.handlers.resource.ClassPathResourceManager;
26+
import io.undertow.server.handlers.resource.FileResourceManager;
27+
import io.undertow.server.handlers.resource.ResourceHandler;
28+
import io.undertow.server.handlers.resource.ResourceManager;
2029

2130
public class CustomHandlers {
22-
private static final Logger logger = LoggerFactory.getLogger(CustomHandlers.class);
31+
private static final Logger log = LoggerFactory.getLogger(CustomHandlers.class);
2332

2433
public static AccessLogHandler accessLog(HttpHandler next, Logger logger) {
2534
return new AccessLogHandler(next, new Slf4jAccessLogReceiver(logger), "combined", CustomHandlers.class.getClassLoader());
@@ -40,6 +49,20 @@ public static HttpHandler gzip(HttpHandler next) {
4049
.setNext(next);
4150
}
4251

52+
public static HttpHandler resource(String prefix) {
53+
ResourceManager resourceManager = null;
54+
if (Env.LOCAL == Env.get()) {
55+
log.debug("using local file resource manager {}", AssetsConfig.assetsRoot());
56+
resourceManager = new FileResourceManager(new File(AssetsConfig.assetsRoot()), 1024 * 1024);
57+
} else {
58+
log.debug("using classpath file resource manager");
59+
resourceManager = new ClassPathResourceManager(CustomHandlers.class.getClassLoader(), prefix);
60+
}
61+
ResourceHandler handler = new ResourceHandler(resourceManager);
62+
handler.setCacheTime((int)TimeUnit.HOURS.toSeconds(4));
63+
return handler;
64+
}
65+
4366
public static StatusCodeHandler statusCodeMetrics(HttpHandler next) {
4467
return new StatusCodeHandler(next, "status.code");
4568
}
@@ -57,7 +80,7 @@ public static ExceptionHandler exception(HttpHandler handler) {
5780
try {
5881
handler.handleRequest(exchange);
5982
} catch (Throwable th) {
60-
logger.error("exception thrown at " + exchange.getRequestURI(), th);
83+
log.error("exception thrown at " + exchange.getRequestURI(), th);
6184
throw th;
6285
}
6386
});

0 commit comments

Comments
 (0)