Skip to content

Commit

Permalink
Add redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Михаил Исаев committed Jun 30, 2024
1 parent 7826459 commit 366f4e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
- [Configuration](#configuration)
- [config.yml or env variables](#configyml-or-env-variables)
- [Endpoints](#endpoints)
- [Endpoint `/`](#endpoint-)
- [Endpoint `/:ip`](#endpoint-ip)
- [Endpoint `/api/v1/ip`](#endpoint-apiv1ip)
- [Endpoint `/api/v1/ip/{ip}`](#endpoint-apiv1ipip)
- [Respone format](#respone-format)
- [Example](#example)
- [Example - Correct response](#example---correct-response)
Expand All @@ -26,13 +26,13 @@ asndb: geoip/GeoLite2-ASN.mmdb # env var: GEOIP_ASN, path to ASN database
## Endpoints
### Endpoint `/`
### Endpoint `/api/v1/ip`

Information about client IP, see [Example - Correct response](#example---correct-response)

### Endpoint `/:ip`
### Endpoint `/api/v1/ip/{ip}`

Information about passed IP, in format `/128.128.128.128` or `2001:2001::2001` see [Example - Correct response](#example---correct-response)
Information about passed IP, in format `128.128.128.128` or `2001:2001::2001` see [Example - Correct response](#example---correct-response)

If passed IP is invalid - returns 400, see [Example - Invalid IP response](#example---invalid-ip-response)

Expand Down
19 changes: 18 additions & 1 deletion pyip/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from fastapi import FastAPI, Request
from fastapi.exceptions import HTTPException
from fastapi.responses import RedirectResponse
from .settings import settings
from .models import IpResponse
from pydantic import IPvAnyAddress
Expand All @@ -13,14 +15,29 @@
reader = GeoIP_Reader(city_db=settings.geoip_city, asn_db=settings.geoip_asn)


@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
raise HTTPException(status_code=404, detail="Not found")


@app.get("/")
async def get_root_redir(request: Request) -> RedirectResponse:
return RedirectResponse(url="/api/v1/ip")


@app.get("/{ip}")
async def get_ip_provided_redir(ip: IPvAnyAddress) -> RedirectResponse:
return RedirectResponse(url=f"/api/v1/ip/{ip}")


@app.get("/api/v1/ip")
async def get_ip(request: Request) -> IpResponse:
ip: IPvAnyAddress = ip_address(request.client.host)
ip_response: IpResponse = await reader.get_ip_info(ip)
return ip_response


@app.get("/{ip}")
@app.get("/api/v1/ip/{ip}")
async def get_ip_provided(ip: IPvAnyAddress) -> IpResponse:
with GeoIP_Reader(city_db=settings.geoip_city, asn_db=settings.geoip_asn) as reader:
ip_response: IpResponse = await reader.get_ip_info(ip)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyip"
version = "0.2.1"
version = "0.2.2"
description = ""
authors = ["Your Name <you@example.com>"]
license = "WTFPL"
Expand Down

0 comments on commit 366f4e0

Please sign in to comment.