-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cfc
65 lines (58 loc) · 2.69 KB
/
Application.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
component output='false' hint='A basic SQL Client for MSSQL and DB2'{
function onApplicationStart(){
this.reload();
}
function onRequestStart(required string targetPage){
// Manually refresh application - http://127.0.0.1:1234/index.cfm?reloadApp
if(structKeyExists(url, 'reloadApp')){
this.reload();
}
}
public void function reload(){
try{
application.utils = createObject('component', 'components.utils').init();
this.loadConfiguration('config.json');
} catch(any e){
application.utils.handleError(errMsg='Error starting application.', e=e, isFatal=true, toScreen=true);
}
}
// Load relevant configuration data into application scope
private void function loadConfiguration(required string configPath){
application.config = structNew();
local.config = application.utils.readJsonFile(arguments.configPath);
if(!structKeyExists(local.config, 'secretKey')){
throw "Credentials have not been encrypted. Please run 'task run tasks/setup'.";
}
this.loadDatasources(local.config);
application.config.timeout = structKeyExists(local.config, 'timeout') ? local.config['timeout'] : 5;
application.config.maxrows = structKeyExists(local.config, 'maxrows') ? local.config['maxrows'] : 100;
application.config.secretKey = local.config.secretKey;
}
private void function loadDatasources(required struct config){
if(!structKeyExists(arguments.config, 'datasources')){
throw "Key 'datasources' was not found in configuration file.";
}
for(local.dsConfig in arguments.config.datasources){
try{
local.datasource = {
'class': local.dsConfig['class'],
'connectionString': local.dsConfig['connectionString'],
};
local.datasource['username'] = local.dsConfig['username'];
local.datasource['password'] = local.dsConfig['password'];
application.datasources[local.dsConfig.name] = local.datasource;
} catch(any e){
application.utils.handleError(errMsg="Error reading datasource configuration.", e=e, isFatal=true);
}
}
}
public array function getDatasources(){
return this.datasources();
}
public struct function getDatasource(required string dsName){
if(structKeyExists(this.datasources, arguments.dsName)){
return this.datasources[arguments.dsName];
}
application.utils.handleError(errMsg="Could not find datasource with key '#arguments.dsName#'.");
}
}