1
0
mirror of https://git.ikl.sh/132ikl/liteshort.git synced 2023-08-10 21:13:04 +03:00

Release v1.2.1 (#22)

This commit is contained in:
132ikl 2020-04-11 17:00:03 -04:00 committed by GitHub
commit 2e308f38db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -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:

View File

@ -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"]