mirror of
https://git.ikl.sh/132ikl/liteshort.git
synced 2023-08-10 21:13:04 +03:00
Add database path selector
This commit is contained in:
parent
968d0e77ac
commit
035d8e85e0
@ -6,7 +6,7 @@ from appdirs import site_config_dir, user_config_dir
|
|||||||
from pkg_resources import resource_filename
|
from pkg_resources import resource_filename
|
||||||
from yaml import safe_load
|
from yaml import safe_load
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.INFO)
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ def get_config():
|
|||||||
for path in paths:
|
for path in paths:
|
||||||
f = path / "config.yml"
|
f = path / "config.yml"
|
||||||
if f.exists():
|
if f.exists():
|
||||||
LOGGER.debug(f"Selecting config file {f}")
|
LOGGER.info(f"Selecting config file {f}")
|
||||||
return open(f, "r")
|
return open(f, "r")
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
|
@ -1,15 +1,21 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
|
from appdirs import user_data_dir
|
||||||
from bcrypt import checkpw
|
from bcrypt import checkpw
|
||||||
from flask import current_app, g, redirect, render_template, request, url_for
|
from flask import current_app, g, redirect, render_template, request, url_for
|
||||||
|
|
||||||
from .config import load_config
|
from .config import load_config
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -183,11 +189,27 @@ def validate_long(long): # https://stackoverflow.com/a/36283503
|
|||||||
# Database connection functions
|
# Database connection functions
|
||||||
|
|
||||||
|
|
||||||
|
def db_path(name):
|
||||||
|
paths = [
|
||||||
|
Path("/var/lib/liteshort/"),
|
||||||
|
Path(user_data_dir("liteshort", "132ikl")),
|
||||||
|
]
|
||||||
|
for path in paths:
|
||||||
|
try:
|
||||||
|
path.mkdir(exist_ok=True)
|
||||||
|
db = path / Path(name + ".db")
|
||||||
|
LOGGER.info(f"Selecting database file {db}")
|
||||||
|
return str(db)
|
||||||
|
except (PermissionError, OSError):
|
||||||
|
LOGGER.warn(f"Failed to access database in {path}")
|
||||||
|
LOGGER.debug("", exc_info=True)
|
||||||
|
raise FileNotFoundError("Cannot access database file")
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
if "db" not in g:
|
if "db" not in g:
|
||||||
g.db = sqlite3.connect(
|
g.db = sqlite3.connect(
|
||||||
"".join((current_app.config["database_name"], ".db")),
|
current_app.config["database"], detect_types=sqlite3.PARSE_DECLTYPES
|
||||||
detect_types=sqlite3.PARSE_DECLTYPES,
|
|
||||||
)
|
)
|
||||||
g.db.cursor().execute("CREATE TABLE IF NOT EXISTS urls (long,short)")
|
g.db.cursor().execute("CREATE TABLE IF NOT EXISTS urls (long,short)")
|
||||||
return g.db
|
return g.db
|
||||||
@ -208,6 +230,8 @@ def close_db(error):
|
|||||||
|
|
||||||
|
|
||||||
app.config.update(load_config()) # Add YAML config to Flask config
|
app.config.update(load_config()) # Add YAML config to Flask config
|
||||||
|
app.config["database"] = db_path(app.config["database_name"])
|
||||||
|
|
||||||
app.secret_key = app.config["secret_key"]
|
app.secret_key = app.config["secret_key"]
|
||||||
app.config["SERVER_NAME"] = app.config["site_domain"]
|
app.config["SERVER_NAME"] = app.config["site_domain"]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user