forked from glynnbird/simple-autocomplete-service
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
executable file
·133 lines (107 loc) · 3.93 KB
/
app.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
var express = require('express'),
cors = require('cors'),
multer = require('multer'),
compression = require('compression'),
isloggedin = require('./lib/isloggedin.js'),
autocomplete = require('./lib/autocomplete.js');
// cfenv provides access to your Cloud Foundry environment
var cfenv = require('cfenv');
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
if (process.env.ETCD_URL) {
var registry = require('./lib/registry.js')()
registry.register("search", "autocomplete-service", {
url: appEnv.url,
name: "Simple Autocomplete Service",
username: ((process.env.LOCKDOWN && process.env.SAS_LOCKDOWN_USERNAME) ? process.env.SAS_LOCKDOWN_USERNAME : null),
password: ((process.env.LOCKDOWN && process.env.SAS_LOCKDOWN_PASSWORD) ? process.env.SAS_LOCKDOWN_PASSWORD : null)
},
{ ttl: 10 });
}
// Use Passport to provide basic HTTP auth when locked down
var passport = require('passport');
passport.use(isloggedin.passportStrategy());
// multi-part uploads
var multipart = multer({ dest: process.env.TMPDIR, limits: { files: 1, fileSize: 100000000 }});
// posted body parser
var bodyParser = require('body-parser')({extended:true})
// create a new express server
var app = express();
// compress all requests
app.use(compression());
// get the public UI
app.get("/", isloggedin.auth, function(req,res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get("/index.html", isloggedin.auth, function(req,res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get("/autocomplete.html", isloggedin.auth, function(req,res) {
res.sendFile(__dirname + '/public/autocomplete.html');
});
app.get("/doc.html", isloggedin.auth, function(req,res) {
res.sendFile(__dirname + '/public/doc.html');
});
// import a new data set either with a file upload or a url parameter
// :name - the name of the data set to upload
// file - the uploaded file
// url - the url of the file to fetch
app.post('/api/:name', isloggedin.auth, multipart, function(req, res) {
if (req.files && typeof req.files.file == 'object') {
console.log("Import from file", req.files.file.path,"into index",req.body.name)
autocomplete.importFile(req.files.file.path, req.params.name, function(err, data) {
res.send({ok:true, summary: data});
});
} else if (req.body && req.body.name && req.body.url) {
console.log("Import from URL", req.body.url,"into index",req.body.name)
autocomplete.importURL(req.body.url, req.body.name, function(err, data) {
res.send({ok:true});
});
} else {
res.status(404).send({ok: false});
}
});
// add a new value to an existing data set
app.put('/api/:name', isloggedin.auth, bodyParser, function(req, res) {
autocomplete.append(req.params.name, req.body.term, function(err, data) {
if (err) {
return res.send(err);
}
res.send({ ok: true })
})
});
// get a list of data sets
app.get("/api", isloggedin.auth, function(req,res) {
autocomplete.list(function(err,data) {
if (err) {
return res.send([]);
}
res.send(data);
});
});
// query the data set ":name".
// :name - the name of the data set to query
// term - the search term
app.get("/api/:name", cors(), function(req,res) {
autocomplete.query(req.params.name, req.query.term, function(err, data) {
if (err) {
return res.send([]);
}
res.send(data);
});
});
// delete a data set
// :name - the name of the data set to deletes
app.delete("/api/:name", isloggedin.auth, function(req, res) {
autocomplete.deleteIndex(req.params.name, function(err, data) {
res.send({"ok": true});
});
});
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
require("cf-deployment-tracker-client").track();