Commit 2326f3e0 authored by Поляков Сергей Викторович's avatar Поляков Сергей Викторович
Browse files

2 Part.

Added script yaml_to_redis
Added redis storage
Web-service work with yaml and redis
parent dafa53ed
Pipeline #3617 failed with stages
in 32 seconds
FROM redis
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
FROM python:3.9
RUN mkdir -p /usr/src/app/
WORKDIR /usr/src/app/
COPY . /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["python", "main.py"]
\ No newline at end of file
from lib.yaml_lib import YamlManager
from lib.libs import Config
from pathlib import Path
import redis
conf = Config(Path("pref.json"))
yaml_manager = YamlManager(path=Path('data', conf.name_yaml_bd))
redis_cli = redis.Redis(host=conf.redis_host, port=conf.redis_port, db=conf.redis_db)
def export_data():
try:
for i in yaml_manager.read():
redis_cli.set(name=i['short_link'], value=i['full_link'])
print(f"Successfully added key: {i['short_link']} value: {i['full_link']}")
except Exception as e:
print(f'Error: {e}')
finally:
redis_cli.close()
export_data()
......@@ -12,9 +12,15 @@ class Config:
js = json.loads(f.read())
storage = js['storage']
name_yaml_bd = js['name_yaml_bd']
redis_host = js['redis_host']
redis_port = js['redis_port']
redis_db = js['redis_db']
self.storage = storage
self.name_yaml_bd = name_yaml_bd
self.redis_host = redis_host
self.redis_port = redis_port
self.redis_db = redis_db
class LinkGenerator:
......
import redis
class RedisManager:
def __init__(self, host='localhost', port=6379, db=0):
self._host = host
self._port = port
self._db = db
def __enter__(self):
""" Opening connection with Redis"""
self._redis_cli = redis.Redis(host=self._host, port=self._port, db=self._db)
return self
def key_exists(self, key):
"""Returns TRUE if key exists"""
if self._redis_cli.exists(key) == 1:
return True
else:
return False
def save(self, key, value):
try:
self._redis_cli.set(name=key, value=value)
except Exception as e:
print(f'Error: {e}')
def get_by_key(self, key):
"""Return value by key"""
try:
return self._redis_cli.get(key).decode('UTF-8')
except Exception as e:
print(e)
return None
def __exit__(self, exc_type, exc_val, exc_tb):
""" Closing the connection """
self._redis_cli.close()
if exc_val:
raise
from pathlib import Path
import uvicorn
from fastapi import FastAPI
from starlette.responses import RedirectResponse
from lib.libs import Config
from lib.yaml_manager import YamlManager
from lib.yaml_lib import YamlManager
from lib.redis_lib import RedisManager
conf = Config(Path("pref.json"))
path_to_yaml_db = Path("data", conf.name_yaml_bd)
yaml = YamlManager(path=path_to_yaml_db)
yaml_manager = YamlManager(path=Path("data", conf.name_yaml_bd))
app = FastAPI()
......@@ -17,8 +16,18 @@ app = FastAPI()
@app.get("/{shorted_link}")
async def short_link(shorted_link: str):
if conf.storage == 'yaml':
key = yaml.get_by_key(shorted_link)
key = yaml_manager.get_by_key(shorted_link)
if key:
return RedirectResponse(url=key)
else:
if conf.storage == 'redis':
with RedisManager(host=conf.redis_host, port=conf.redis_port, db=conf.redis_db) as t:
key = t.key_exists(shorted_link)
if key:
return RedirectResponse(url=t.get_by_key(shorted_link))
return '404 Not found'
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
{
"storage": "yaml",
"name_yaml_bd": "db.yaml"
"storage": "redis",
"name_yaml_bd": "db.yaml",
"redis_host" : "localhost",
"redis_port" : 6379,
"redis_db" : 0
}
......@@ -3,3 +3,4 @@ fastapi==0.68.1
uvicorn==0.15.0
starlette~=0.14.2
pathlib~=1.0.1
redis~=3.5.3
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment