-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (78 loc) · 3.01 KB
/
index.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
var KServer = function () {
var INSTANCE = this;
INSTANCE.forIn = function (coll, fn) {
Object.keys(coll).forEach(function (o) {
fn(coll[o], o);
});
};
var SLASH = "/";
var express = require("express");
INSTANCE.APP_ROOT = __dirname + "/../..";
INSTANCE.path = require("path");
INSTANCE.multer = require("multer");
INSTANCE.shortid = require("shortid");
INSTANCE.mapper = require("sqlite3-orm");
var crudHandlers = require("./handlers.js");
var install = function (models) {
crudHandlers.config(INSTANCE.APP_ROOT, INSTANCE, models);
if (INSTANCE.config.file) {
INSTANCE.app.get(SLASH, crudHandlers.sendAppFile);
}
INSTANCE.forIn(models, function (m, uid) {
if (m.session) {
INSTANCE.config.sessionModel = uid;
INSTANCE.app.post(INSTANCE.api_root + "session/:" + uid,
INSTANCE.regular.array(),
crudHandlers.authenticate);
return;
}
});
INSTANCE.app.use(INSTANCE.api_root + ":model", crudHandlers.handleModel);
INSTANCE.app.use(INSTANCE.api_root + ":model/:id", crudHandlers.handleModel);
INSTANCE.app.get(INSTANCE.api_root + ":model", crudHandlers.readModel);
INSTANCE.app.post(INSTANCE.api_root + ":model", INSTANCE.regular.array(),
crudHandlers.createModel);
INSTANCE.app.put(INSTANCE.api_root + ":model/:id", INSTANCE.regular.array(),
crudHandlers.updateModel);
INSTANCE.app.delete(INSTANCE.api_root + ":model/:id", INSTANCE.regular.array(),
crudHandlers.deleteModel);
INSTANCE.app.listen(INSTANCE.config.port);
console.log("Krasny server listening at ~:" + INSTANCE.config.port);
};
INSTANCE.build = function (prop) {
INSTANCE.models = prop.models;
INSTANCE.config = prop.config;
INSTANCE.app = express();
INSTANCE.api_root = INSTANCE.config.api ? SLASH + INSTANCE.config.api +
SLASH : SLASH;
INSTANCE.mapper.connect(INSTANCE.config.db, INSTANCE.config.verbosedb ||
false);
INSTANCE.regular = INSTANCE.multer();
if (prop.config.uploaddir && prop.config.fileinput) {
var tmp = INSTANCE.multer.diskStorage({
destination: function (req, file, callback) {
callback(null, INSTANCE.config.uploaddir);
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now() + "." +
file.originalname.split(".").pop());
}
});
var upload = INSTANCE.multer({
storage: tmp
}).single(INSTANCE.config.fileinput);
INSTANCE.app.post(INSTANCE.api_root + INSTANCE.config.fileinput,
upload, crudHandlers.fileUpload);
INSTANCE.app.get(INSTANCE.api_root + "download/:filepath",
crudHandlers.fileDownload);
}
INSTANCE.app.use(express.static(INSTANCE.path.join(INSTANCE.APP_ROOT, INSTANCE.config
.rootdir)));
if (INSTANCE.config.reset) {
INSTANCE.mapper.dropTables(INSTANCE.models);
}
INSTANCE.mapper.createTables(INSTANCE.models, install);
};
return INSTANCE;
};
module.exports = new KServer();