enh(#8): Refactoring code
This commit is contained in:
parent
a305ea9388
commit
d2fb9a5565
4 changed files with 12 additions and 6 deletions
|
@ -1,11 +1,11 @@
|
||||||
Server:
|
Server:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 8000
|
port: 8000
|
||||||
|
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
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
cd frontend
|
cd frontend
|
||||||
npm run build
|
npm run build
|
||||||
cd ..
|
cd ..
|
||||||
poetry run shortenit
|
poetry run shortenit "$@"
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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()
|
||||||
CORS(self.app)
|
CORS(self.app)
|
||||||
|
@ -56,6 +57,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:
|
||||||
|
@ -67,7 +71,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)
|
||||||
|
@ -101,7 +108,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)
|
||||||
|
|
Loading…
Reference in a new issue