-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
174 lines (145 loc) · 5 KB
/
server.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
173
var _ = require('./public/js/underscore')._;
require.paths.unshift('vendor');
require.paths.unshift('vendor/ejs');
require.paths.unshift('vendor/node-couchdb/lib');
var express = require('express/index'),
connect = require('connect/index'),
hl_http_client = require('./lib/highlevel_http_client'),
querystring = require('querystring'),
sys = require('sys'),
node_mail = require('./vendor/node-mail/lib/mail/index');
var app = module.exports = express.createServer();
var couchdb = require('couchdb'), couch_client, db;
var couch_views = require('./lib/couch_views');
var view_helpers = require('./lib/view_helpers');
var Apartment = require('./models/apartment').Apartment,
Query = require('./models/query').Query,
Message = require('./models/message').Message;
sys.puts('RUNNING IN ' + (process.env.EXPRESS_ENV || 'development') + ' environment')
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.logger());
app.use(connect.bodyDecoder());
app.use(connect.methodOverride());
app.use(app.router);
app.use(connect.staticProvider(__dirname + '/public'));
});
app.configure('development', function(){
app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('host', 'localhost:3000');
couch_client = couchdb.createClient(80, 'langalex.couchone.com', 'w4lls', 'upstream');
db = couch_client.db('w4lls_development');
});
app.configure('test', function(){
app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('host', 'localhost:3000');
couch_client = couchdb.createClient(80, 'langalex.couchone.com', 'w4lls', 'upstream');
db = couch_client.db('w4lls_test');
});
app.configure('production', function(){
app.use(connect.errorHandler());
app.set('host', 'four.w4lls.com');
couch_client = couchdb.createClient(80, 'langalex.couchone.com', 'w4lls_production', process.env.COUCHONE_PASSWORD);
db = couch_client.db('w4lls_production');
});
app.db = db;
app.helpers({
host: app.settings.host
});
if(!process.env.SKIP_UPDATE_VIEWS) {
sys.puts('updating views. set SKIP_UPDATE_VIEWS to skip this');
couch_views.update_views(db, _);
};
// Routes
app.put('/update_views', function(req, res) {
couch_views.update_views(db, _);
res.send(201);
});
app.get('/', function(req, res){
if(req.headers.host != app.settings.host) {
res.redirect('http://' + app.settings.host + '/', 301);
} else {
res.render('index.ejs');
};
});
app.get('/about', function(req, res) {
res.render('about.ejs');
});
app.get('/tags', function(req, res) {
db.view('apartment', 'by_tags', {startkey: req.query.q, endkey: req.query.q + "\u9999", limit: 20, group: true}, function(err, results) {
if(err) {
send_error(res, err);
} else {
res.send(results.rows.map(function(row) {return row.key}).join("\n"));
}
})
});
app.get('/geolocation', function(req, res) {
var address = querystring.stringify({address: req.query.q + ', Berlin, Germany', sensor: 'false'});
hl_http_client.get('maps.google.com', '/maps/api/geocode/json?' + address, function(err, body) {
if(err) {
send_error(res, err);
} else {
var location = JSON.parse(body).results[0].geometry.location;
res.send(location, 200);
}
});
});
app.post('/apartments', function(req, res) {
var address = querystring.stringify({address: req.body.apartment.street + ', ' + req.body.apartment.postcode + ', Berlin, Germany', sensor: 'false'});
hl_http_client.get('maps.google.com', '/maps/api/geocode/json?' + address, function(err, body) {
if(err) {
send_error(res, err);
} else {
var location = JSON.parse(body).results[0].geometry.location;
var doc = Apartment.from_params(req.body.apartment, location, req.body.transloadit);
db.saveDoc(doc, function(_err, ok) {
if(_err) {
send_error(res, _err);
} else {
doc._id = ok.id;
doc._rev = ok.rev;
res.send(doc, 201);
}
});
}
});
});
app.get('/apartments', function(req, res) {
var query = Query.build(req.query);
if(query.length > 0) {
db.request('/_fti/_design/apartment/by_filters', {q: query, include_docs: true}, function(err, results) {
if(err) {
send_error(res, err);
} else {
res.send(results.rows.map(function(row) {return row.doc}));
}
});
} else {
db.view('apartment', 'all', {include_docs: true}, function(err, results) {
if(err) {
send_error(res, err);
} else {
res.send(results.rows.map(function(row) {return row.doc}));
}
})
};
});
app.post('/messages', function(req, res) {
db.getDoc(req.body.apartment_id, function(err, apartment) {
if(err) {
send_error(res, err);
} else {
Message.send(apartment.email, req.body.message);
res.send(201);
}
});
});
function send_error(res, er) {
res.send(JSON.stringify(er), 500);
};
// Only listen on $ node app.js
if(!module.parent) {
app.listen(parseInt(process.env.PORT || 3000, 10));
}