From bf6c81e157a45923e0d0eef21898383535d5ee61 Mon Sep 17 00:00:00 2001 From: maride Date: Mon, 22 Sep 2025 16:03:48 +0200 Subject: [PATCH] Init --- Dockerfile | 20 ++++++++++++++++++++ README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ main.py | 43 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 4 files changed, 111 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 main.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..634867c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Python image from the Docker Hub +FROM python:3.11-slim + +# Set the working directory +WORKDIR /app + +# Copy the requirements file into the container +COPY requirements.txt . + +# Install the dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code +COPY . . + +# Expose the port the app runs on +EXPOSE 8000 + +# Command to run the application +CMD ["python", "main.py"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..eaa7d73 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# FastAPI DateTime Service + +This is a simple FastAPI application that provides an OpenAPI-compatible `/datetime` endpoint returning the current date, time, weekday, and calendar week. + +## Requirements + +- Python 3.7+ +- FastAPI +- Uvicorn + +```bash +pip install -r requirements.txt +``` + +## Running the Application + +To start the FastAPI server, run: + +```bash +python main.py +``` + +The server will be available at `http://localhost:8000`. + +## Endpoints + +### `GET /datetime` + +Returns current date, time, weekday, and calendar week. + +**Response Format:** + +```json +{ + "date": "2025-09-22", + "time": "14:30:45", + "weekday": "Monday", + "calendar_week": 38 +} +``` + +**Response Fields:** +- `date` (string): Current date in YYYY-MM-DD format +- `time` (string): Current time in HH:MM:SS format +- `weekday` (string): Full weekday name (e.g., "Monday") +- `calendar_week` (integer): ISO calendar week number diff --git a/main.py b/main.py new file mode 100644 index 0000000..2417821 --- /dev/null +++ b/main.py @@ -0,0 +1,43 @@ +from fastapi import FastAPI +from datetime import datetime +import calendar +from typing import Any + +app = FastAPI() + +@app.get( + "/datetime", + summary="Get Current Date and Time Information", + description="""Returns the current date, time, weekday, and calendar week. +This endpoint provides time-related information in a structured JSON format +that can be used for various applications including logging, timestamps, +and scheduling. + +**Response Fields:** +- `date` (string): Current date in YYYY-MM-DD format +- `time` (string): Current time in HH:MM:SS format +- `weekday` (string): Full weekday name (e.g., "Monday") +- `calendar_week` (integer): ISO calendar week number +""", + tags=["datetime"], + response_model=dict, + response_description="Current date and time information" +) +async def get_datetime() -> dict[str, Any]: + """ + Get current date and time information. + + Returns: + dict: A dictionary containing current date, time, weekday, and calendar week + """ + now = datetime.now() + return { + "date": now.strftime("%Y-%m-%d"), + "time": now.strftime("%H:%M:%S"), + "weekday": now.strftime("%A"), + "calendar_week": now.isocalendar().week + } + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..97dc7cd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi +uvicorn