From c4013943a1f3a210dcad87bea06c1bbb0a1e489e Mon Sep 17 00:00:00 2001 From: berkay Date: Mon, 13 Jan 2025 17:22:20 +0300 Subject: [PATCH] Config and Service initilaized --- .idea/.gitignore | 3 + .idea/inspectionProfiles/Project_Default.xml | 34 ++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/wag-managment-api-service-version-4.iml | 8 + AllConfigs/Email/configs.py | 18 +++ AllConfigs/Email/email_send_model.py | 12 ++ AllConfigs/NoSqlDatabase/configs.py | 10 ++ AllConfigs/Redis/configs.py | 8 + AllConfigs/SqlDatabase/configs.py | 11 ++ AllConfigs/__init__.py | 3 + AllConfigs/main.py | 3 + ApiServiceRedis/Email/send_email.py | 36 +++++ ApiServiceRedis/Redis/Actions/actions.py | 148 ++++++++++++++++++ ApiServiceRedis/Redis/Models/response.py | 32 ++++ ApiServiceRedis/Redis/Models/row.py | 25 +++ ApiServiceRedis/Redis/conn.py | 29 ++++ ApiServiceRedis/Redis/howto.py | 10 ++ ApiServiceRedis/__init__.py | 0 api-docker-compose.yml | 57 +++++++ 22 files changed, 474 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/wag-managment-api-service-version-4.iml create mode 100644 AllConfigs/Email/configs.py create mode 100644 AllConfigs/Email/email_send_model.py create mode 100644 AllConfigs/NoSqlDatabase/configs.py create mode 100644 AllConfigs/Redis/configs.py create mode 100644 AllConfigs/SqlDatabase/configs.py create mode 100644 AllConfigs/__init__.py create mode 100644 AllConfigs/main.py create mode 100644 ApiServiceRedis/Email/send_email.py create mode 100644 ApiServiceRedis/Redis/Actions/actions.py create mode 100644 ApiServiceRedis/Redis/Models/response.py create mode 100644 ApiServiceRedis/Redis/Models/row.py create mode 100644 ApiServiceRedis/Redis/conn.py create mode 100644 ApiServiceRedis/Redis/howto.py create mode 100644 ApiServiceRedis/__init__.py create mode 100644 api-docker-compose.yml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..94568fb --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,34 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fa53637 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5ae937e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..49d9f73 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/wag-managment-api-service-version-4.iml b/.idea/wag-managment-api-service-version-4.iml new file mode 100644 index 0000000..001c2cd --- /dev/null +++ b/.idea/wag-managment-api-service-version-4.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/AllConfigs/Email/configs.py b/AllConfigs/Email/configs.py new file mode 100644 index 0000000..2cd61c9 --- /dev/null +++ b/AllConfigs/Email/configs.py @@ -0,0 +1,18 @@ +from AllConfigs import HostConfig + + +class EmailConfig: + EMAIL_HOST: str = HostConfig.EMAIL_HOST + EMAIL_USERNAME: str = "karatay@mehmetkaratay.com.tr" + EMAIL_PASSWORD: str = "system" + EMAIL_PORT: int = 587 + EMAIL_SEND: bool = False + + @classmethod + def as_dict(cls): + return dict( + host=EmailConfig.EMAIL_HOST, + port=EmailConfig.EMAIL_PORT, + username=EmailConfig.EMAIL_USERNAME, + password=EmailConfig.EMAIL_PASSWORD, + ) diff --git a/AllConfigs/Email/email_send_model.py b/AllConfigs/Email/email_send_model.py new file mode 100644 index 0000000..2b25428 --- /dev/null +++ b/AllConfigs/Email/email_send_model.py @@ -0,0 +1,12 @@ +from pydantic import BaseModel +from typing import List, Dict, Optional + +class EmailSendModel(BaseModel): + subject: str + html: str = "" + receivers: List[str] + text: Optional[str] = "" + cc: Optional[List[str]] = None + bcc: Optional[List[str]] = None + headers: Optional[Dict] = None + attachments: Optional[Dict] = None diff --git a/AllConfigs/NoSqlDatabase/configs.py b/AllConfigs/NoSqlDatabase/configs.py new file mode 100644 index 0000000..8dcc208 --- /dev/null +++ b/AllConfigs/NoSqlDatabase/configs.py @@ -0,0 +1,10 @@ +from AllConfigs import HostConfig + + +class MongoConfig: + PASSWORD = "mongo_password" + USER_NAME = "mongo_user" + DATABASE_NAME = "mongo_database" + HOST = HostConfig.MAIN_HOST + PORT = 11777 + URL = f"mongodb://{USER_NAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE_NAME}?retryWrites=true&w=majority" diff --git a/AllConfigs/Redis/configs.py b/AllConfigs/Redis/configs.py new file mode 100644 index 0000000..a6531b8 --- /dev/null +++ b/AllConfigs/Redis/configs.py @@ -0,0 +1,8 @@ +from AllConfigs import HostConfig + + +class WagRedis: + REDIS_HOST = HostConfig.MAIN_HOST + REDIS_PASSWORD: str = "commercial_redis_password" + REDIS_PORT: int = 11222 + REDIS_DB: int = 0 diff --git a/AllConfigs/SqlDatabase/configs.py b/AllConfigs/SqlDatabase/configs.py new file mode 100644 index 0000000..7478ba7 --- /dev/null +++ b/AllConfigs/SqlDatabase/configs.py @@ -0,0 +1,11 @@ +from AllConfigs import HostConfig + + +class WagDatabase: + HOST: str = HostConfig.MAIN_HOST + PORT: str = "5444" + SQL: str = "postgresql+psycopg2" + USERNAME: str = "berkay_wag_user" + PASSWORD: str = "berkay_wag_user_password" + DATABASE_NAME: str = "wag_database" + DATABASE_URL: str = f"{SQL}://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE_NAME}" diff --git a/AllConfigs/__init__.py b/AllConfigs/__init__.py new file mode 100644 index 0000000..7712fd9 --- /dev/null +++ b/AllConfigs/__init__.py @@ -0,0 +1,3 @@ +from .main import HostConfig + +__all__ = ["HostConfig"] diff --git a/AllConfigs/main.py b/AllConfigs/main.py new file mode 100644 index 0000000..2e3d78d --- /dev/null +++ b/AllConfigs/main.py @@ -0,0 +1,3 @@ +class HostConfig: + MAIN_HOST = "10.10.2.36" # http://10.10.2.36 + EMAIL_HOST = "10.10.2.34" # http://10.10.2.34 diff --git a/ApiServiceRedis/Email/send_email.py b/ApiServiceRedis/Email/send_email.py new file mode 100644 index 0000000..1c22a7d --- /dev/null +++ b/ApiServiceRedis/Email/send_email.py @@ -0,0 +1,36 @@ +from AllConfigs.Email.configs import EmailConfig +from AllConfigs.Email.email_send_model import EmailSendModel +from redmail import EmailSender + +email_sender = EmailSender(**EmailConfig.as_dict()) + + +class EmailService: + + @classmethod + def send_email( + cls, + params : EmailSendModel + ) -> bool: + if not EmailConfig.EMAIL_SEND: + print("Email sending is disabled", params) + return False + try: + email_sender.connect() + receivers = ["karatay@mehmetkaratay.com.tr"] + email_sender.send( + subject=params.subject, + receivers=receivers, + text=params.text + f" : Gonderilen [{str(receivers)}]", + html=params.html, + cc=params.cc, + bcc=params.bcc, + headers=params.headers or {}, + attachments=params.attachments or {}, + ) + return True + except Exception as e: + print(f"Error raised at email send :{e}") + finally: + email_sender.close() + return False diff --git a/ApiServiceRedis/Redis/Actions/actions.py b/ApiServiceRedis/Redis/Actions/actions.py new file mode 100644 index 0000000..f8985b4 --- /dev/null +++ b/ApiServiceRedis/Redis/Actions/actions.py @@ -0,0 +1,148 @@ +import json +import arrow +from typing import ( + Any, + Optional, + List, + Dict, +) + +from ApiServiceRedis.Redis.conn import redis_cli +from ApiServiceRedis.Redis.Models.response import RedisResponse + + +class RedisRow: + + key: str | bytes + value: Any + delimiter: str = "*" + expires_at: Optional[str] = None + + @classmethod + def merge(cls, set_values: list[str | bytes]): + for key, set_value in enumerate(set_values): + set_value = str(set_value) if isinstance(set_value, bytes) else set_value + cls.key += f"{set_value}" if key == len(set_values) - 1 else f"{set_value}{cls.delimiter}" + cls.key = cls.key.encode() + + @classmethod + def regex(cls, list_keys: list[str | bytes]): + search_regex = "" + for key, list_key in enumerate(list_keys): + list_key = list_key.decode() if isinstance(list_key, bytes) else str(list_key) + if key == 0 and list_key: + search_regex += f"{list_key}{cls.delimiter}*" + elif key == len(list_keys) - 1 and list_key: + search_regex += f"*{cls.delimiter}{list_key}" + else: + if list_key: + search_regex += f"*{cls.delimiter}{list_key}{cls.delimiter}*" + return search_regex + + @classmethod + def parse(cls): + return cls.key.split(cls.delimiter) if cls.key else [] + + @classmethod + def feed(cls, value: Any): + cls.value = json.dumps(value) + + @classmethod + def modify(cls, add_dict): + value = cls.data or {} + cls.feed({**value, **add_dict}) + + @classmethod + def remove(cls, key): + value = cls.data or {} + value.pop(key) + cls.feed(value) + + @property + def keys(self): + return self.key if isinstance(self.key, str) else self.key.decode() + + @property + def redis_key(self): + return self.key if isinstance(self.key, bytes) else self.key.encode() + + @property + def data(self): + return json.loads(self.value) + + @property + def as_dict(self): + return { + "keys": self.keys, + "value": self.data, + } + + +class RedisActions: + + @classmethod + def get_expiry_time(cls, expiry_kwargs: dict) -> int: + expiry_time = 0 + if "days" in expiry_kwargs: + expiry_time += int(expiry_kwargs["days"]) * 24 * 60 * 60 + if "hours" in expiry_kwargs: + expiry_time += int(expiry_kwargs["hours"]) * 60 * 60 + if "minutes" in expiry_kwargs: + expiry_time += int(expiry_kwargs["minutes"]) * 60 + if "seconds" in expiry_kwargs: + expiry_time += int(expiry_kwargs["seconds"]) + return expiry_time + + @classmethod + def set_json( + cls, list_keys: list[str | bytes], value: Optional[dict | list], expires: Optional[dict] = None + ): + redis_row = RedisRow() + redis_row.merge(set_values=list_keys) + redis_row.feed(value) + try: + if expires: + expiry_time = cls.get_expiry_time(expiry_kwargs=expires) + redis_cli.setex( + name=redis_row.redis_key, + time=expiry_time, + value=redis_row.value, + ) + redis_row.expires_at = arrow.now().shift(seconds=expiry_time).format("YYYY-MM-DD HH:mm:ss") + else: + redis_cli.set(name=redis_row.redis_key, value=redis_row.value) + return RedisResponse( + status=True, + message="Value is set successfully.", + data=redis_row, + ) + except Exception as e: + return RedisResponse( + status=False, + message="Value is not set successfully.", + error=str(e), + ) + + @classmethod + def get_json(cls, list_keys: list[str | bytes]): + try: + redis_row = RedisRow() + json_get = redis_cli.get(redis_row.regex(list_keys=list_keys)) + redis_row.key = json_get + if not json_get: + return RedisResponse( + status=False, + message="Value is not get successfully.", + error="Value is not found in the redis.", + ) + return RedisResponse( + status=True, + message="Value is get successfully.", + data=json.loads(json_get), + ) + except Exception as e: + return RedisResponse( + status=False, + message="Value is not get successfully.", + error=str(e), + ) diff --git a/ApiServiceRedis/Redis/Models/response.py b/ApiServiceRedis/Redis/Models/response.py new file mode 100644 index 0000000..5c02bf2 --- /dev/null +++ b/ApiServiceRedis/Redis/Models/response.py @@ -0,0 +1,32 @@ +from typing import Union, Dict, List +from ApiServiceRedis.Redis.Actions.actions import RedisRow + + +class RedisResponse: + + def __init__( + self, + status: bool, + message: str, + data: RedisRow = None, + error: str = None, + ): + self.status = status + self.message = message + self.data = data + if isinstance(data, dict): + self.data_type = "dict" + elif isinstance(data, list): + self.data_type = "list" + elif data is None: + self.data_type = None + self.error = error + + def as_dict(self): + return { + "status": self.status, + "message": self.message, + "data": self.data, + "dataType": self.data_type, + "error": self.error, + } diff --git a/ApiServiceRedis/Redis/Models/row.py b/ApiServiceRedis/Redis/Models/row.py new file mode 100644 index 0000000..f36a998 --- /dev/null +++ b/ApiServiceRedis/Redis/Models/row.py @@ -0,0 +1,25 @@ +from pydantic import BaseModel + + +class AccessToken(BaseModel): + + accessToken: str + userUUID: str + + def to_list(self): + return [self.accessToken, self.userUUID] + + @property + def count(self): + return 2 + + @property + def delimiter(self): + return "*" + + +# access_token_obj = AccessToken.from_dict({ +# "accessToken": "token", +# "userUUID": "uuid" +# }) +# access_token_obj.to_list() \ No newline at end of file diff --git a/ApiServiceRedis/Redis/conn.py b/ApiServiceRedis/Redis/conn.py new file mode 100644 index 0000000..4713218 --- /dev/null +++ b/ApiServiceRedis/Redis/conn.py @@ -0,0 +1,29 @@ +from redis import Redis +from api_configs import WagRedis + + +class RedisConn: + + def __init__(self): + self.redis = Redis( + host=WagRedis.REDIS_HOST, + password=WagRedis.REDIS_PASSWORD, + port=WagRedis.REDIS_PORT, + db=WagRedis.REDIS_DB, + ) + if not self.check_connection(): + raise Exception("Connection error") + + def check_connection(self): + return self.redis.ping() + + def set_connection(self, host, password, port, db): + self.redis = Redis(host=host, password=password, port=port, db=db) + return self.redis + + +try: + redis_conn = RedisConn() + redis_cli = redis_conn.redis +except Exception as e: + print("Redis Connection Error", e) diff --git a/ApiServiceRedis/Redis/howto.py b/ApiServiceRedis/Redis/howto.py new file mode 100644 index 0000000..5b2fca4 --- /dev/null +++ b/ApiServiceRedis/Redis/howto.py @@ -0,0 +1,10 @@ +from ApiServiceRedis.Redis.Actions.actions import RedisActions +from ApiServiceRedis.Redis.Models.row import AccessToken + + +redis_cli_actions = RedisActions() + +access_object = AccessToken( + accessToken="token", + userUUID="uuid" +) diff --git a/ApiServiceRedis/__init__.py b/ApiServiceRedis/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api-docker-compose.yml b/api-docker-compose.yml new file mode 100644 index 0000000..0976315 --- /dev/null +++ b/api-docker-compose.yml @@ -0,0 +1,57 @@ +services: + + wag_management_auth_service: + container_name: wag_management_auth_service + # restart: on-failure + build: + context: . + dockerfile: ApiServices/AuthService/Dockerfile + ports: + - "1111:41575" + environment: + - PYTHONPATH=/service_app + volumes: + - auth_venv:/service_app/.venv + - auth_logs:/service_app/logs + + wag_management_validation_service: + container_name: wag_management_validation_service + # restart: on-failure + build: + context: . + dockerfile: ApiServices/ValidationService/Dockerfile + ports: + - "1113:41575" + environment: + - PYTHONPATH=/service_app + volumes: + - validation_venv:/service_app/.venv + - validation_logs:/service_app/logs + +# wag_management_init_service: +# container_name: wag_management_init_service +# build: +# context: . +# dockerfile: service_app_init/Dockerfile + +# wag_management_event_service: +# container_name: wag_management_event_service +# # restart: on-failure +# build: +# context: . +# dockerfile: ApiServices/EventService/Dockerfile +# ports: +# - "1112:41575" +# environment: +# - PYTHONPATH=/service_app +# volumes: +# - event_venv:/service_app/.venv +# - event_logs:/service_app/logs + +volumes: + auth_venv: + event_venv: + validation_venv: + auth_logs: + event_logs: + validation_logs: