Read-only mirror of https://github.com/gisktzh/gb3-search_api — Kanton Zürich. Issues & pull requests at the source.
  • Python 95.9%
  • Dockerfile 4.1%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2024-11-20 10:06:34 +01:00
.azure-pipelines Add coverage 2023-11-15 13:44:48 +01:00
.github/workflows GB3-1647: Add GitHub Actions 2024-09-03 08:09:58 +02:00
dtos Fix query builder 2024-02-26 13:37:02 +01:00
enums Use proper pydantic types for query 2024-02-26 13:01:13 +01:00
indexes Add error handling for incorrectly configured index 2024-02-26 13:12:44 +01:00
tests Compare pydantic models instead of dictionaries 2024-02-26 15:01:31 +01:00
utils Add tests for index_utils 2024-02-26 14:00:19 +01:00
.gitignore PEP8 2023-05-03 16:58:31 +02:00
Dockerfile GB3-1160: Add SSL encryption 2024-03-21 13:16:09 +01:00
LICENSE.txt Added LICENSE.txt to / 2024-08-29 12:41:15 +00:00
main.py Use proper pydantic types for query 2024-02-26 13:01:13 +01:00
Pipfile Update dependency fastapi to v0.114.2 2024-09-14 00:10:19 +00:00
Pipfile.lock Update dependency fastapi to v0.114.2 2024-09-14 00:10:19 +00:00
pyproject.toml Add basic tests 2023-11-13 09:58:11 +01:00
README.md GB3-1160: Add SSL encryption 2024-03-21 13:16:09 +01:00
renovate.json GB3-1658: Modified renovate.json my extracting all important global configs and moved it to this local one. 2024-09-03 13:20:17 +02:00
sonar-project.properties GB3-1647: Add sonar settings 2024-09-17 16:34:40 +02:00

GB3 Search Backend

The search API for the GB3, serves as an interface for Elasticsearch and realized with Python and FastAPI.

Deployment

The search API is deployable with a docker image.

Dependencies

Requires a running instance of Elasticsearch to run properly.

Build

Currently, no specific build steps are needed, so a basic docker build command should do the trick:

docker build -t [name-of-image]:[version] .
# example: docker build -t gb3-search-api:latest .

Run

The image can be run using docker run. As a prerequisite, we enforce the use of HTTPS, so the image requires a key and a certificate file to be present in the container at /certs. The easiest way to achieve this is to mount a volume to the container at this location, e.g. using -v /path/to/certs:/certs. The latter command would mound the host's /path/to/certs to the container's /certs, assuming the key and certificate files are present in the host's /path/to/certs.

It also requires the following environment variables to be set:

  • ELASTIC_URL: Fully-qualified URL to the Elasticsearch URL, e.g. https://localhost:9200
  • ELASTIC_PASSWORD: The password needed to access Elasticsearch

and, optionally, the name of the key file and/or the certificate file if they are not named key.pem and cert.pem,:

  • SSL_KEY_FILE: The name of the key file, defaults to key.pem
  • SSL_CERT_FILE: The name of the certificate file, defaults to cert.pem

The container further exposes port 8000, so a port mapping should be added.

All of these options can be set directly in the docker run command.

An example invocation looks as follows:

docker run -v /path/to/certs:/certs -p 8001:8000 -e SSL_KEY_FILE=differentname.pem -e ELASTIC_PASSWORD=123 -e ELASTIC_URL=http://localhost:9200 gb3-search-api:latest

You can then access https://localhost:8001/docs and verify that the FastAPI client is running. This works even if there is no backend running, so you can specify any localhost URL and spin up the container to make sure it is working. Note that there is no non-HTTPS version of the API, so you need to use HTTPS.

Local Installation

  1. Make sure Elasticsearch is running locally
  2. Install a Pipenv with the necessary packages by running pipenv install
  3. Run the search API with pipenv run uvicorn main:gb3_search --reload

The search API should now listen to http://localhost:8000/. Go to http://localhost:8000/docs to access the API documentation

Running Docker container locally

If you need to build the docker container locally, you also need to have the certificates in the /certs directory. An easy way to get a self-signed certificate is to use the mkcert tool.

Tests

Tests are written using pytest.

Important: Due to some architectural issues, the tests within tests/main cannot be run together. In isolation, they work well; when all are run together, only once succeeds. This is most likely an issue due to how we're using the client and how we're injecting elastic search (it gets no calls); so further investigation is needed to determine how we can reset the mock.