Skip to content

Commit cac5b04

Browse files
committed
improve env callbacks and make them to follow the application order + auto-reload conf when modules are imported inside an env callback
1 parent 46e3f8f commit cac5b04

File tree

5 files changed

+173
-53
lines changed

5 files changed

+173
-53
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
import com.google.inject.Binder;
7+
import com.google.inject.Key;
8+
import com.google.inject.name.Names;
9+
import com.typesafe.config.Config;
10+
import com.typesafe.config.ConfigFactory;
11+
import com.typesafe.config.ConfigValueFactory;
12+
13+
public class EnvCallbackReloadConfFeature extends ServerFeature {
14+
15+
{
16+
Key<String> key = Key.get(String.class, Names.named(("envvar")));
17+
on("dev", () -> {
18+
use(new Jooby.Module() {
19+
20+
@Override
21+
public void configure(final Env env, final Config conf, final Binder binder) {
22+
binder.bind(key).toInstance(conf.getString("envvar"));
23+
}
24+
25+
@Override
26+
public Config config() {
27+
return ConfigFactory.empty().withValue("envvar", ConfigValueFactory.fromAnyRef("foo"));
28+
}
29+
});
30+
});
31+
32+
get("/", req -> req.require(key));
33+
}
34+
35+
@Test
36+
public void devCallback() throws Exception {
37+
request().get("/").expect("foo");
38+
}
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class EnvCallbackRouteOrderFeature extends ServerFeature {
7+
8+
{
9+
StringBuilder buff = new StringBuilder();
10+
on("dev", () -> {
11+
get("/", (req, rsp) -> {
12+
buff.append("pre");
13+
});
14+
});
15+
16+
get("/", (req, rsp) -> {
17+
buff.append("-");
18+
});
19+
20+
on("dev", () -> {
21+
get("/", (req, rsp) -> {
22+
rsp.send(buff.append("post"));
23+
});
24+
});
25+
}
26+
27+
@Test
28+
public void devCallback() throws Exception {
29+
request().get("/").expect("pre-post");
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class EnvEmpyCallbackFeature extends ServerFeature {
7+
8+
{
9+
on("dev", () -> {
10+
});
11+
12+
get("/", () -> "empty");
13+
14+
on("dev", () -> {
15+
});
16+
}
17+
18+
@Test
19+
public void devCallback() throws Exception {
20+
request().get("/").expect("empty");
21+
}
22+
}

0 commit comments

Comments
 (0)