1 module hunt.framework.provider.ConfigServiceProvider;
2 
3 import hunt.framework.application.HostEnvironment;
4 import hunt.framework.config.ApplicationConfig;
5 import hunt.framework.config.AuthUserConfig;
6 import hunt.framework.config.ConfigManager;
7 import hunt.framework.provider.ServiceProvider;
8 import hunt.framework.Init;
9 import hunt.framework.routing;
10 
11 import hunt.logging;
12 import poodinis;
13 
14 import std.array;
15 import std.file;
16 import std.path;
17 import std.parallelism : totalCPUs;
18 import std.process;
19 import std.range;
20 
21 
22 /**
23  * 
24  */
25 class ConfigServiceProvider : ServiceProvider {
26 
27     private ApplicationConfig appConfig;
28 
29     override void register() {
30         container.register!(ConfigManager).singleInstance();
31 
32         registerApplicationConfig();
33 
34         registerRouteConfigManager();
35 
36         registerAuthUserConfig();
37     }
38 
39     protected void registerApplicationConfig() {
40         container.register!ApplicationConfig.initializedBy(&buildAppConfig).singleInstance();
41     }
42 
43     private ApplicationConfig buildAppConfig() {
44         ConfigManager configManager = container.resolve!(ConfigManager)();
45         ApplicationConfig appConfig = configManager.load!(ApplicationConfig);
46 
47         checkWorkerThreads(appConfig);
48 
49         // update the config item with the environment variable if it exists
50         string value = environment.get(ENV_APP_NAME, "");
51         if(!value.empty) {
52             appConfig.application.name = value;
53         }
54 
55         // value = environment.get(ENV_APP_VERSION, "");
56         // if(!value.empty) {
57         //     appConfig.application.version = value;
58         // }
59 
60         value = environment.get(ENV_APP_LANG, "");
61         if(!value.empty) {
62             appConfig.application.defaultLanguage = value;
63         }
64 
65         value = environment.get(ENV_APP_KEY, "");
66         if(!value.empty) {
67             appConfig.application.secret = value;
68         }
69 
70         return appConfig;
71     }
72 
73     private void checkWorkerThreads(ApplicationConfig appConfig) {
74         
75         // Worker pool
76         int minThreadCount = totalCPUs / 4 + 1;
77         // if (appConfig.http.workerThreads == 0)
78         //     appConfig.http.workerThreads = minThreadCount;
79 
80         if (appConfig.http.workerThreads != 0 && appConfig.http.workerThreads < minThreadCount) {
81             warningf("It's better to make the worker threads >= %d. The current value is: %d",
82                     minThreadCount, appConfig.http.workerThreads);
83             // appConfig.http.workerThreads = minThreadCount;
84         }
85 
86         if (appConfig.http.workerThreads == 1) {
87             appConfig.http.workerThreads = 2;
88         }
89 
90         if(appConfig.http.ioThreads <= 0) {
91             appConfig.http.ioThreads = totalCPUs;
92         }
93 
94         // 0 means to use the IO thread
95     }
96 
97     protected void registerRouteConfigManager() {
98         container.register!RouteConfigManager.initializedBy({
99             ConfigManager configManager = container.resolve!(ConfigManager)();
100             ApplicationConfig appConfig = container.resolve!(ApplicationConfig)();
101             RouteConfigManager routeConfig = new RouteConfigManager(appConfig);
102             routeConfig.basePath = configManager.hostEnvironment.configPath();
103 
104             return routeConfig;
105         }).singleInstance();
106     }
107 
108     protected void registerAuthUserConfig() {
109         container.register!AuthUserConfig.initializedBy(() {
110             string userConfigFile = buildPath(APP_PATH, DEFAULT_CONFIG_PATH, DEFAULT_USERS_CONFIG);
111             string roleConfigFile = buildPath(APP_PATH, DEFAULT_CONFIG_PATH, DEFAULT_ROLES_CONFIG);
112             AuthUserConfig config = AuthUserConfig.load(userConfigFile, roleConfigFile);
113 
114             return config;
115         }).singleInstance();
116     }
117 
118     override void boot() {
119         AuthUserConfig userConfig = container.resolve!(AuthUserConfig);
120     }
121 }