Skip to content

Commit

Permalink
add, feat: add docker-compose file, add description to advanced search
Browse files Browse the repository at this point in the history
  • Loading branch information
r3tr0ananas committed Jul 12, 2024
1 parent eb05858 commit 9f62806
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 21 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ This is an API for anime-girls-and-computers [github repo](https://github.com/TH
How to host your own AGAC API instance

### 🐬 Docker Method (recommended)
Coming Soon™
1. Pull the image
```sh
docker pull r3tr0ananas/agac-api:latest
```
2. Then launch a container with this command.
> *you don't really need to mount a volume but it's recommended*
```sh
docker run -p 8000:8000/tcp -v ./cached_images:/app/assets/cache r3tr0ananas/agac-api:latest
```
3. Now visit ``localhost:8000`` in your browser and there you go! 👍
> *if you wanna use docker-compose, [this file](./docker-compose.yml) might be useful to you*
### 🐍 Native Method (recommended for development)

Expand All @@ -41,8 +51,8 @@ make
```sh
make get-repo
```
5. Run that sh#t.
5. Run.
```sh
make run
```
6. Visit ``localhost:8083`` in your browser, then all should be good! 🌈
6. Visit ``localhost:8083`` in your browser, then all should be good! 🌈
2 changes: 1 addition & 1 deletion api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.2"
__version__ = "1.1.3"
17 changes: 16 additions & 1 deletion api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,27 @@ class ImageMetadataNotFound(BaseModel):
}
}

class RateLimited(BaseModel):
error: str
message: str

model_config = {
"json_schema_extra": {
"examples": [
{
"error": "RateLimited",
"message": "Rate limit exceeded: 3 per 1 second (Follow the rates: https://github.com/r3tr0ananas/agac-api/wiki#-rate-limiting)"
}
]
}
}

def rate_limit_handler(request: Request, exc: RateLimitExceeded):
response = JSONResponse(
status_code = 429,
content = {
"error": "RateLimited",
"message": f"Rate limit exceeded: {exc.detail} (Follow the rates: https://github.com/r3tr0ananas/agac-api/wiki#rate-limiting)"
"message": f"Rate limit exceeded: {exc.detail} (Follow the rates: https://github.com/r3tr0ananas/agac-api/wiki#-rate-limiting)"
}
)

Expand Down
64 changes: 48 additions & 16 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,43 @@

ROOT_PATH = (lambda x: x if x is not None else "")(environ.get("ROOT_PATH"))

TAGS_METADATA = [
{
"name": "image",
"description": "The main endpoints that allow you to get images."
},
{
"name": "other",
"description": "Other endpoints."
}
]

DESCRIPTION = """
<div align="center">
<img src="https://api.ananas.moe/agac/v1/get/miyako_shikimori_fedora" width="600">
The **anime-girls-and-computers** API!
This is an API for anime-girls-and-computers [github repo](https://github.com/THEGOLDENPRO/anime-girls-and-computers).
Report bugs [over here](https://github.com/r3tr0ananas/agac-api/issues).
</div>
Rate limiting applies to the ``/random`` and ``/get`` endpoints. Check out the rate limits [over here](https://github.com/r3tr0ananas/agac-api/wiki#-rate-limiting).
"""


limiter = Limiter(key_func=get_remote_address, headers_enabled = True)
app = FastAPI(
title = "AGAC-API",
description = "",
description = DESCRIPTION,
license_info = {
"name": "MIT",
"name": "License: MIT",
"identifier": "MIT",
},
},
version = f"v{__version__}",

root_path = ROOT_PATH
)
app.state.limiter = limiter
Expand All @@ -38,9 +65,9 @@

@app.get(
"/",
tags = ["misc"]
tags = ["other"]
)
async def root(request: Request):
async def root():
return RedirectResponse(f"{ROOT_PATH}/docs")

@app.get(
Expand All @@ -49,7 +76,7 @@ async def root(request: Request):
tags = ["other"]
)
async def info():
"""Returns repository information like image count and etc."""
"""Returns repository information like image count and etc."""
return {
"version": __version__,
"image_count": len(agac.images)
Expand All @@ -67,7 +94,7 @@ async def info():
}
},
)
async def all(request: Request):
async def all():
return [
image.to_dict() for image in agac.images
]
Expand All @@ -89,6 +116,10 @@ async def all(request: Request):
"model": errors.ImageNotFound,
"description": "The image was not Found."
},
429: {
"model": errors.RateLimited,
"description": "Rate Limit exceeded"
}
},
)
@limiter.limit(f"{RATE_LIMIT}/second")
Expand Down Expand Up @@ -122,7 +153,7 @@ async def get(request: Request, id: str, raw: bool = False):
},
},
)
async def get_metadata(request: Request, id: str):
async def get_metadata(id: str):
image = agac.get(id)

if image is not None:
Expand All @@ -140,6 +171,7 @@ async def get_metadata(request: Request, id: str):
"/random",
name = "Get a random image",
tags = ["image"],
description = "To retrieve metadata for a random image, check the `x-image-id` header for the search ID.",
response_class = FileResponse,
responses = {
200: {
Expand All @@ -149,6 +181,10 @@ async def get_metadata(request: Request, id: str):
},
"description": "Returned an image successfully. 😁",
},
429: {
"model": errors.RateLimited,
"description": "Rate Limit exceeded"
}
},
)
@limiter.limit(f"{RATE_LIMIT}/second")
Expand All @@ -170,7 +206,6 @@ async def random_image(request: Request, category: str = None, raw: bool = False
},
)
async def search(
request: Request,
query: str,
category: str = None,
limit: int = 10
Expand Down Expand Up @@ -199,7 +234,7 @@ async def search(
"/search/advanced",
name = "Advanced Search for images.",
tags = ["image"],
description = "You add multiple tags by adding \",\" after each tag",
description = "You can add multiple tags by adding \",\" after each tag",
response_class = JSONResponse,
responses = {
200: {
Expand All @@ -209,7 +244,6 @@ async def search(
},
)
async def search_advanced(
request: Request,
tags: str = "",
author: str = None,
limit: int = 10
Expand Down Expand Up @@ -247,7 +281,5 @@ async def search_advanced(
},
},
)
async def categories(request: Request):
return list(agac.categories.keys())


async def categories():
return list(agac.categories.keys())
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
agac-api:
image: r3tr0ananas/agac-api:latest
volumes:
- ./cached_images:/app/assets/cache
environment:
ROOT_PATH: ""
restart: unless-stopped

0 comments on commit 9f62806

Please sign in to comment.