Skip to content

Commit

Permalink
✨ update blade cli param parse logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jul 27, 2018
1 parent 849bbc6 commit c9a33c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
28 changes: 16 additions & 12 deletions src/main/java/com/blade/Blade.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public Class<?> bootClass() {
*/
public Blade enableCors(boolean enableCors) {
this.environment.set(ENV_KEY_CORS_ENABLE, enableCors);
if(enableCors){
if (enableCors) {
this.use(new CorsMiddleware());
}
return this;
Expand Down Expand Up @@ -672,7 +672,7 @@ public Blade use(@NonNull WebHook... middleware) {
return this;
}
this.middleware.addAll(Arrays.asList(middleware));
for (var webHook: middleware) {
for (var webHook : middleware) {
this.register(webHook);
}
return this;
Expand Down Expand Up @@ -809,12 +809,12 @@ public Blade disableSession() {
return this;
}

public Blade disableCost(){
public Blade disableCost() {
this.environment.set(ENV_KEY_HTTP_REQUEST_COST, false);
return this;
}

public boolean allowCost(){
public boolean allowCost() {
return this.environment.getBoolean(ENV_KEY_HTTP_REQUEST_COST, true);
}

Expand Down Expand Up @@ -1047,14 +1047,17 @@ private void loadConfig(String[] args) {
}

if (!Objects.requireNonNull(bootEnv).isEmpty()) {
Map<String, String> bootEnvMap = bootEnv.toMap();
Set<Map.Entry<String, String>> entrySet = bootEnvMap.entrySet();
Map<String, String> bootEnvMap = bootEnv.toMap();
Set<Map.Entry<String, String>> entrySet = bootEnvMap.entrySet();
entrySet.forEach(entry -> environment.set(entry.getKey(), entry.getValue()));
}

String envName = "default";
if (StringKit.isNotEmpty(System.getProperty(ENV_KEY_APP_ENV))) {
envName = System.getProperty(ENV_KEY_APP_ENV);

Map<String, String> argsMap = BladeKit.parseArgs(args);

if (StringKit.isNotEmpty(argsMap.get(ENV_KEY_APP_ENV))) {
envName = argsMap.get(ENV_KEY_APP_ENV);
String evnFileName = "application-" + envName + ".properties";
Environment customEnv = Environment.of(evnFileName);
if (customEnv != null && !customEnv.isEmpty()) {
Expand All @@ -1078,11 +1081,12 @@ private void loadConfig(String[] args) {
return;
}

if (StringKit.isNotEmpty(System.getProperty(ENV_KEY_SERVER_ADDRESS))) {
this.environment.set(ENV_KEY_SERVER_ADDRESS, System.getProperty(ENV_KEY_SERVER_ADDRESS));
if (StringKit.isNotEmpty(argsMap.get(ENV_KEY_SERVER_ADDRESS))) {
this.environment.set(ENV_KEY_SERVER_ADDRESS, argsMap.get(ENV_KEY_SERVER_ADDRESS));
}
if (StringKit.isNotEmpty(System.getProperty(ENV_KEY_SERVER_PORT))) {
this.environment.set(ENV_KEY_SERVER_PORT, System.getProperty(ENV_KEY_SERVER_PORT));

if (StringKit.isNotEmpty(argsMap.get(ENV_KEY_SERVER_PORT))) {
this.environment.set(ENV_KEY_SERVER_PORT, argsMap.get(ENV_KEY_SERVER_PORT));
}

}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/blade/kit/BladeKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -339,4 +336,18 @@ public static void logWebSocket(Logger log, String path) {
log.info("{}Add WebSocket {}", getStartedSymbol(), path);
}

// --app.env=dev --server.port=9001
public static Map<String, String> parseArgs(String[] args) {
Map<String, String> argsMap = new HashMap<>();
if (null == args || args.length == 0) {
return argsMap;
}
for (String arg : args) {
if (arg.startsWith("--") && arg.contains("=")) {
String[] param = arg.substring(2).split("=");
argsMap.put(param[0], param[1]);
}
}
return argsMap;
}
}
2 changes: 1 addition & 1 deletion src/test/java/netty_hello/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void main(String[] args) {
// .showFileList(true)
// .gzip(true)
// .enableCors(true)
.start();
.start(Hello.class, args);
}

}

0 comments on commit c9a33c7

Please sign in to comment.