Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • F FastAPI
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • Sitemule
  • Public
  • workshops
  • open source on IBM i
  • FastAPI
  • Wiki
  • Part 2b. Organise my first service

Part 2b. Organise my first service · Changes

Page history
Update Part 2b. Organise my first service authored Oct 30, 2021 by Jens Melgard Churchill's avatar Jens Melgard Churchill
Hide whitespace changes
Inline Side-by-side
Part-2b.-Organise-my-first-service.md
View page @ 640f95a2
# Since we care about maintainability, lets do a bit of housework
\ No newline at end of file
# Its time we gave this service a face
While FastAPI is brilliant at building API's, it is also able to build self-contained micro-services that incorporate more than just data and business logic, it can exist as a sort of micro-site in it self.
## Content + Style == Success
Usually, the frontpage of a site (index.html), doesn't return JSON data.
So lets change that first.
Remove the function `read_root()` and its body, the `return {"Hello": "World"}`
Replace it with the following code
```python
@app.get("/")
async def index():
content: str = """
<html>
<body>
<h1>Hello world</1>
</body>
</html>
"""
return HTMLResponse(content)
```
And add the following import to the top `from fastapi.responses import HTMLResponse`
There are many response classes in FastAPI, and in this case we make use of its HTML response.
You know the drill, save `:w` and navigate to your services frontpage: [fastapi.ibmi.rocks](https://fastapi.ibmi.rocks)
## Since we care about maintainability, lets do a bit of housework
First, API's can grow large containing many endpoints, having them all in one file is silly.
A bit of refactoring can go a long way, but
\ No newline at end of file
Clone repository
  • Part 1a. My first service
  • Part 1b. Testing my first service
  • Part 2a. Extending my first service
  • Part 2b. Organise my first service
  • Part 3a. Review my first service
  • Part 3b. Connecting my first service
  • Home