Skip to content

Commit

Permalink
Wrap up "app from scratch" example.
Browse files Browse the repository at this point in the history
  • Loading branch information
aholmes committed Nov 19, 2024
1 parent a08b3d8 commit f41446f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Adding an API endpoint to a `FlaskApp` application requires two things:
The Python functions are regular functions that you're already familiar with.
The specification is an `OpenAPI specification <https://swagger.io/specification/>`_ written in a YAML file.

Modifying the Existing Application
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

First, let's modify the application config file ``app/config.toml``. Add the ``flask.openapi`` section so your file looks like this.

.. code-block:: toml
Expand All @@ -23,17 +26,99 @@ First, let's modify the application config file ``app/config.toml``. Add the ``f
[flask.openapi]
spec_path = 'openapi.yaml'
This change allows us to use an OpenAPI specification file to control details of the API endpoint.

We also need to modify the ``[flask]`` section, and add a ``[logging]`` section.

Your file will look like this.

.. code-block:: toml
[flask]
app_name = 'app'
host = 'localhost'
port = '5000'
[flask.openapi]
spec_path = 'openapi.yaml'
[logging]
format = 'plaintext'
Finally, let's make a few modifications to ``app/__init__.py``.
This ensures the ``localhost`` and ``port`` options in ``config.toml`` are used.

.. code-block:: python
from Ligare.web.config import Config
from Ligare.web.config import FlaskConfig
application_builder = (
ApplicationBuilder[FlaskApp]()
application_builder = ApplicationBuilder[FlaskApp]() \
.use_configuration(
lambda config_builder: config_builder
.with_config_filename("app/config.toml")
.with_root_config_type(Config)
lambda config_builder: \
config_builder.with_config_filename("app/config.toml")
)
)
result = application_builder.build()
app = result.app_injector.app
injector = result.app_injector.flask_injector.injector
config = injector.get(FlaskConfig)
app.run(host=config.host, port=int(config.port))
Adding Endpoints
^^^^^^^^^^^^^^^^

With these preliminary adjustments completed, we can add a new endpoint.

We're going to add an endpoint to handle requests made to the root URL, ``/``, of the application.
This means accessing http://localhost:5000 will show the "Hello, World!" message we're expecting.

The ``openapi.yaml`` File
~~~~~~~~~~~~~~~~~~~~~~~~~

Create a new file at ``app/openapi.yaml`` and add this content.

.. code-block:: yaml
info:
title: Test Application
version: 3.0.3
openapi: 3.0.3
paths:
/:
get:
description: Say "Hello, World!"
operationId: root.get
responses:
"200":
content:
application/json:
schema:
type: string
description: Said "Hello, World!" successfully
This OpenAPI specification states that there is an HTTP GET endpoint at
``/`` that returns an HTTP 200 status, and a JSON string for its content.

Pay special attention to the ``operationId`` property. This tells Connexion
that it can find a function named ``get`` in the Python module named ``root``.

The Endpoint Handler
~~~~~~~~~~~~~~~~~~~~

Finally, let's create the Python module and function that will handle requests
for this endpoint.

Create the file ``app/root.py`` and add this to it.

.. code-block:: python
def get():
return "Hello, World!"
The function named ``get`` is the same one specified for the ``operationId`` property.
This is all you need to do to return data through your API endpoint!

Now you can start the application with ``python app/__init__.py``. Once it's running,
open http://localhost:5000/ and you should see "Hello, World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. toctree::
:maxdepth: 2
:caption: Contents:

Additional Features
-------------------

Swagger UI
^^^^^^^^^^

``Ligare.web`` ships with `Swagger UI <https://swagger.io/tools/swagger-ui/>`_, which makes testing your API simple.

When your application is running you can access Swagger UI at http://localhost:5000/ui by default.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Create an Application from Scratch
==================================

Let's create a web application that shows "Hello World!"
Let's create a web application that shows "Hello, World!"

We will create this application from scratch, building each piece necessary for ``Ligare.web`` to function.

Expand All @@ -12,3 +12,4 @@ We will create this application from scratch, building each piece necessary for
setting-up-your-project
creating-the-application
adding-api-endpoints
additional-features

0 comments on commit f41446f

Please sign in to comment.