enh(#8): Refactoring code

This commit is contained in:
Elia el Lazkani 2024-12-24 01:27:21 +01:00
parent 000729a925
commit a925c87872
4 changed files with 12 additions and 6 deletions

View file

@ -2,11 +2,11 @@ Server:
host: 127.0.0.1
port: 8000
cors: False
static_folder: frontend/build
Web:
host: 127.0.0.1
port: 8000
static_folder: frontend/build
Database:
username: foo

View file

@ -3,4 +3,4 @@
cd frontend
npm run build
cd ..
poetry run shortenit
poetry run shortenit "$@"

View file

@ -54,8 +54,7 @@ def main() -> typing.NoReturn:
def shorten_it(config: dict, session: Session, data: str, ttl: int):
shortener_config = config.get("Shortener", None)
shortener = Shortener(session, shortener_config)
shortener = Shortener(session, config)
identifier = shortener.generate_uuid()
if identifier:
try:

View file

@ -22,6 +22,7 @@ class Web:
self.app.run(host=self.host, port=self.port, debug=self.debug)
def init(self):
server_config = self.handler.configuration.get("Server", None)
self.app = Flask(__name__)
self.setup_routes()
if server_config and server_config.get("cors", False):
@ -58,6 +59,9 @@ class SiteHandler:
{trafaret.Key("url"): trafaret.URL, trafaret.Key("timestamp"): trafaret.Int}
)
def _get_server_config(self):
return self.configuration.get("Server", None)
def shortenit(self):
data = request.get_json()
try:
@ -69,7 +73,10 @@ class SiteHandler:
abort(400)
try:
short_url = self.shorten_url(
self.configuration, self.database, data["url"], data["timestamp"]
self.configuration.get("Shortener", None),
self.database,
data["url"],
data["timestamp"],
)
except KeyError as e:
self.logger.error(e)
@ -103,7 +110,7 @@ class SiteHandler:
try:
project_root = Path(__file__).parent.parent
static_folder = (
f"{project_root}/" + self.configuration.get("Web")["static_folder"]
f"{project_root}/" + self._get_server_config()["static_folder"]
)
if check_file(static_folder + "/" + path):
return send_from_directory(static_folder, path)