Skip to content

Commit

Permalink
assets handler: automatically configure on startup fix #512
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Oct 24, 2016
1 parent b5bc9cc commit d27c83b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Issue356 extends ServerFeature {
.etag(true).lastModified(false).maxAge(Duration.ofDays(2)));

assets("/assets/empty.css", new AssetHandler("/")
.etag(true).lastModified(true).maxAge(Duration.ofDays(7)));
.etag(true).lastModified(true).maxAge("7d"));
}

@Test
Expand Down
8 changes: 1 addition & 7 deletions jooby-assets/src/main/java/org/jooby/assets/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
package org.jooby.assets;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.jooby.Env;
import org.jooby.Jooby;
import org.jooby.Router;
Expand Down Expand Up @@ -333,10 +330,7 @@ public void configure(final Env env, final Config config, final Binder binder) {
.cdn(conf.getString("assets.cdn"))
.lastModified(conf.getBoolean("assets.lastModified"));

if (conf.hasPath("assets.cache.maxAge")) {
handler.maxAge(Duration
.ofSeconds(conf.getDuration("assets.cache.maxAge", TimeUnit.SECONDS)));
}
handler.maxAge(conf.getString("assets.cache.maxAge"));

compiler.patterns().forEach(pattern -> routes.get(pattern, handler));

Expand Down
9 changes: 6 additions & 3 deletions jooby-assets/src/test/java/org/jooby/assets/AssetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class AssetsTest {
public void configure() throws Exception {
Config conf = ConfigFactory.empty()
.withValue("application.path", ConfigValueFactory.fromAnyRef("/path"))
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false));
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false))
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef(-1));
new MockUnit(Env.class, Binder.class, Request.class, Response.class,
Route.Chain.class).expect(unit -> {
AssetCompiler compiler = unit.constructor(AssetCompiler.class)
Expand Down Expand Up @@ -95,7 +96,8 @@ public void configure() throws Exception {
public void configureWithWatch() throws Exception {
Config conf = ConfigFactory.empty()
.withValue("application.path", ConfigValueFactory.fromAnyRef("/"))
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(true));
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(true))
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef(-1));
new MockUnit(Env.class, Binder.class, Request.class, Response.class,
Route.Chain.class).expect(unit -> {
AssetCompiler compiler = unit.constructor(AssetCompiler.class)
Expand Down Expand Up @@ -161,7 +163,8 @@ public void configureWithWatch() throws Exception {
public void configureWithoutWatch() throws Exception {
Config conf = ConfigFactory.empty()
.withValue("application.path", ConfigValueFactory.fromAnyRef("/"))
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false));
.withValue("assets.watch", ConfigValueFactory.fromAnyRef(false))
.withValue("assets.cache.maxAge", ConfigValueFactory.fromAnyRef(-1));
new MockUnit(Env.class, Binder.class, Request.class, Response.class,
Route.Chain.class).expect(unit -> {
AssetCompiler compiler = unit.constructor(AssetCompiler.class)
Expand Down
3 changes: 2 additions & 1 deletion jooby/src/main/java/org/jooby/Jooby.java
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,8 @@ public Route.Definition assets(final String path, final String location) {
handler
.cdn(conf.getString("assets.cdn"))
.lastModified(conf.getBoolean("assets.lastModified"))
.etag(conf.getBoolean("assets.etag"));
.etag(conf.getBoolean("assets.etag"))
.maxAge(conf.getString("assets.cache.maxAge"));
});
return assets(path, handler);
}
Expand Down
35 changes: 27 additions & 8 deletions jooby/src/main/java/org/jooby/handlers/AssetHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.time.Duration;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

import org.jooby.Asset;
import org.jooby.Jooby;
Expand All @@ -40,9 +39,12 @@
import org.jooby.internal.URLAsset;

import com.google.common.base.Strings;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

import javaslang.Function1;
import javaslang.Function2;
import javaslang.control.Try;

/**
* Serve static resources, via {@link Jooby#assets(String)} or variants.
Expand Down Expand Up @@ -94,7 +96,7 @@ public class AssetHandler implements Route.Handler {

private boolean etag = true;

private Optional<Duration> maxAge = Optional.empty();
private long maxAge = -1;

private boolean lastModified = true;

Expand Down Expand Up @@ -191,16 +193,33 @@ public AssetHandler cdn(final String cdn) {
* @return This handler.
*/
public AssetHandler maxAge(final Duration maxAge) {
this.maxAge = Optional.of(maxAge);
return this;
return maxAge(maxAge.getSeconds());
}

/**
* @param maxAge Set the cache header max-age value in seconds.
* @return This handler.
*/
public AssetHandler maxAge(final long maxAge) {
return maxAge(Duration.ofSeconds(maxAge));
this.maxAge = maxAge;
return this;
}

/**
* Parse value as {@link Duration}. If the value is already a number then it uses as seconds.
* Otherwise, it parse expressions like: 8m, 1h, 365d, etc...
*
* @param maxAge Set the cache header max-age value in seconds.
* @return This handler.
*/
public AssetHandler maxAge(final String maxAge) {
Try.of(() -> Long.parseLong(maxAge))
.recover(x -> ConfigFactory.empty()
.withValue("v", ConfigValueFactory.fromAnyRef(maxAge))
.getDuration("v")
.getSeconds())
.onSuccess(this::maxAge);
return this;
}

@Override
Expand Down Expand Up @@ -263,9 +282,9 @@ private void doHandle(final Request req, final Response rsp, final Asset asset)
}

// cache max-age
maxAge.ifPresent(d -> {
rsp.header("Cache-Control", "max-age=" + d.getSeconds());
});
if (maxAge > 0) {
rsp.header("Cache-Control", "max-age=" + maxAge);
}

send(req, rsp, asset);
}
Expand Down
1 change: 1 addition & 0 deletions jooby/src/test/java/org/jooby/JoobyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,7 @@ public void assets() throws Exception {
expect(conf.getString("assets.cdn")).andReturn("").times(2);
expect(conf.getBoolean("assets.lastModified")).andReturn(true).times(2);
expect(conf.getBoolean("assets.etag")).andReturn(true).times(2);
expect(conf.getString("assets.cache.maxAge")).andReturn("-1").times(2);

Injector injector = unit.get(Injector.class);
expect(injector.getInstance(Key.get(Config.class))).andReturn(conf).times(2);
Expand Down

0 comments on commit d27c83b

Please sign in to comment.