Skip to content
Snippets Groups Projects
Commit 94daea90 authored by Marc Feger's avatar Marc Feger
Browse files

Add flask :sparkles: and swagger documentations :books:

parent 8bb665f2
No related branches found
No related tags found
No related merge requests found
Showing
with 418 additions and 0 deletions
......@@ -29,6 +29,15 @@ services:
- ./wait-for-it.sh:/usr/share/logstash/wait-for-it.sh
command: ["./wait-for-it.sh", "elasticsearch:9200", "--", "bin/logstash"]
flask:
build: flask
ports:
- "5000:5000"
volumes:
- ./flask:/flask
- ./wait-for-it.sh:/flask//wait-for-it.sh
command: ["./wait-for-it.sh", "elasticsearch:9200", "--", "python3.7", "app.py"]
networks:
default:
external:
......
FROM python:3.7-slim
ENV PYTHONPATH /flask
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /flask
WORKDIR /flask
RUN pip install -r requirements.txt
\ No newline at end of file
from flask import Flask, jsonify, request, render_template
from elasticsearch import Elasticsearch
from queries.fuzzy import fuzzy_search
app = Flask(__name__, template_folder=".")
@app.route("/")
def hello():
return render_template("doc/swagger-ui/" + "index.html")
@app.route("/search")
def search():
"""
Route to do a fuzzy-search for textversions.
:return: Results of the fuzzy-search.
"""
text = request.args.get("q")
elastic = Elasticsearch([{"host": "elasticsearch", "port": 9200}])
result = elastic.search(index="textversions", doc_type="record", body=fuzzy_search(text))
return jsonify(result=result["hits"]["hits"])
if __name__ == "__main__":
app.run(host="0.0.0.0", use_reloader=True, use_debugger=True, use_evalex=True)
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DKE API</title>
<link rel="stylesheet" type="text/css" href="../../static/swagger/swagger-ui.css">
<link rel="icon" type="image/png" href="../../static/swagger/favicon-32x32.png" sizes="32x32"/>
<link rel="icon" type="image/png" href="../../static/swagger/favicon-16x16.png" sizes="16x16"/>
<style>
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
margin: 0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="../../static/swagger/swagger-ui-bundle.js"></script>
<script src="../../static/swagger/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function () {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "../static/swagger/swagger_documentation.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
// End Swagger UI call region
window.ui = ui
}
</script>
</body>
</html>
<!doctype html>
<html lang="en-US">
<body onload="run()">
</body>
</html>
<script>
'use strict';
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;
if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1);
} else {
qp = location.search.substring(1);
}
arr = qp.split("&")
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value)
}
) : {}
isValid = qp.state === sentState
if ((
oauth2.auth.schema.get("flow") === "accessCode"||
oauth2.auth.schema.get("flow") === "authorizationCode"
) && !oauth2.auth.code) {
if (!isValid) {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
});
}
if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
let oauthErrorMsg
if (qp.error) {
oauthErrorMsg = "["+qp.error+"]: " +
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
(qp.error_uri ? "More info: "+qp.error_uri : "");
}
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
});
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}
</script>
from typing import Dict
def fuzzy_search(text: str) -> Dict:
"""
Fuzzy query to search.
This also returns the highlighted result.
:param text: The text to be searched for.
:return: Results of the query with highlighted content.
"""
return {
"query": {
"match": {
"content": {
"analyzer": "simple",
"query": "*" + text + "*",
"fuzziness": "AUTO",
"prefix_length": 1
}
}
},
"highlight": {
"pre_tags": ["<em>"],
"post_tags": ["</em>"],
"fields": {
"content": {
"highlight_query": {
"match": {
"content": {
"analyzer": "simple",
"query": "*" + text + "*",
"fuzziness": "AUTO",
"prefix_length": 1
}
}
}
}
}
}
}
Flask==1.1.1
elasticsearch>=6.0.0,<7.0.0
\ No newline at end of file
flask/static/swagger/favicon-16x16.png

665 B

flask/static/swagger/favicon-32x32.png

628 B

This diff is collapsed.
This diff is collapsed.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
This diff is collapsed.
{"version":3,"sources":[],"names":[],"mappings":"","file":"swagger-ui.css","sourceRoot":""}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
openapi: 3.0.0
servers:
- url: 'http://0.0.0.0:5000/'
info:
version: "1.0"
title: D-BASearch
paths:
/search:
get:
summary: Assemble the DBPedia groundtruth with english labels.
description: >-
This function assembles the groundtruth of DBPedia.
It searches for all Comedy Films which have at least one director who is born after or on 1970.
This is done in more then one step and may take a while. 1-2 Minutes.
tags:
- DBPedia
parameters:
- in: query
name: q
schema:
type: string
description: The number of items to skip before starting to collect the result set
responses:
200:
description: OK
content:
application/json:
schema:
properties:
results:
type: object
properties:
_id:
type: string
example: "42"
_index:
type: string
example: "textversions"
_score:
type: number
example: 3.14
_source:
type: object
properties:
"@timestamp":
type: string
format: date-time
"@version":
type: string
example: "1"
author_uid:
type: integer
example: 2
content:
type: string
example: "we should get a cat"
is_disabled:
type: boolean
example: false
issue_uid:
type: integer
example: 1
statement_uid:
type: integer
example: 1
timestamp:
type: string
format: date-time
uid:
type: integer
example: 2
_type:
type: string
example: "record"
highlight:
type: object
properties:
content:
type: array
items:
type: string
example: ["we should get a <em>cat</em>"]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment