Skip to content

Commit

Permalink
Added proxy for elasticsearch requests
Browse files Browse the repository at this point in the history
Strings are now properly sanitized before being sent to the
elasticsearch server.
Since this API endpoint is on the same domain, this also removes
the requirement to allow CORS in elasticsearch.

Closes #1
  • Loading branch information
wfriesen committed Feb 14, 2016
1 parent 02b0387 commit 2ad363e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ Search across subtitle files to find relevant screenshots
* Load the extracted JSON subtitles using commands like...
`curl -XPOST 'http://localhost:9200/seinfeld/external' -d @json/filename.json`. I used `find` with `-exec` to help with this.
* Create 1 screenshot per second from your video files with something like `ffmpeg -i Video.mkv -vf fps=1 %d.png`. Each video file should have it's screenshots saved into a separate folder, and serve the root folder with `python -m SimpleHTTPServer`
* To allow the web app to communicate with Elasticsearch, change the CORS permissions with...

```
vagrant ssh
vi /etc/elasticsearch/es-01/elasticsearch.yml
# Add these lines...
http.cors.enabled : true
http.cors.allow-origin : "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length
```

* Set up the web application in `./webapp` with `npm install`
* Start it with `npm start`. Your browser should launch with The Jerryatric!
1 change: 1 addition & 0 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"babel-core": "5.8.34",
"bluebird": "3.1.1",
"classnames": "2.2.1",
"elasticsearch-sanitize": "2.0.0",
"eventemitter3": "1.1.1",
"express": "4.13.3",
"fastclick": "1.0.6",
Expand Down
22 changes: 22 additions & 0 deletions webapp/src/api/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Router } from 'express';
import fetch from '../core/fetch';
import escapeElastic from 'elasticsearch-sanitize';

const router = new Router();

router.get('/', async (req, res, next) => {
try {
const q = encodeURIComponent(escapeElastic(req.query.q));
const searchUrl = 'http://localhost:9200/seinfeld/_search?q=text:' + q;
const response = await fetch(searchUrl);
const content = await response.json();
const results = content.hits.hits.map(function getResultObject(hit) {
return hit._source;
});
res.status(200).send({ results });
} catch (err) {
next(err);
}
});

export default router;
22 changes: 11 additions & 11 deletions webapp/src/components/SearchArea/SearchArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class SearchArea extends Component {

_handleKeyPress(e) {
if (e.key === 'Enter') {
axios.get('http://localhost:9200/seinfeld/_search?q=text:' + e.target.value)
.then(function processResponse(response) {
const results = response.data.hits.hits.map(function getResultObject(hit) {
return hit._source;
const query = e.target.value;
if (query) {
axios.get('/api/search?q=' + query)
.then(function processResponse(response) {
this.setState({
results: response.data.results,
});
}.bind(this))
.catch(function error(response) {
console.log(response);
});
this.setState({
results,
});
}.bind(this))
.catch(function error(response) {
console.log(response);
});
}
}
}

Expand Down
1 change: 1 addition & 0 deletions webapp/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ server.use(express.static(path.join(__dirname, 'public')));
// Register API middleware
// -----------------------------------------------------------------------------
server.use('/api/content', require('./api/content'));
server.use('/api/search', require('./api/search'));

//
// Register server-side rendering middleware
Expand Down
1 change: 1 addition & 0 deletions webapp/tools/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const config = {
include: [
path.resolve(__dirname, '../node_modules/react-routing/src'),
path.resolve(__dirname, '../node_modules/axios'),
path.resolve(__dirname, '../node_modules/elasticsearch-sanitize'),
path.resolve(__dirname, '../src'),
],
loader: 'babel-loader',
Expand Down

0 comments on commit 2ad363e

Please sign in to comment.