-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexCopy.js
103 lines (93 loc) · 2.46 KB
/
indexCopy.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
// Global variables
// WARNING: Use with discretion
global.__basePath = process.cwd();
// All require variables
var Hapi = require('hapi'),
cRedis = require('catbox-redis'),
// routes = require(__basePath+'/routes'),
Cache = require(__basePath+'/cache');
// Temporary - should be cleaned
var Boom = require('boom'),
mongoose = require('mongoose'),
addr = process.env.MONGO_PORT_27017_TCP_ADDR || 'localhost',
port = process.env.MONGO_PORT_27017_TCP_PORT || 27017;
var Schema = mongoose.Schema;
mongoose.connect('mongodb://'+addr+':'+port+'/hapicaching');
var testingSchema = new Schema({name: String});
var Testing = mongoose.model('testing', testingSchema);
// Variable declarations and initialization
var redisHost = process.env.REDIS_PORT_6379_TCP_ADDR || 'localhost';
server = new Hapi.Server({
cache: {
name : 'redis',
engine : require('catbox-redis'),
host : redisHost,
partition : 'hapiCaching'
}
});
server.connection({port:3005});
var Find = function (params, next) {
Testing.find().exec(function (err, result) {
if (result.length > 0) {
data = {
count : result.length,
data : result
};
return next(null, data);
}
});
};
server.method([
{
name: 'find',
method: Find,
options: {
cache: {
cache : 'redis',
expiresIn : 20 * 1000,
},
generateKey: function (para) {
// console.log(para.model);
return para.model;
}
}
}
]);
server.route([
{
method : 'GET',
path : '/',
handler : function (req, res) {
res('Yow');
}
},
{
method : '*',
path : '/something',
handler : function (req, res) {
var param = {model: 'testing'};
var random = 'Testing' + Math.floor(Math.random() * 100);
Testing.create({name: random}, function (err, response) {
return res(err, response);
});
var param = {model: 'testing'};
return server.methods.find.cache.drop(param, function (err, result, cache, report) {});
}
},
{
method : '*',
path : '/{model}/list',
handler : function (req, res) {
server.methods.find(req.params, function (err, result, cache, report) {
if (cache) console.log('cache ok');
console.log(report);
return res(result);
});
}
}
]);
// server.route(routes);
server.start(function (err) {
if (err) console.log(err);
console.log('Server running at:', server.info.uri);
});