Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up docker build #6

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.coverage
venv
.pytest_cache
.github
.coverage
.idea
.env
.git
.DS_Store
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use an official Python runtime as a parent image
FROM python:3.10

# Set the working directory in the container
WORKDIR /app

# Copy the current requirements into the container
COPY requirements.txt /app/requirements.txt

# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents into the container at /app
COPY . /app

# Make port 8080 available to the world outside this container
EXPOSE 8003

# Run app.py when the container launches
CMD ["fastapi", "dev", "main.py", "--host", "0.0.0.0", "--port", "8003"]

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ This project has been developed to test the GitHub Actions as part of a CI/DC wo
```sh
pytest --cov --cov-report term-missing
```


## Starting the App

Save the [dotenv](dotenv) file as '.env' and set the desired `PORT` and `HOST` variables.

Run the app
```commandline
fastapi dev main.py
```

2 changes: 2 additions & 0 deletions dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=<PORT_NUMBER>
HOST=<HOST_IP>
39 changes: 36 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
#!/usr/bin/python
import os

import uvicorn
from src.routers import function
from fastapi import FastAPI
from contextlib import asynccontextmanager
from dotenv import load_dotenv

app = FastAPI()

# Check if the file exists
if os.path.exists('.env'):
print("Loading .env file")
load_dotenv('.env')
else:
print("No .env file defined")

HOST: str = os.getenv('HOST')
PORT: int = None
if os.getenv('PORT'):
PORT = int(os.getenv('PORT'))


@asynccontextmanager
async def lifespan(fastapi_app: FastAPI):
# this code runs before the app starts
print("* Lifespan: App started", fastapi_app)
print("\t - HOST =", HOST)
print("\t - PORT =", PORT)
yield
# this code runs after the app finishes
print("* Lifespan: App stopped")


app = FastAPI(lifespan=lifespan)
app.include_router(function.router)


@app.get("/")
def read_root():
return {"message": "Hello World"}
def root():
return {"message": "Hello World!"}


if __name__ == "__main__":
print("\t - HOST =", HOST)
print("\t - PORT =", PORT)
uvicorn.run(app, host=HOST, port=PORT)
2 changes: 1 addition & 1 deletion test/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
assert response.json() == {"message": "Hello World!"}
Loading