This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (86 loc) · 2.2 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
85
86
87
88
89
90
var util = require('util'),
assert = require('assert'),
Joi = require('joi'),
Boom = require('boom'),
Hapi = require('hapi'),
server = new Hapi.Server({connections: {routes: {cors: true}}}),
cassandra = require('cassandra-driver'),
client = new cassandra.Client({contactPoints: ['localhost'], keyspace: 'mykeyspace'});
server.connection({port:8080});
server.views({
engines: { html: require('handlebars')},
path: 'templates'
});
server.register({register: require('lout'), options: { endpoint: '/docs' }}, function (err) {
if (err) {
console.error('Failed loading plugin: lout');
}
});
server.route([{
method: 'GET',
path: '/status',
handler: function(request,reply) {
reply('ok');
}
}, {
method: 'GET',
path: '/favicon.ico',
handler: function(request,reply) {
reply(Boom.notFound());
}
}, {
method: 'GET',
path: '/blog',
handler: function(request,reply) {
client.execute("SELECT * FROM posts", function(err,results){
assert.ifError(err);
reply.view('posts.html', {results: results});
});
}
}, {
method: 'GET',
path: '/blog/{title}',
config: {
handler: function(request,reply) {
client.execute("SELECT * FROM posts WHERE title = ?", [request.params.title], function(err,result){
assert.ifError(err);
if(result.length < 1){
reply(new Error('no results'));
}else{
reply.view('post.html', result[0]);
}
});
},
validate: {
params: {
title: Joi.string().required()
}
}
}
}, {
method: 'GET',
path: '/{name}',
config: {
handler: function(request,reply) {
var greet = 'nice' in request.query && request.query.nice === 'true' ? 'Hello ' : 'Piss off ';
reply.view('index.html', {greeting: greet + request.params.name});
},
validate: {
params: {
name: Joi.string().required()
},
query: {
nice: Joi.boolean().allow(null)
}
}
}
}, {
method: 'GET',
path: '/',
handler: function(request,reply) {
reply.view('index.html', {greeting: 'Hello Anonymous Coward!'});
}
}]);
server.start( function() {
console.log('Hapi server started at: ' + server.info.uri);
});