1- package com .pcdd .sonovel .util ;
1+ package com .pcdd .sonovel .core ;
22
33import cn .hutool .core .io .FileUtil ;
44import cn .hutool .core .util .StrUtil ;
55import cn .hutool .setting .Setting ;
66import cn .hutool .setting .dialect .Props ;
77import com .pcdd .sonovel .model .AppConfig ;
8+ import com .pcdd .sonovel .util .EnvUtils ;
9+ import com .pcdd .sonovel .util .LangUtil ;
810import lombok .experimental .UtilityClass ;
911
1012import java .io .File ;
1719 * Created at 2024/3/23
1820 */
1921@ UtilityClass
20- public class ConfigUtils {
22+ public class AppConfigLoader {
2123
2224 private final String SELECTION_GLOBAL = "global" ;
2325 private final String SELECTION_DOWNLOAD = "download" ;
@@ -26,6 +28,7 @@ public class ConfigUtils {
2628 private final String SELECTION_WEB = "web" ;
2729 private final String SELECTION_COOKIE = "cookie" ;
2830 private final String SELECTION_PROXY = "proxy" ;
31+ public final AppConfig APP_CONFIG = loadConfig ();
2932
3033 /**
3134 * 加载应用属性
@@ -54,49 +57,49 @@ public Setting usr() {
5457 return new Setting (absolutePath .toString ());
5558 }
5659
57- public AppConfig defaultConfig () {
58- AppConfig config = new AppConfig ();
59- config .setVersion (sys ().getStr ("version" ));
60+ public AppConfig loadConfig () {
61+ AppConfig cfg = new AppConfig ();
62+ cfg .setVersion (sys ().getStr ("version" ));
6063 Setting usr = usr ();
6164
6265 // [global]
63- config .setAutoUpdate (usr .getInt ("auto-update" , SELECTION_GLOBAL , 0 ));
64- config .setGhProxy (usr .getStr ("gh-proxy" , SELECTION_GLOBAL , "" ));
66+ cfg .setAutoUpdate (usr .getInt ("auto-update" , SELECTION_GLOBAL , 0 ));
67+ cfg .setGhProxy (usr .getStr ("gh-proxy" , SELECTION_GLOBAL , "" ));
6568
6669 // [download]
67- config .setDownloadPath (getStrOrDefault (usr , "download-path" , SELECTION_DOWNLOAD , "downloads" ));
68- config .setExtName (getStrOrDefault (usr , "extname" , SELECTION_DOWNLOAD , "epub" ).toLowerCase ());
69- config .setTxtEncoding (getStrOrDefault (usr , "txt-encoding" , SELECTION_DOWNLOAD , "UTF-8" ));
70- config .setPreserveChapterCache (usr .getInt ("preserve-chapter-cache" , SELECTION_DOWNLOAD , 0 ));
70+ cfg .setDownloadPath (getStrOrDefault (usr , "download-path" , SELECTION_DOWNLOAD , "downloads" ));
71+ cfg .setExtName (getStrOrDefault (usr , "extname" , SELECTION_DOWNLOAD , "epub" ).toLowerCase ());
72+ cfg .setTxtEncoding (getStrOrDefault (usr , "txt-encoding" , SELECTION_DOWNLOAD , "UTF-8" ));
73+ cfg .setPreserveChapterCache (usr .getInt ("preserve-chapter-cache" , SELECTION_DOWNLOAD , 0 ));
7174
7275 // [source]
73- config .setLanguage (getStrOrDefault (usr , "language" , SELECTION_SOURCE , LangUtil .getCurrentLang ()));
74- config .setActiveRules (getStrOrDefault (usr , "active-rules" , SELECTION_SOURCE , "main-rules.json" ));
75- config .setSourceId (usr .getInt ("source-id" , SELECTION_SOURCE , -1 ));
76- config .setSearchLimit (usr .getInt ("search-limit" , SELECTION_SOURCE , -1 ));
76+ cfg .setLanguage (getStrOrDefault (usr , "language" , SELECTION_SOURCE , LangUtil .getCurrentLang ()));
77+ cfg .setActiveRules (getStrOrDefault (usr , "active-rules" , SELECTION_SOURCE , "main-rules.json" ));
78+ cfg .setSourceId (usr .getInt ("source-id" , SELECTION_SOURCE , -1 ));
79+ cfg .setSearchLimit (usr .getInt ("search-limit" , SELECTION_SOURCE , -1 ));
7780
7881 // [crawl]
79- config .setThreads (usr .getInt ("threads" , SELECTION_CRAWL , -1 ));
80- config .setMinInterval (usr .getInt ("min-interval" , SELECTION_CRAWL , 200 ));
81- config .setMaxInterval (usr .getInt ("max-interval" , SELECTION_CRAWL , 400 ));
82- config .setEnableRetry (usr .getInt ("enable-retry" , SELECTION_CRAWL , 1 ));
83- config .setMaxRetries (usr .getInt ("max-retries" , SELECTION_CRAWL , 5 ));
84- config .setRetryMinInterval (usr .getInt ("retry-min-interval" , SELECTION_CRAWL , 2000 ));
85- config .setRetryMaxInterval (usr .getInt ("retry-max-interval" , SELECTION_CRAWL , 4000 ));
82+ cfg .setThreads (usr .getInt ("threads" , SELECTION_CRAWL , -1 ));
83+ cfg .setMinInterval (usr .getInt ("min-interval" , SELECTION_CRAWL , 200 ));
84+ cfg .setMaxInterval (usr .getInt ("max-interval" , SELECTION_CRAWL , 400 ));
85+ cfg .setEnableRetry (usr .getInt ("enable-retry" , SELECTION_CRAWL , 1 ));
86+ cfg .setMaxRetries (usr .getInt ("max-retries" , SELECTION_CRAWL , 5 ));
87+ cfg .setRetryMinInterval (usr .getInt ("retry-min-interval" , SELECTION_CRAWL , 2000 ));
88+ cfg .setRetryMaxInterval (usr .getInt ("retry-max-interval" , SELECTION_CRAWL , 4000 ));
8689
8790 // [web]
88- config .setWebEnabled (usr .getInt ("enabled" , SELECTION_WEB , 0 ));
89- config .setWebPort (usr .getInt ("port" , SELECTION_WEB , 7765 ));
91+ cfg .setWebEnabled (usr .getInt ("enabled" , SELECTION_WEB , 0 ));
92+ cfg .setWebPort (usr .getInt ("port" , SELECTION_WEB , 7765 ));
9093
9194 // [cookie]
92- config .setQidianCookie (usr .getStr ("qidian" , SELECTION_COOKIE , "" ));
95+ cfg .setQidianCookie (usr .getStr ("qidian" , SELECTION_COOKIE , "" ));
9396
9497 // [proxy]
95- config .setProxyEnabled (usr .getInt ("enabled" , SELECTION_PROXY , 0 ));
96- config .setProxyHost (getStrOrDefault (usr , "host" , SELECTION_PROXY , "127.0.0.1" ));
97- config .setProxyPort (usr .getInt ("port" , SELECTION_PROXY , 7890 ));
98+ cfg .setProxyEnabled (usr .getInt ("enabled" , SELECTION_PROXY , 0 ));
99+ cfg .setProxyHost (getStrOrDefault (usr , "host" , SELECTION_PROXY , "127.0.0.1" ));
100+ cfg .setProxyPort (usr .getInt ("port" , SELECTION_PROXY , 7890 ));
98101
99- return config ;
102+ return cfg ;
100103 }
101104
102105 // 修复 hutool 空串不能触发默认值的 bug
0 commit comments