Search & show listings

This commit is contained in:
Edin Dazdarevic
2017-03-31 14:31:56 +02:00
parent c32e49e6c3
commit d6c2e857d0
16 changed files with 1598 additions and 35 deletions

3
backend/.babelrc Normal file
View File

@@ -0,0 +1,3 @@
{
"presets": ["es2015", "es2017"],
}

187
backend/build/server.js Normal file
View File

@@ -0,0 +1,187 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _express = __webpack_require__(1);
var _express2 = _interopRequireDefault(_express);
var _bodyParser = __webpack_require__(2);
var _bodyParser2 = _interopRequireDefault(_bodyParser);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var MongoClient = __webpack_require__(3).MongoClient;
var url = 'mongodb://localhost:27017/example';
__webpack_require__(4);
var router = _express2.default.Router({ mergeParams: true });
var PORT = process.env.PORT || 3001;
var AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
// TODO:
// db.results.ensureIndex({loc:"2d"})
//collection.ensureIndex("username",callback)
router.get('/search', function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) {
var bounds, db, properties, query, _bounds$split$map, _bounds$split$map2, lat1, lng1, lat2, lng2, box, all;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
bounds = req.query.bounds || '';
_context.next = 4;
return MongoClient.connect(url);
case 4:
db = _context.sent;
properties = db.collection('results');
query = {};
if (bounds) {
_bounds$split$map = bounds.split(',').map(parseFloat), _bounds$split$map2 = _slicedToArray(_bounds$split$map, 4), lat1 = _bounds$split$map2[0], lng1 = _bounds$split$map2[1], lat2 = _bounds$split$map2[2], lng2 = _bounds$split$map2[3];
box = [[lat1, lng1], [lat2, lng2]];
query = Object.assign(query, {
loc: {
"$geoWithin": {
"$box": box
}
}
});
}
_context.next = 10;
return properties.find(query).toArray();
case 10:
all = _context.sent;
res.json(all);
res.end();
_context.next = 15;
return db.close();
case 15:
_context.next = 21;
break;
case 17:
_context.prev = 17;
_context.t0 = _context['catch'](0);
console.log('error:', _context.t0);
next(_context.t0);
case 21:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[0, 17]]);
}));
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
}());
var app = (0, _express2.default)();
app.use(_bodyParser2.default.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
app.use('/api', router);
app.listen(PORT, function () {
return console.log('Express server running at localhost: ' + PORT);
});
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = require("express");
/***/ },
/* 2 */
/***/ function(module, exports) {
module.exports = require("body-parser");
/***/ },
/* 3 */
/***/ function(module, exports) {
module.exports = require("mongodb");
/***/ },
/* 4 */
/***/ function(module, exports) {
module.exports = require("babel-polyfill");
/***/ }
/******/ ]);

24
backend/package.json Normal file
View File

@@ -0,0 +1,24 @@
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-es2017": "^6.22.0",
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"express": "^4.15.2",
"isomorphic-fetch": "^2.2.1",
"mongodb": "^2.2.25"
}
}

62
backend/server.js Normal file
View File

@@ -0,0 +1,62 @@
import express from 'express'
import bodyParser from 'body-parser';
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/example';
require("babel-polyfill");
const router = express.Router({mergeParams: true})
const PORT = process.env.PORT || 3001;
const AGENTURA_KEY = process.env.AGENTURA_KEY || '1somethingverysecret';
// TODO:
// db.results.ensureIndex({loc:"2d"})
//collection.ensureIndex("username",callback)
router.get('/search', async (req, res, next) => {
try {
const bounds = req.query.bounds || '';
const db = await MongoClient.connect(url);
const properties = db.collection('results');
let query = {};
if (bounds) {
const [lat1, lng1, lat2, lng2] = bounds.split(',').map(parseFloat)
const box = [[lat1, lng1], [lat2, lng2]];
query = Object.assign(query, {
loc: {
"$geoWithin": {
"$box": box
}
}
});
}
const all = await properties.find(query).toArray();
res.json(all);
res.end();
await db.close();
} catch (e) {
console.log('error:', e);
next(e);
}
});
const app = express()
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
app.use('/api', router);
app.listen(PORT, () => console.log('Express server running at localhost: ' + PORT));

23
backend/webpack.config.js Normal file
View File

@@ -0,0 +1,23 @@
var fs = require('fs')
var path = require('path')
module.exports = {
entry: path.resolve(__dirname, 'server.js'),
output: {
filename: 'build/server.js'
},
target: 'node',
externals: fs.readdirSync(path.resolve(__dirname, 'node_modules')).reduce((ext, mod) => {
ext[mod] = 'commonjs ' + mod
return ext
}, {}),
node: {
__filename: true,
__dirname: true
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=es2017' }
]
}
}

1120
backend/yarn.lock Normal file

File diff suppressed because it is too large Load Diff