Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

Commit

Permalink
Release v1.2.1 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
132ikl authored Apr 11, 2020
2 parents 968d0e7 + 035d8e8 commit 2e308f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions liteshort/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pkg_resources import resource_filename
from yaml import safe_load

logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)


Expand All @@ -24,7 +24,7 @@ def get_config():
for path in paths:
f = path / "config.yml"
if f.exists():
LOGGER.debug(f"Selecting config file {f}")
LOGGER.info(f"Selecting config file {f}")
return open(f, "r")

for path in paths:
Expand Down
28 changes: 26 additions & 2 deletions liteshort/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import logging
import os
import random
import sqlite3
import time
import urllib
from pathlib import Path

import flask
from appdirs import user_data_dir
from bcrypt import checkpw
from flask import current_app, g, redirect, render_template, request, url_for

from .config import load_config

logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)

app = flask.Flask(__name__)


Expand Down Expand Up @@ -183,11 +189,27 @@ def validate_long(long): # https://stackoverflow.com/a/36283503
# 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():
if "db" not in g:
g.db = sqlite3.connect(
"".join((current_app.config["database_name"], ".db")),
detect_types=sqlite3.PARSE_DECLTYPES,
current_app.config["database"], detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.cursor().execute("CREATE TABLE IF NOT EXISTS urls (long,short)")
return g.db
Expand All @@ -208,6 +230,8 @@ def close_db(error):


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.config["SERVER_NAME"] = app.config["site_domain"]

Expand Down

0 comments on commit 2e308f3

Please sign in to comment.