Commit f931dbf0 authored by Nicolas Gelot's avatar Nicolas Gelot
Browse files

Update '/' endpoint to return json

Very useful for test purpose, we have to bench and test that endpoint
to ensure that the main feature works fine. From test part this is
easier to deal with json object.
parent ff600724
......@@ -20,6 +20,7 @@ with one command.
- Run the docker-compose **up** command to start the project ``docker-compose up --build``
- Getting the ip of the spot service and go to http://<spot-ip>:8888
- Or you can use the command line ``curl -X POST -F 'category=general' -F 'language=en-US' -F 'q=lequipe' -F 'time_range=' -F 'output=json' http://<spot-ip>:8888/``
.. note:: Here the command to get the IP of the spot service
``docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-spot_spot_1``
......
......@@ -452,21 +452,26 @@ def config_results(results, query):
result['publishedDate'] = format_date(publishedDate)
def index_error():
request.errors.append(gettext('search error'))
return render(
'index.html',
)
def index_error(exn, output):
user_error = gettext("search error")
if output == "json":
return jsonify({"error": f"{user_error}: {exn}"})
request.errors.append(user_error)
return render('index.html')
@app.route('/search', methods=['GET', 'POST'])
@app.route('/', methods=['GET', 'POST'])
def index():
# check the response format
output = request.form.get("output", "html")
# check if there is query
if request.form.get('q') is None:
return render(
'index.html',
)
if output == 'json':
return jsonify({}), 204
return render('index.html')
selected_category = request.form.get('category') or 'general'
first_page = request.form.get('pageno')
......@@ -489,9 +494,9 @@ def index():
# is it an invalid input parameter or something else ?
if issubclass(e.__class__, SearxParameterException):
return index_error(), 400
return index_error(e, output), 400
else:
return index_error(), 500
return index_error(e, output), 500
if is_general_first_page:
result_copy = copy.copy(search_data.results)
......@@ -512,8 +517,7 @@ def index():
config_results(images, search_data.query)
config_results(videos, search_data.query)
return render(
'results.html',
response = dict(
results=search_data.results,
q=search_data.query,
selected_category=selected_category,
......@@ -521,12 +525,12 @@ def index():
time_range=search_data.time_range,
number_of_results=format_decimal(search_data.results_number),
advanced_search=request.form.get('advanced_search', None),
suggestions=search_data.suggestions,
answers=search_data.answers,
corrections=search_data.corrections,
suggestions=list(search_data.suggestions),
answers=list(search_data.answers),
corrections=list(search_data.corrections),
infoboxes=search_data.infoboxes,
paging=search_data.paging,
unresponsive_engines=search_data.unresponsive_engines,
unresponsive_engines=list(search_data.unresponsive_engines),
current_language=match_language(search_data.language,
LANGUAGE_CODES,
fallback=settings['search']['language']),
......@@ -536,6 +540,9 @@ def index():
theme=get_current_theme_name(),
favicons=global_favicons[themes.index(get_current_theme_name())]
)
if output == 'json':
return jsonify(response)
return render('results.html', **response)
@app.route('/about', methods=['GET'])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment