Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitebb9b6a

Browse files
committed
Refactor configs some
1 parentd3a8dc7 commitebb9b6a

File tree

5 files changed

+124
-55
lines changed

5 files changed

+124
-55
lines changed

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

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importjava.io.File;
44
importjava.util.Map;
5+
importjava.util.concurrent.atomic.AtomicReference;
56
importjava.util.function.BiFunction;
67
importjava.util.function.Supplier;
78

@@ -24,25 +25,42 @@ private Configs() { }
2425
* We could abstract out and delegate but its not worth it.
2526
* I am gambling on the fact that I will not switch out the config library.
2627
*/
27-
privatestaticfinalConfigsystem =ConfigFactory.systemProperties();
28-
privatestaticfinalConfigproperties =newBuilder().withSecureConf().envAwareApp().build();
2928

30-
publicstaticConfigsystem() {
31-
returnsystem;
29+
// This config has all of the JVM system properties including any custom -D properties
30+
privatestaticfinalConfigsystemProperties =ConfigFactory.systemProperties();
31+
32+
// This config has access to all of the environment variables
33+
privatestaticfinalConfigsystemEnvironment =ConfigFactory.systemEnvironment();
34+
35+
// Always start with a blank config and add fallbacks
36+
privatestaticfinalAtomicReference<Config>propertiesRef =newAtomicReference<>(null);
37+
38+
publicstaticvoidinitProperties(Configconfig) {
39+
booleansuccess =propertiesRef.compareAndSet(null,config);
40+
if (!success) {
41+
thrownewRuntimeException("propertiesRef Config has already been initialized. This should only be called once.");
42+
}
3243
}
3344

3445
publicstaticConfigproperties() {
35-
returnproperties;
46+
returnpropertiesRef.get();
3647
}
3748

38-
// This should return the current executing user path
39-
publicstaticStringgetExecutionDirectory() {
40-
returnsystem.getString("user.dir");
49+
publicstaticConfigsystemProperties() {
50+
returnsystemProperties;
4151
}
4252

43-
publicstaticMap<String,Object>asMap(Configconfig) {
44-
returnSeq.seq(config.entrySet())
45-
.toMap(e ->e.getKey(),e ->e.getValue().unwrapped());
53+
publicstaticConfigsystemEnvironment() {
54+
returnsystemEnvironment;
55+
}
56+
57+
publicstaticConfigs.BuildernewBuilder() {
58+
returnnewBuilder();
59+
}
60+
61+
// This should return the current executing user path
62+
publicstaticStringgetExecutionDirectory() {
63+
returnsystemProperties.getString("user.dir");
4664
}
4765

4866
publicstatic <T>TgetOrDefault(Configconfig,Stringpath,BiFunction<Config,String,T>extractor,TdefaultValue) {
@@ -59,6 +77,11 @@ public static <T> T getOrDefault(Config config, String path, BiFunction<Config,
5977
returndefaultSupplier.get();
6078
}
6179

80+
publicstaticMap<String,Object>asMap(Configconfig) {
81+
returnSeq.seq(config.entrySet())
82+
.toMap(e ->e.getKey(),e ->e.getValue().unwrapped());
83+
}
84+
6285
publicstaticclassBuilder {
6386
privateConfigconf;
6487

@@ -72,6 +95,18 @@ public Builder withResource(String resource) {
7295
returnthis;
7396
}
7497

98+
publicBuilderwithSystemProperties() {
99+
conf =returnOrFallback(systemProperties);
100+
log.info("Loaded system properties into config");
101+
returnthis;
102+
}
103+
104+
publicBuilderwithSystemEnvironment() {
105+
conf =returnOrFallback(systemEnvironment);
106+
log.info("Loaded system environment into config");
107+
returnthis;
108+
}
109+
75110
publicBuilderwithOptionalFile(Stringpath) {
76111
FilesecureConfFile =newFile(path);
77112
if (secureConfFile.exists()) {
@@ -83,14 +118,13 @@ public Builder withOptionalFile(String path) {
83118
returnthis;
84119
}
85120

86-
publicBuilderenvAwareApp() {
87-
Stringenv =system.hasPath("env") ?system.getString("env") :"local";
88-
StringenvFile ="application." +env +".conf";
89-
returnwithResource(envFile).withResource("application.conf");
121+
publicBuilderwithOptionalRelativeFile(Stringpath) {
122+
returnwithOptionalFile(getExecutionDirectory() +path);
90123
}
91124

92-
publicBuilderwithSecureConf() {
93-
returnwithOptionalFile(getExecutionDirectory() +"/secure.conf");
125+
publicBuilderwithConfig(Configconfig) {
126+
conf =returnOrFallback(config);
127+
returnthis;
94128
}
95129

96130
publicConfigbuild() {
@@ -112,7 +146,9 @@ private Config returnOrFallback(Config config) {
112146
}
113147

114148
publicstaticvoidmain(String[]args) {
115-
Configs.properties();
149+
log.debug(ConfigFactory.load().root().render(ConfigRenderOptions.concise()));
150+
151+
//newBuilder().withSystemEnvironment().withSystemProperties().build();
116152
}
117153
}
118154
// {{end:config}}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
publicenumEnv {
77
LOCAL("local")
8-
,DEV("dev")
9-
,PROD("prod")
8+
,DEV("development")
9+
,STAGING("staging")
10+
,PROD("production")
1011
;
1112

1213
privatefinalStringname;
@@ -23,8 +24,9 @@ public String getName() {
2324
privatestaticfinalEnvcurrentEnv;
2425
static {
2526
Stringenv ="local";
26-
if (Configs.system().hasPath("env")) {
27-
env =Configs.system().getString("env");
27+
// This comes from -Denv={environment}
28+
if (Configs.systemProperties().hasPath("env")) {
29+
env =Configs.systemProperties().getString("env");
2830
}
2931
currentEnv =Env.valueOf(env.toUpperCase());
3032
log.info("Current Env: {}",currentEnv.getName());

‎stubbornjava-common/src/test/java/com/stubbornjava/common/ConfigsTest.java‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
importstaticorg.junit.Assert.assertEquals;
44

5-
importjava.util.concurrent.TimeUnit;
6-
75
importorg.junit.Test;
86

97
importcom.typesafe.config.Config;
@@ -15,19 +13,16 @@ public void emptyConfigShouldFail() {
1513
newConfigs.Builder().build();
1614
}
1715

16+
@Test
17+
publicvoidemptyConfigShouldNotFail() {
18+
newConfigs.Builder().build();
19+
}
20+
21+
@Test
1822
publicvoidconfigShouldLoadResource() {
1923
Configconf =newConfigs.Builder()
2024
.withResource("other.conf")
2125
.build();
2226
assertEquals("other",conf.getString("name"));
2327
}
24-
25-
publicvoidconfigShouldLoadAppConfig() {
26-
Configconf =newConfigs.Builder()
27-
.envAwareApp()
28-
.build();
29-
assertEquals("StubbornJava Common",conf.getString("app"));
30-
// 2 minutes is the override local config.
31-
assertEquals(2,conf.getDuration("someTimeout",TimeUnit.MINUTES));
32-
}
3328
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packagecom.stubbornjava.webapp;
2+
3+
importorg.slf4j.Logger;
4+
importorg.slf4j.LoggerFactory;
5+
6+
importcom.stubbornjava.common.Configs;
7+
importcom.stubbornjava.common.Env;
8+
importcom.stubbornjava.common.Json;
9+
importcom.typesafe.config.Config;
10+
11+
publicclassStubbornJavaBootstrap {
12+
13+
privatestaticfinalLoggerlogger =LoggerFactory.getLogger(StubbornJavaBootstrap.class);
14+
15+
publicstaticConfiggetConfig() {
16+
Configconfig =Configs.newBuilder()
17+
.withOptionalRelativeFile("./secure.conf")
18+
.withResource("application." +Env.get().getName() +".conf")
19+
.withResource("application.conf")
20+
.build();
21+
logger.debug(Json.serializer().toPrettyString(Configs.asMap(config)));
22+
returnconfig;
23+
}
24+
25+
publicstatic <T>voidrun(Runnablerunnable) {
26+
try {
27+
Configs.initProperties(getConfig());
28+
runnable.run();
29+
}catch (Throwableex) {
30+
logger.error("",ex);
31+
}finally {
32+
// Close pools and stuff
33+
}
34+
}
35+
}

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaWebApp.java‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,43 +62,44 @@ private static HttpHandler wrapWithMiddleware(HttpHandler next) {
6262
// {{end:middleware}}
6363

6464
// These routes do not require any authentication
65-
privatestaticfinalHttpHandlerbasicRoutes =newRoutingHandler()
66-
.get("/",timed("getHome",PageRoutes::home))
67-
.get("/ping",timed("ping",PageRoutes::ping))
65+
privatestaticfinalHttpHandlergetBasicRoutes() {
66+
returnnewRoutingHandler()
67+
.get("/",timed("getHome",PageRoutes::home))
68+
.get("/ping",timed("ping",PageRoutes::ping))
6869

69-
.get("/favicon.ico",timed("favicon",CustomHandlers.resource("images/", (int)TimeUnit.DAYS.toSeconds(30))))
70-
.get("robots.txt",timed("robots",PageRoutes::robots))
70+
.get("/favicon.ico",timed("favicon",CustomHandlers.resource("images/", (int)TimeUnit.DAYS.toSeconds(30))))
71+
.get("robots.txt",timed("robots",PageRoutes::robots))
7172

72-
.get("/posts/{slug}",timed("getPost",PostRoutes::getPost))
73+
.get("/posts/{slug}",timed("getPost",PostRoutes::getPost))
7374

74-
.get("/tags/{tag}/posts",timed("getRecentPostsWithTag",PostRoutes::recentPostsWithTag))
75+
.get("/tags/{tag}/posts",timed("getRecentPostsWithTag",PostRoutes::recentPostsWithTag))
7576

76-
.get("/java-libraries",timed("getJavaLibraries",JavaLibRoutes::listLibraries))
77-
.get("/java-libraries/{library}",timed("getLibrary",JavaLibRoutes::getLibrary))
78-
.get("/libraries/{library}/posts",timed("getLibraryRedirect",JavaLibRoutes::getLibraryRedirect))
77+
.get("/java-libraries",timed("getJavaLibraries",JavaLibRoutes::listLibraries))
78+
.get("/java-libraries/{library}",timed("getLibrary",JavaLibRoutes::getLibrary))
79+
.get("/libraries/{library}/posts",timed("getLibraryRedirect",JavaLibRoutes::getLibraryRedirect))
7980

80-
.get("/guides/{slug}",timed("getGuide",GuideRoutes::getGuide))
81+
.get("/guides/{slug}",timed("getGuide",GuideRoutes::getGuide))
8182

82-
.get("/best-selling-html-css-themes-and-website-templates",timed("popularThemes",ThemeRoutes::popularThemes))
83+
.get("/best-selling-html-css-themes-and-website-templates",timed("popularThemes",ThemeRoutes::popularThemes))
8384

84-
.get("/dev/metrics",timed("getMetrics",HelperRoutes::getMetrics))
85+
.get("/dev/metrics",timed("getMetrics",HelperRoutes::getMetrics))
8586

86-
// addAll allows you to combine more than one RoutingHandler together.
87-
.addAll(SitemapRoutes.router(StubbornJavaSitemapGenerator.getSitemap()))
87+
// addAll allows you to combine more than one RoutingHandler together.
88+
.addAll(SitemapRoutes.router(StubbornJavaSitemapGenerator.getSitemap()))
8889

89-
.setFallbackHandler(timed("notFound",PageRoutes::notFound))
90-
;
91-
92-
privatestaticfinalHttpHandlerstaticRoutes =newPathHandler(basicRoutes)
93-
.addPrefixPath("/assets",timed("getAssets",CustomHandlers.resource("", (int)TimeUnit.DAYS.toSeconds(30))))
94-
;
90+
.setFallbackHandler(timed("notFound",PageRoutes::notFound));
91+
}
9592

9693
privatestaticvoidstartServer() {
94+
HttpHandlerstaticRoutes =newPathHandler(getBasicRoutes())
95+
.addPrefixPath("/assets",timed("getAssets",CustomHandlers.resource("", (int)TimeUnit.DAYS.toSeconds(30))));
9796
SimpleServerserver =SimpleServer.simpleServer(wrapWithMiddleware(staticRoutes));
9897
server.start();
9998
}
10099

101100
publicstaticvoidmain(String[]args) {
102-
startServer();
101+
StubbornJavaBootstrap.run(() -> {
102+
startServer();
103+
});
103104
}
104105
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp