Skip to content

Commit

Permalink
Merge branch 'main' into feature/status
Browse files Browse the repository at this point in the history
  • Loading branch information
Vianpyro authored Nov 12, 2024
2 parents 14bc8df + 1f2e7ce commit afc4c29
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ jobs:
# To report GitHub Actions status checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_ALL_CODEBASE: false
VALIDATE_JSON_PRETTIER: false
VALIDATE_PYTHON_PYLINT: false
VALIDATE_PYTHON_ISORT: false
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"isort.args": [
"--profile",
"black"
]
],
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# 0 C.E. API

This is the backend API for the **0 C.E.** game, providing essential services and data to support game functionality. The API is currently in development and will expand with more endpoints and features over time.

## Table of Contents

- [Overview](#overview)
- [Getting Started](#getting-started)
- [Endpoints](#endpoints)
- [Response Structure](#response-structure)
- [Running in Docker](#running-in-docker)
- [Future Development](#future-development)

## Overview

The 0 C.E. API is built with Flask and uses JSON for data communication. Initially, it provides a basic endpoint for retrieving the server time, but it will expand as more game functionality is implemented.

## Getting Started

To run the API locally, you’ll need:

- **Docker** to build and run the application in a container.

### Setup Steps

1. **Build the Docker Image**:
```bash
docker build -t 0ce-flask-api .
```
2. **Run the Docker Container**:
```bash
docker run -d -p 5000:5000 --name 0CE_API 0ce-flask-api
```
3. **Access the API**:
Open your browser or API client to `http://localhost:5000`.

## Endpoints

### 1. `/time` - Retrieve Server Time

- **Description**: This endpoint returns the current server time.
- **Method**: `GET`
- **URL**: `/time`
- **Response**:

```json
{
"status": "success",
"data": {
"server_time": "2023-12-01T12:34:56Z"
}
}
```

**Sample Response (Error)**:

```json
{
"status": "error",
"message": "Resource not found"
}
```

**Note**: Additional endpoints will be added as the game develops.

## Response Structure

To maintain consistency across responses, all data responses will follow a standard JSON format:

- **Success**:
```json
{
"status": "success",
"data": { ... }
}
```
- **Error**:
```json
{
"status": "error",
"message": "Error message here"
}
```

This structure allows easy parsing of successful responses and clear indication of errors.

## Running in Docker

The API is designed to run within a Docker container. Here’s a breakdown of the Dockerfile configuration and commands for running the container.

### Dockerfile Configuration

The [Dockerfile](Dockerfile) uses the official `python:3.13-slim` image and installs dependencies from `requirements.txt`. A health check verifies the container’s status every 30 seconds.

### Running the Container

To run the container, expose it on port 5000:

```bash
docker run -p 5000:5000 0ce-flask-api
```

Access the API via `http://localhost:5000`.

## Future Development

Planned API enhancements:

- **User Authentication**: Implementing a secure user authentication system.
- **Additional Endpoints**: To support game features like city management, alliances, resources, and combat.
- **Error Handling and Logging**: Structured error codes and enhanced logging for debugging and monitoring.
- **API Versioning**: To support backward compatibility in future versions.
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ def add_status(response):
response.set_data(jsonify(original_data).data)
return response


if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
18 changes: 15 additions & 3 deletions tests/test_get_time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import re
from unittest.mock import patch

import pytest

Expand All @@ -10,7 +11,18 @@ def client():
return app.test_client()


def test_get_time(client):
@pytest.mark.parametrize(
"fixed_time", ["12:34:56 01/01/2024", "23:59:59 31/12/2023", "00:00:00 01/01/2025"]
)
def test_get_time(client, fixed_time):
with patch("time.strftime", return_value=fixed_time):
response = client.get("/time")
assert response.status_code == 200
assert response.json == {"server_time": fixed_time}


def test_time_format(client):
response = client.get("/time")
assert response.status_code == 200
assert response.json == {"server_time": time.strftime("%H:%M:%S %d/%m/%Y")}
time_str = response.json.get("server_time")
assert re.match(r"\d{2}:\d{2}:\d{2} \d{2}/\d{2}/\d{4}", time_str)

0 comments on commit afc4c29

Please sign in to comment.