Moved database to subdirectory "data"

This resolves problems in sqlite when the directory containing the
database is not writable by the webserver, even when the database file
itself is.
This commit is contained in:
Thomas Kolb 2016-02-10 21:37:39 +01:00
parent 383643bdca
commit 2ef757b74b
2 changed files with 5 additions and 3 deletions

View file

@ -1,2 +1,4 @@
# schort # schort
schort is a tiny link shortener written in python3 and flask schort is a tiny link shortener written in python3 and flask
You need to create a directory /data/ which is writable by the webserver, which will contain the link database.

View file

@ -7,7 +7,7 @@ app = Flask(__name__)
@app.route('/<shortLink>', methods=['GET', 'POST']) @app.route('/<shortLink>', methods=['GET', 'POST'])
def short(shortLink=""): def short(shortLink=""):
if request.method == "GET": if request.method == "GET":
conn = sqlite3.connect("links.sqlite") conn = sqlite3.connect("data/links.sqlite")
c = conn.cursor() c = conn.cursor()
result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone() result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone()
if result: if result:
@ -32,7 +32,7 @@ def insertIdUnique(idToCheck, longUrl):
if len(idToCheck) == 0: if len(idToCheck) == 0:
idToCheck = base64Url[:4] idToCheck = base64Url[:4]
conn = sqlite3.connect("links.sqlite") conn = sqlite3.connect("data/links.sqlite")
c = conn.cursor() c = conn.cursor()
try: try:
c.execute('INSERT INTO links VALUES (?, ?, ?, ?, ?)', (idToCheck, longUrl, int(time.time()), request.remote_addr, "default" )) c.execute('INSERT INTO links VALUES (?, ?, ?, ?, ?)', (idToCheck, longUrl, int(time.time()), request.remote_addr, "default" ))
@ -60,7 +60,7 @@ def insertIdUnique(idToCheck, longUrl):
return databaseId return databaseId
def initDB(): def initDB():
conn = sqlite3.connect("links.sqlite") conn = sqlite3.connect("data/links.sqlite")
c = conn.cursor() c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS links (shortLink UNIQUE NOT NULL, longLink, timestamp, ip, redirectMethod);''') c.execute('''CREATE TABLE IF NOT EXISTS links (shortLink UNIQUE NOT NULL, longLink, timestamp, ip, redirectMethod);''')
conn.commit() conn.commit()