pyronaut create example.micronaut.micronautguide
Creating your first Pyronaut application
Learn how to create a Hello World Pyronaut application with a controller and a functional test.
1. Getting Started
In this guide, we will create a Python application built with Pyronaut.
2. What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
GraalPy installed and the Pyronaut CLI available locally
3. Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
4. Writing the Application
Create an application using the Pyronaut CLI (Command Line Interface) or Pyronaut Launch
The HTTP endpoint below is an ordinary Python function. Micronaut’s decorators define its route and response content type; a controller class is not required.
4.1. Controller
To create a microservice that responds with "Hello World", you first need a controller.
from micronaut.http import MediaType
from micronaut.http.annotation import Get
from micronaut.http.annotation import Produces
@Get("/hello") (1)
@Produces(MediaType.TEXT_PLAIN) (2)
def index() -> str:
return "Hello World" (3)
| 1 | The @Get annotation maps the index method to an HTTP GET request on /hello. |
| 2 | By default, a Micronaut response uses application/json as Content-Type. We are returning a String, not a JSON object, so we set it to text/plain with the @Produces annotation. |
| 3 | A String "Hello World" is returned as the result. |
4.2. Test
Create a test to verify that when you make a GET request to /hello you get Hello World as a response:
import pytest
import requests
from pyronaut.test import MicronautTest, micronaut_test_fixture
@pytest.fixture
def my_context(request):
fixture = micronaut_test_fixture(
request,
MicronautTest(environments=["test"], transactional=False),
) (1)
yield fixture
fixture.stop()
@pytest.fixture
def client(my_context):
with requests.with_context(my_context) as session:
yield session
def test_hello_world_response(client):
response = client.get("/hello") (2)
assert response.status_code == 200
assert response.headers["content-type"].startswith("text/plain")
assert response.text == "Hello World" (3)
| 1 | Start the application for the pytest fixture. |
| 2 | Send an HTTP request with the requests library. |
| 3 | Verify the response body. |
requests.with_context connects the test client to the application’s assigned port, so tests do not need to construct a server URL or access the embedded server directly.
5. Testing the Application
To run the tests:
pyronaut install
pyronaut validate-config
pyronaut test
6. Running the Application
To run the application, use the pyronaut dev command, which starts the application on port 8080.
With the application running, call the endpoint:
curl localhost:8080/hello
The response is Hello World.
6.1. Logging
Pyronaut configures logging in src/main.py:
from logback.config import dictConfig
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"colored": {
"format": "%cyan(%d{HH:mm:ss.SSS}) %highlight(%-5level) %magenta(%logger{36}): %msg"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "colored",
"stream": "ext://sys.stdout"
}
},
"root": {
"level": "INFO",
"handlers": ["console"]
}
}
dictConfig(LOGGING)
7. License
| All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |