Skip to content

Commit

Permalink
Stricter but customizable configuration file names.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Aug 18, 2016
1 parent 77426d1 commit 140f70f
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 115 deletions.
23 changes: 10 additions & 13 deletions rapidoid-commons/src/main/java/org/rapidoid/config/Conf.java
Expand Up @@ -130,6 +130,7 @@ public static synchronized boolean micro() {

public static synchronized void reset() {
ROOT.clear();
ROOT.filenameBase("config");
args = new String[0];
}

Expand All @@ -138,9 +139,7 @@ public static synchronized Config section(String name) {
}

private static Config createSection(String name) {
Config config = ROOT.sub(name);
ConfigUtil.load(filename(config.keys()), config, false);
return config;
return ROOT.sub(name);
}

public static synchronized Config section(Class<?> clazz) {
Expand All @@ -161,22 +160,20 @@ public static synchronized void reload() {

ROOT.clear();

ConfigUtil.load(Msc.path("default", "config.y?ml"), ROOT, true);
String filenameBase = U.or(ROOT.filenameBase(), "config");
String configFilenamePattern = filenameBase + ".y?ml";
String configProfilePattern = filenameBase + "-%s.y?ml";

ConfigUtil.load(Msc.path("default", configFilenamePattern), ROOT, true);

for (String profile : Env.profiles()) {
ConfigUtil.load(Msc.path("default", U.frmt("profile-%s.y?ml", profile)), ROOT, true);
ConfigUtil.load(Msc.path("default", U.frmt(configProfilePattern, profile)), ROOT, true);
}

ConfigUtil.load(Msc.path(path, "application.y?ml"), ROOT, false);
ConfigUtil.load(Msc.path(path, "config.y?ml"), ROOT, false);
ConfigUtil.load(Msc.path(path, configFilenamePattern), ROOT, false);

for (String profile : Env.profiles()) {
ConfigUtil.load(Msc.path(path, U.frmt("application-%s.y?ml", profile)), ROOT, false);
ConfigUtil.load(Msc.path(path, U.frmt("profile-%s.y?ml", profile)), ROOT, false);
}

for (Config sub : SECTIONS.values()) {
ConfigUtil.load(filename(sub.keys()), sub, false);
ConfigUtil.load(Msc.path(path, U.frmt(configProfilePattern, profile)), ROOT, false);
}

for (List<String> keys : detached) {
Expand Down
35 changes: 30 additions & 5 deletions rapidoid-commons/src/main/java/org/rapidoid/config/Config.java
Expand Up @@ -7,6 +7,7 @@
import org.rapidoid.commons.Arr;
import org.rapidoid.commons.Coll;
import org.rapidoid.lambda.ToMap;
import org.rapidoid.log.Log;
import org.rapidoid.u.U;
import org.rapidoid.value.Value;
import org.rapidoid.value.Values;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class Config extends RapidoidThing implements ToMap<String, Object> {

private final boolean isRoot;

private volatile String filenameBase = "config";

private Config(Map<String, Object> properties, List<String> baseKeys, Config root) {
this.properties = properties;
this.root = root;
Expand Down Expand Up @@ -274,12 +277,21 @@ public String toString() {
public void args(String... args) {
if (U.notEmpty(args)) {
for (String arg : args) {
String[] parts = arg.split("=", 2);
if (!arg.contains("->")) {
String[] parts = arg.split("=", 2);
String name = parts[0];

if (parts.length > 1) {
setNested(parts[0], parts[1]);
} else {
setNested(parts[0], true);
if (parts.length > 1) {
String value = parts[1];

if (name.equals("config")) {
filenameBase(value);
}

setNested(name, value);
} else {
setNested(name, true);
}
}
}
}
Expand Down Expand Up @@ -338,4 +350,17 @@ public Properties toProperties() {
public ConfigAlternatives or(Config alternative) {
return new ConfigAlternatives(this, alternative);
}

public String filenameBase() {
return filenameBase;
}

public synchronized Config filenameBase(String filenameBase) {
if (U.neq(this.filenameBase, filenameBase)) {
Log.info("Changing configuration filename base", "!from", this.filenameBase, "!to", filenameBase);
}

this.filenameBase = filenameBase;
return this;
}
}
Expand Up @@ -36,6 +36,7 @@ public class ConfigOptions extends RapidoidThing {
private static List<ConfigOption> configOptions() {
List<ConfigOption> opts = U.list();

opts.add(opt("config", "configuration filename prefix", "config"));
opts.add(opt("dev", "configure DEV mode", "auto-detected"));
opts.add(opt("production", "configure PRODUCTION mode", "auto-detected"));
opts.add(opt("secret=<SECRET>", "configure app-specific secret for encryption", "(random)"));
Expand Down
21 changes: 21 additions & 0 deletions rapidoid-commons/src/test/resources/DataPermissionsTest/config.yml
@@ -0,0 +1,21 @@
users:
adm1:
roles: administrator

adm2:
roles: administrator

mng1:
roles: manager

mod1:
roles: moderator

mod2:
roles: moderator

abc:
roles: abc

other:
roles: other_role
20 changes: 0 additions & 20 deletions rapidoid-commons/src/test/resources/DataPermissionsTest/users.yml

This file was deleted.

24 changes: 24 additions & 0 deletions rapidoid-commons/src/test/resources/MethodSecurityTest/config.yaml
@@ -0,0 +1,24 @@
users:
adm1:
roles: ["administrator"]

adm2:
roles: ["administrator"]

mng1:
roles: ["manager"]

mod1:
roles: ["moderator"]

mod2:
roles: ["moderator"]

abc1:
roles: abc

abc2:
roles: abc

xyz1:
roles: xyz
23 changes: 0 additions & 23 deletions rapidoid-commons/src/test/resources/MethodSecurityTest/users.yaml

This file was deleted.

@@ -0,0 +1,18 @@
users:
adm1:
roles: ["administrator"]

adm2:
roles: ["administrator"]

mng1:
roles: ["manager"]

mod1:
roles: ["moderator"]

mod2:
roles: ["moderator"]

abc:
roles: abc

This file was deleted.

16 changes: 16 additions & 0 deletions rapidoid-commons/src/test/resources/RolesTest/config.yaml
@@ -0,0 +1,16 @@
users:
niko:
email: niko@rapidoid.org.abcde
password: easy
roles:
- owner
- administrator
- moderator

chuck:
password: chuck
roles: ["moderator", "restarter"]

abc:
password: abc
roles: guest , foo , bar
15 changes: 0 additions & 15 deletions rapidoid-commons/src/test/resources/RolesTest/users.yaml

This file was deleted.

This file was deleted.

@@ -0,0 +1,20 @@
MyClient:
abc:
GET: http://localhost:8888/test-abc

numbers:
GET: http://localhost:8888/nums

sizeOf:
GET: http://localhost:8888/size?s=%s

asyncSizeOf:
GET: http://localhost:8888/size?s=%s

theBean:
POST: http://localhost:8888/echo?aa=%s&bb=%s&cc=%s

asyncBean:
POST: http://localhost:8888/echo?aa=%s&bb=%s&cc=%s
data:
aa: not-implemented
Expand Up @@ -123,7 +123,7 @@ private void openSocket() throws IOException {

ServerSocket socket = serverSocketChannel.socket();

Log.debug("Opening port to listen", "port", port);
Log.info("!Starting server", "!address", address, "!port", port, "I/O workers", workers, "accept", blockingInfo);

InetSocketAddress addr = new InetSocketAddress(address, port);

Expand All @@ -138,8 +138,6 @@ private void openSocket() throws IOException {
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
}

Log.info("!Server started", "!address", address, "!port", port, "I/O workers", workers, "accept", blockingInfo);

initWorkers();

} else {
Expand Down

0 comments on commit 140f70f

Please sign in to comment.