diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f8e4aef --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: FastAPI", + "type": "python", + "request": "launch", + "module": "uvicorn", + "args": [ + "coordinates_transformation_api.main:start", + "--reload" + ], + "jinja": true, + "justMyCode": true + } + ] +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4e1086b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.11.4-slim-bullseye +LABEL maintainer="NSGI " + +COPY . /src + +RUN apt-get update && \ + apt-get install -y \ + moreutils && \ + pip install --upgrade setuptools && \ + pip install /src + +# TODO: investigate how to properly setup Dockerfile for production use + +ENTRYPOINT [ "ct-api" ] diff --git a/README.md b/README.md index 8f55b97..2bf5cc6 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,16 @@ Design rues. Examples of these are: parameter. - with additional scenarios for other combinations. +## Install + +```bash +pip3 install . +``` + ## Run ```bash -uvicorn main:app --reload +ct-api ``` ## Exanple operaties @@ -129,3 +135,17 @@ that one forwarded to ### new URL + +## Docker + +### Build container + +```bash +docker build -t nsgi/coordinatestransformation-api . +``` + +### Run container + +```bash +docker run --rm -d -p 8000:8000 --name ct-api nsgi/coordinatestransformation-api +``` diff --git a/coordinates_transformation_api/assets/ __init__.py b/coordinates_transformation_api/assets/ __init__.py new file mode 100644 index 0000000..e69de29 diff --git a/openapi.yaml b/coordinates_transformation_api/assets/openapi.yaml similarity index 100% rename from openapi.yaml rename to coordinates_transformation_api/assets/openapi.yaml diff --git a/main.py b/coordinates_transformation_api/main.py similarity index 78% rename from main.py rename to coordinates_transformation_api/main.py index 1cb1525..be43798 100644 --- a/main.py +++ b/coordinates_transformation_api/main.py @@ -1,12 +1,14 @@ -from fastapi import FastAPI, Response +from fastapi import FastAPI from pydantic import BaseModel -from starlette.responses import RedirectResponse, JSONResponse from typing import List - import yaml -import json +import uvicorn + +from importlib import resources as impresources +from . import assets -default_headers = {"API-Version": "2.0.0"} + +default_headers = {"API-Version": "2.0.1"} app = FastAPI(docs_url="/api") @@ -83,8 +85,12 @@ async def transform(): def custom_openapi(): - with open("openapi.yaml", "rb") as openapi: - return yaml.load(openapi, yaml.SafeLoader) + oas_file_resource = (impresources.files(assets) / "openapi.yaml") + with oas_file_resource.open("rb") as oas_file: + return yaml.load(oas_file, yaml.SafeLoader) app.openapi = custom_openapi + +def start(): + uvicorn.run("coordinates_transformation_api.main:app", workers=2, host="0.0.0.0", port=8000, reload=True) diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..4f0e29d --- /dev/null +++ b/mypy.ini @@ -0,0 +1,3 @@ +[mypy] +warn_return_any = True +warn_unused_configs = True diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..1668147 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,42 @@ +[project] +name = "coordinates-transformation-api" +description = "Coordinates transformation API of the Nederlandse Samenwerking Geodetische Infrastructuur (NSGI)" +keywords = ["NSGI", "pyproj", "fastapi", "coordinates", "geo"] +license = {text = "MIT"} +classifiers = [ + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.11", +] +readme = { file = "./README.md", content-type = "text/markdown" } +dependencies = [ + "fastapi == 0.100.1", + "uvicorn == 0.23.2", + "pyyaml == 5.3.1", +] +requires-python = ">=3.11.4" +dynamic = ["version"] + +[project.optional-dependencies] +dev = ["black", "mypy", "autoflake", "isort"] + +[build-system] +build-backend = "setuptools.build_meta" +requires = [ + "setuptools>=45", + "setuptools-git-versioning==1.13.1", + "wheel==0.38.4" +] + +[project.scripts] +ct-api = "coordinates_transformation_api.main:start" + +[tool.setuptools-git-versioning] +enabled = true + +[tool.setuptools.packages.find] +include = ["coordinates_transformation_api*"] # package names should match these glob patterns (["*"] by default) +exclude = [] # exclude packages matching these glob patterns (empty by default) + +[tool.setuptools.package-data] +"coordinates_transformation_api.assets" = ["*"] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6068493 --- /dev/null +++ b/setup.py @@ -0,0 +1,3 @@ +from setuptools import setup + +setup()