Skip to content

Commit

Permalink
Fix redis issues
Browse files Browse the repository at this point in the history
  • Loading branch information
XboxBedrock committed Jul 3, 2024
1 parent 4d4ea12 commit e588bcd
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "-m", "hypercorn", "shorter:app", "--bind", "0.0.0.0:8000", "--access-logfile", "-" ]
CMD [ "python", "-m", "hypercorn", "shorter:app", "--bind", "0.0.0.0:8000", "--access-logfile", "-" ]
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
aioredis==1.3.1
redis>=4.2.0rc1
black==20.8b1
Cerberus==1.3.2
Hypercorn==0.11.1
Hypercorn==0.17.3
python-dotenv==0.15.0
Quart==0.13.1
Quart-CORS==0.3.0
Quart-Rate-Limiter==0.4.0
Jinja2<=3.0.3
werkzeug==2.1.2
Binary file added shorter/.DS_Store
Binary file not shown.
12 changes: 4 additions & 8 deletions shorter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import asyncio
import os

import aioredis
from redis import asyncio as aioredis
from quart import Quart, exceptions
from quart_cors import cors

import shorter.bp.shortener
from shorter import config
from shorter.errors import ApiError
from shorter.ratelimit import rate_limiter

app = Quart(__name__)
app = cors(app, expose_headers=["Location"])

rate_limiter.init_app(app)


app.register_blueprint(shorter.bp.shortener.bp)


@app.before_serving
async def before_serving():
loop = asyncio.get_event_loop()

app.redis = await aioredis.create_redis_pool(
cpool = aioredis.ConnectionPool.from_url(
config.redis_address,
loop=loop,
encoding="utf-8",
)

app.redis = aioredis.Redis.from_pool(cpool)


@app.errorhandler(ApiError)
def handle_api_error(error: ApiError):
Expand Down
2 changes: 1 addition & 1 deletion shorter/bp/shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def create():
shortname = await generate_shortname(8)
key = f"{current_app.import_name}-shorten-{shortname}"
await current_app.redis.set(key, url)
await current_app.redis.expire(key, ttl)
await current_app.redis.expire(key, int(ttl))

url = urlunparse(
(
Expand Down
43 changes: 0 additions & 43 deletions shorter/ratelimit.py

This file was deleted.

0 comments on commit e588bcd

Please sign in to comment.