Skip to content

Commit a87972e

Browse files
Merge pull request #1 from devwithkrishna/feature/v1
Example python app with fast api
2 parents 6b86466 + 44296bd commit a87972e

File tree

8 files changed

+229
-14
lines changed

8 files changed

+229
-14
lines changed

.github/dependabot.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "pip"
4+
directory: /
5+
schedule:
6+
interval: "weekly"
7+
day: wednesday
8+
time: "11:30"
9+
timezone: Asia/Kolkata
10+
# Assignees to set on pull requests
11+
assignees:
12+
- "githubofkrishnadhas"
13+
# prefix specifies a prefix for all commit messages. When you specify a prefix for commit messages,
14+
# GitHub will automatically add a colon between the defined prefix and the commit message provided the
15+
# defined prefix ends with a letter, number, closing parenthesis, or closing bracket.
16+
commit-message:
17+
prefix: "dependabot python package"
18+
# Use reviewers to specify individual reviewers or teams of reviewers for all pull requests raised for a package manager.
19+
reviewers:
20+
- "devwithkrishna/admin"
21+
# Raise pull requests for version updates to pip against the `main` branch
22+
target-branch: "main"
23+
# Labels on pull requests for version updates only
24+
labels:
25+
- "pip"
26+
- "dependencies"
27+
# Increase the version requirements for Composer only when required
28+
versioning-strategy: increase-if-necessary
29+
# Dependabot opens a maximum of five pull requests for version updates. Once there are five open pull requests from Dependabot,
30+
# Dependabot will not open any new requests until some of those open requests are merged or closed.
31+
# Use open-pull-requests-limit to change this limit.
32+
open-pull-requests-limit: 10

.github/workflows/release.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: create release on example python application
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
branches:
8+
- main
9+
run-name: create release from pr number ${{ github.event.number }}
10+
jobs:
11+
create-release:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
16+
- name: Token generator
17+
uses: githubofkrishnadhas/github-access-using-githubapp@v2
18+
id: token-generation
19+
with:
20+
github_app_id: ${{ secrets.TOKEN_GENERATOR_APPID }}
21+
github_app_private_key: ${{ secrets.TOKEN_GENERATOR_PRIVATE_KEY }}
22+
23+
- name: Checkout Repository
24+
uses: actions/checkout@v4
25+
with:
26+
token: ${{ steps.token-generation.outputs.token }}
27+
28+
- name: create-release
29+
uses: devwithkrishna/[email protected]
30+
with:
31+
token: ${{ steps.token-generation.outputs.token }}
32+
pr_number: ${{ github.event.number }}
33+
generate_release_notes: true

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Use the official Python image as the base image
2+
FROM python:3.11-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy the Poetry configuration files to the container
8+
COPY pyproject.toml /app/
9+
10+
# Copy the README.md file to the container
11+
COPY README.md /app/
12+
13+
# Install Poetry
14+
RUN pip install poetry
15+
16+
# Install the application dependencies using Poetry
17+
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi --no-root
18+
19+
# Copy the application code to the container
20+
COPY app /app/app
21+
22+
# Expose the port that the FastAPI application will run on
23+
EXPOSE 8000
24+
25+
# Command to run the application
26+
CMD ["poetry", "run", "uvicorn", "app.quickapi:app", "--host", "0.0.0.0", "--port", "8000"]

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
# example-python-application
2-
Sample python application
1+
# Example Python Application
2+
3+
This is a sample Python application built using **FastAPI**. The application allows users to submit their name and favorite color, and provides endpoints to retrieve the submitted data. It also includes logging functionality with JSON formatting.
4+
5+
## Features
6+
7+
- **FastAPI** for building RESTful APIs.
8+
- **Pydantic** for data validation and serialization.
9+
- **Logging** with JSON formatting using `python-json-logger`.
10+
- In-memory storage for demonstration purposes.
11+
12+
## Endpoints
13+
14+
### Root Endpoint
15+
- **GET** `/`
16+
- Returns a welcome message.
17+
18+
### Healthcheck
19+
- **GET** `/healthcheck`
20+
- Verifies if the service is running.
21+
22+
### Submit User Color
23+
- **POST** `/usercolour/`
24+
- Accepts a user's name and favorite color.
25+
- **Request Body**:
26+
```json
27+
{
28+
"name": "John Doe",
29+
"favorite_color": "Blue"
30+
}
31+
```
32+
- **Response**:
33+
```json
34+
{
35+
"name": "John Doe",
36+
"favorite_color": "Blue"
37+
}
38+
```
39+
40+
### Get All User Colors
41+
- **GET** `/allusercolour/`
42+
- Returns all submitted user-color mappings.
43+
44+
## Installation
45+
46+
* Clone the repository:
47+
```bash
48+
git clone https://github.com/githubofkrishnadhas/example-python-application.git
49+
cd example-python-application
50+
```
51+
52+
* Install dependencies using Poetry:
53+
54+
```
55+
poetry install
56+
```
57+
58+
Run the application:
59+
60+
```
61+
poetry run uvicorn app.quickapi:app --reload
62+
```
63+
Access the API documentation at http://127.0.0.1:8000/docs.
64+
65+
## Project Structure
66+
```yaml
67+
example-python-application/
68+
├── app/
69+
│ ├── data_model.py # Defines the Pydantic models
70+
│ ├── logging_config.py # Configures logging for the application
71+
│ ├── quickapi.py # Main FastAPI application
72+
├── README.md # Project documentation
73+
├── pyproject.toml # Poetry configuration
74+
```
75+
76+
## Dependencies
77+
* Python: 3.11+
78+
* FastAPI: For building APIs.
79+
* Uvicorn: ASGI server for running the application.
80+
* Pydantic: Data validation and settings management.
81+
* python-json-logger: JSON formatting for logs.

app/data_model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
This module defines the data models used in the application.
3+
4+
Classes:
5+
UserColorEntry: A Pydantic model representing a user's name and their favorite color.
6+
"""
7+
8+
from pydantic import BaseModel
9+
10+
class UserColorEntry(BaseModel):
11+
"""
12+
Represents a user's name and their favorite color.
13+
14+
Attributes:
15+
name (str): The name of the user.
16+
favorite_color (str): The user's favorite color.
17+
"""
18+
name: str
19+
favorite_color: str

app/main.py

Whitespace-only changes.

app/quickapi.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fastapi import FastAPI
2-
# from pylogs import get_logger
3-
from logging_config import setup_logging
2+
from app.logging_config import setup_logging
3+
from app.data_model import UserColorEntry
44

55
# Setup logging
66
logger = setup_logging()
@@ -15,11 +15,13 @@
1515
description="Submit your name and favorite color",
1616
version="1.0.0"
1717
)
18+
# In-memory database (for demonstration purposes)
19+
user_colour = []
1820

1921
@app.get("/")
2022
async def root():
23+
"""Default end point.Serves welcome message for the API"""
2124
logger.info("Root endpoint accessed")
22-
# logger = logging.getLogger("myapp")
2325
logger.info("Root endpoint was accessed!")
2426

2527
return {"message": "Hello World"}
@@ -32,12 +34,19 @@ async def healthcheck():
3234
logger.info("Healthcheck endpoint accessed")
3335
return {"status": "ok"}
3436

35-
# @app.middleware("http")
36-
# async def log_requests(request: Request, call_next):
37-
# logger = logging.getLogger("myapp")
38-
# logger.info(f"Received request: {request.method} {request.url}")
39-
# response = await call_next(request)
40-
# logger.info(f"Response status: {response.status_code}")
41-
# return response
42-
43-
37+
# Create an user_colour mapping
38+
@app.post("/usercolour/", response_model=UserColorEntry)
39+
async def create_item(item: UserColorEntry):
40+
"""Create an item with a username and users favourite colour and return it."""
41+
user_colour.append(item)
42+
print(user_colour)
43+
logger.info(item)
44+
return item
45+
46+
# List all user_colour mappings
47+
@app.get("/allusercolour/")
48+
async def get_all_user_colour():
49+
"""Get all user_colour mappings."""
50+
logger.info("All user_colour mappings accessed")
51+
logger.info(user_colour)
52+
return user_colour

pyproject.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[tool.poetry]
2+
name = "example-python-application"
3+
version = "0.1.0"
4+
description = "Sample Python Application"
5+
authors = ["githubofkrishnadhas <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.11"
10+
fastapi = {extras = ["standard"], version = "*"}
11+
uvicorn = "*"
12+
python-json-logger = "*"
13+
pydantic = "^2.11.3"
14+
15+
[build-system]
16+
requires = ["poetry-core"]
17+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)