forked from bubersson/humla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumla.js
175 lines (147 loc) · 4.94 KB
/
humla.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Humla Module
* ~~~~~~~~~~~~
* usage:
* var humla = require("./humla");
* humla.init(config,false);
*
* -- Full usage example is at the bottom of this file
*
*/
var fs = require('fs');
var path = require('path');
// global config variable // TODO: this is pretty ugly to bind var to global context...
config = {};
var models = {};
/**
* Init Humla, setup and load plugins from config file.
* Plugins can be listed with or without src param
* Note that some extensions may require Mongoose DB
* @param plugins Object of plugins (e.g. {"slideindex":{"enable":true,"src":"./server_ext/slideindex/slideindex_ext.js"}})
* @param usedb boolean value
*/
exports.init = function(plugins, usedb){
var c_in = plugins;
var plugin,src;
// load config to global context
config=loadConfig('./server-config.json') || {};
// load plugins from config if no plugins param is provided
if(typeof c_in !== "object") {
c_in = {};
for(var i = 0; i<config.plugins.length;i++) {
if (config.plugins[i].enable) c_in[config.plugins[i].id] = config.plugins[i];
}
}
// initialize db and load models
if(usedb) {
var mongoose = require('mongoose');
mongoose.connect(config.server.db_uri);
var MODELS_DIRECTORY = (path.join(path.dirname(__filename), config.server.models_relative_path)).toString();
models = loadFiles(MODELS_DIRECTORY);
models.forEach(function (model){
require(model);
});
}
// load and export plugins
for(var p in c_in) {
if(c_in.hasOwnProperty(p)) {
plugin = c_in[p];
if(plugin.src) {
src = plugin.src
} else {
src = getPluginConfig(p,config).src;
if(!src) {
//console.log("ERR: Plugin with name '"+p+"' is not in server-config.json");
throw new Error("Plugin with name '"+p+"' is not in server-config.json. Please provide 'src' attribute!");
}
}
if(plugin.enable) {
console.log("Loading Humla plugin... "+JSON.stringify(p));
exports[p] = require(src);
}
}
}
}
/**
* Load and parse JSON config file
* @param filename Relative path to JSON config file
*/
function loadConfig(filename) {
try {
var data = fs.readFileSync(filename).toString();
config = JSON.parse(data);
return config;
} catch (err) {
/*console.log('There has been an error parsing your JSON Configuration (server-config.json)');
console.log(err);*/
throw 'There has been an error parsing your JSON Configuration (server-config.json)\n'+err;
}
}
/**
* Tests if string ends with given suffix
*/
function endsWith(string, suffix) {
return string.indexOf(suffix, string.length - suffix.length) !== -1;
}
/**
* Return array of paths
* @param path Folder path to .js files
*/
function loadFiles(path) {
var arr =[];
var files = fs.readdirSync( path);
files.forEach(function(file) {
if(endsWith(file, "js")){
//var req = require( HANDLERS_DIRECTORY+'/'+file );
arr.push(path+file );
}
});
return arr;
}
/**
* Load single plugin object from config object
* @param name Name of the plugin
* @param config Configurarion object with structure {"plugins":[]}
*/
function getPluginConfig(name,config){
for(var i=0; i<config.plugins.length;i++) {
if (config.plugins[i].id === name) return config.plugins[i];
}
return false;
}
/**
* Example usage
* ~~~~~~~~~~~~~
*
* var humla = require("humla");
*
* This example is executed automatically when run as $node humla.js
*
*/
if(!module.parent) {
//var humla = require("humla"); // use this one in your code
var humla = this; // only for example usage
// Either load default plugins by:
// humla.init(null,true); // OR list them as following
// load plugins and enable db
humla.init({
"administration":{"enable":true},
"atom":{"enable":true},
"editor":{"enable":true},
"facetparser":{"enable":true},
"facet":{"enable":true},
"maintenance":{"enable":true},
"gbooks":{"enable":true},
"microdata":{"enable":true},
"slideindex":{"enable":true}
},true);
// get course, params: courseID, express_callback, internal_callback
// list all available methods
console.log(humla.administration);
console.log(humla.gbooks);
// See extension docs for further details
humla.administration.getCourse("MI-MDW",undefined,function(err,data){
if(!err) console.log(JSON.stringify(data));
else console.log("MI-MDW not found");
});
}