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 host: 127.0.0.1
port: 8000 port: 8000
cors: False cors: False
static_folder: frontend/build
Web: Web:
host: 127.0.0.1 host: 127.0.0.1
port: 8000 port: 8000
static_folder: frontend/build
Database: Database:
username: foo username: foo

View file

@ -3,4 +3,4 @@
cd frontend cd frontend
npm run build npm run build
cd .. 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): def shorten_it(config: dict, session: Session, data: str, ttl: int):
shortener_config = config.get("Shortener", None) shortener = Shortener(session, config)
shortener = Shortener(session, shortener_config)
identifier = shortener.generate_uuid() identifier = shortener.generate_uuid()
if identifier: if identifier:
try: try:

View file

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