[search] Added simple search http server.

This commit is contained in:
Yuri Gorshenin 2017-03-02 19:11:41 +03:00
parent db8081d072
commit eb058b64da
3 changed files with 87 additions and 27 deletions

View file

@ -15,6 +15,7 @@
#include "coding/file_name_utils.hpp"
#include "geometry/mercator.hpp"
#include "geometry/rect2d.hpp"
#include "base/logging.hpp"
@ -69,22 +70,6 @@ void Init(string const & resource_path, string const & mwm_path)
g_storage->Init(&DidDownload, &WillDelete);
}
struct LatLon
{
LatLon() = default;
LatLon(double lat, double lon) : m_lat(lat), m_lon(lon) {}
string ToString() const
{
ostringstream os;
os << "lat: " << m_lat << ", lon: " << m_lon;
return os.str();
}
double m_lat = 0.0;
double m_lon = 0.0;
};
struct Mercator
{
Mercator() = default;
@ -123,14 +108,14 @@ struct Params
string ToString() const
{
ostringstream os;
os << m_query << ", " << m_locale << ", " << m_latLon.ToString() << ", "
os << m_query << ", " << m_locale << ", " << m_position.ToString() << ", "
<< m_viewport.ToString();
return os.str();
}
string m_query;
string m_locale;
LatLon m_latLon;
Mercator m_position;
Viewport m_viewport;
};
@ -193,7 +178,8 @@ struct SearchEngineProxy
sp.m_query = params.m_query;
sp.m_inputLocale = params.m_locale;
sp.m_mode = search::Mode::Everywhere;
sp.SetPosition(params.m_latLon.m_lat, params.m_latLon.m_lon);
sp.SetPosition(MercatorBounds::YToLat(params.m_position.m_y),
MercatorBounds::XToLon(params.m_position.m_x));
sp.m_suggestsEnabled = false;
auto const & bottomLeft = params.m_viewport.m_min;
@ -218,12 +204,6 @@ BOOST_PYTHON_MODULE(pysearch)
def("init", &Init);
class_<LatLon>("LatLon")
.def(init<double, double>())
.def_readwrite("lat", &LatLon::m_lat)
.def_readwrite("lon", &LatLon::m_lon)
.def("to_string", &LatLon::ToString);
class_<Mercator>("Mercator")
.def(init<double, double>())
.def_readwrite("x", &Mercator::m_x)
@ -239,7 +219,7 @@ BOOST_PYTHON_MODULE(pysearch)
class_<Params>("Params")
.def_readwrite("query", &Params::m_query)
.def_readwrite("locale", &Params::m_locale)
.def_readwrite("lat_lon", &Params::m_latLon)
.def_readwrite("position", &Params::m_position)
.def_readwrite("viewport", &Params::m_viewport)
.def("to_string", &Params::ToString);

View file

@ -22,7 +22,7 @@ engine = search.SearchEngine()
params = search.Params()
params.query = 'кафе юность'
params.locale = 'ru'
params.lat_lon = search.LatLon(55.751633, 37.618705)
params.position = search.Mercator(37.618705, 67.455669)
params.viewport = search.Viewport(search.Mercator(37.1336, 67.1349),
search.Mercator(38.0314, 67.7348))
for result in engine.query(params):

View file

@ -0,0 +1,80 @@
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from __future__ import print_function
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import argparse
import json
import os
import pysearch
import urlparse
DIR = os.path.dirname(__file__)
RESOURCE_PATH = os.path.realpath(os.path.join(DIR, '..', '..', 'data'))
MWM_PATH = os.path.realpath(os.path.join(DIR, '..', '..', 'data'))
PORT=8080
class HTTPHandler(BaseHTTPRequestHandler):
def do_GET(self):
result = urlparse.urlparse(self.path)
query = urlparse.parse_qs(result.query)
def sparam(name):
return query[name][-1]
def fparam(name):
return float(sparam(name))
params = pysearch.Params()
try:
params.query = sparam('query')
params.locale = sparam('locale')
params.position = pysearch.Mercator(fparam('posx'), fparam('posy'))
params.viewport = pysearch.Viewport(pysearch.Mercator(fparam('minx'), fparam('miny')),
pysearch.Mercator(fparam('maxx'), fparam('maxy')))
except KeyError:
self.send_response(400)
return
results = HTTPHandler.engine.query(params)
responses = [{'name': result.name,
'address': result.address,
'has_center': result.has_center,
'center': {'x': result.center.x,
'y': result.center.y
}
}
for result in results]
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
json.dump(responses, self.wfile)
def main(args):
pysearch.init(args.r, args.m)
engine = pysearch.SearchEngine()
HTTPHandler.engine = pysearch.SearchEngine()
try:
print('Starting HTTP server on port', PORT)
server = HTTPServer(('', args.p), HTTPHandler)
server.serve_forever()
finally:
server.socket.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-r', metavar='RESOURCE_PATH', default=RESOURCE_PATH,
help='Path to resources directory.')
parser.add_argument('-m', metavar='MWM_PATH', default=MWM_PATH,
help='Path to mwm files.')
parser.add_argument('-p', metavar='PORT', default=PORT,
help='Port for the server to listen')
args = parser.parse_args()
main(args)