forked from sevnote/sevnote-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
executable file
·98 lines (81 loc) · 2.56 KB
/
api.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
var fibers = require("fibers");
var fs = require('fs');
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
var RedisStore = require('connect-redis')(express);
var cors = require('cors');
var common = require('./lib/common');
var http = require('http').Server(app);
var path = require('path');
var util = require('util');
moment = require('moment');
config = require("./config/config.json")[app.get("env")];
upload = require('jquery-file-upload-middleware');
moment = require('moment');
_ = require('underscore');
var mysql = require('./lib/mysql');
var rand = require("random-key");
console.log(rand.generate());
GLOBAL.API_PATH = config.api_domain;
// all environments
app.use(cors());
app.set('port', config.app_port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('FhDIjP5784Us9M1V'));
app.use(express.session({
store: new RedisStore(config.redis_store)
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
//Upload Function
app.use('/upload', function(req, res, next) {
// imageVersions are taken from upload.configure()
upload.fileHandler({
uploadDir: function() {
return 'public/uploads'
}
})(req, res, next);
});
//Upload Finish Event
upload.on('end', function(fileInfo) {
var extension = common.extension(fileInfo.originalName);
var target_path = __dirname + '/public/uploads/' + fileInfo.filename + '.jpeg';
// Move File
var tmp_path = __dirname + '/public/uploads/' + fileInfo.originalName;
fs.rename(tmp_path, target_path, function(err) {
if (err) throw err;
fs.unlink(tmp_path, function() {
if (err) throw err;
});
});
});
app.all("*", function(req, res, next) {
fibers(function() {
next();
}).run();
})
app.get("/", function(req, res, next) {
res.json({
RetCode: 10000,
ErrorMessage: 'Fatal error'
})
});
var methods_path = util.format("./methods/%s",(config.api_version));
fs.readdir(methods_path,function(err,methods){
for(var i in methods){
var method = methods[i].split(".").shift();
var method = methods_path + '/' + method;
app.use(require(method))
}
});
http.listen(app.get('port'), function() {
console.info("listening :" + app.get("port"));
});