Commit ecd5c6fb authored by Emanuele Santoro's avatar Emanuele Santoro

Initial relational database support

parent 5cae76c1
#!/usr/bin/env python3
import os
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Link(Base) :
__tablename__ = "links"
id = Column(Integer, primary_key=True)
url = Column(String)
short = Column(String)
def __init__(self, url, short) :
self.url = url
self.short = short
def __repr__(self) :
return "<Link(id='%d', short='%s', url='%s')>" % (self.id, self.short, self.url)
def create_connection() :
DB_URL = os.getenv("DATABASE_URL")
DB_TYPE = os.getenv("DB_TYPE")
engine = create_engine(DB_URL, echo=True)
return engine
manu@nadia.16918:1460031346
\ No newline at end of file
#!/usr/bin/env python3
import os
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Link(Base) :
__tablename__ = "links"
id = Column(Integer, primary_key=True)
url = Column(String)
short = Column(String)
def __init__(self, url, short) :
self.url = url
self.short = short
def __repr__(self) :
return "<Link(id='%d', short='%s', url='%s')>" % (self.id, self.short, self.url)
def create_connection() :
DB_URL = os.getenv("DATABASE_URL")
DB_TYPE = os.getenv("DB_TYPE")
engine = create_engine(DB_URL, echo=True)
import os
import hashlib
from flask import *
musho = Flask(__name__)
from flask_sqlalchemy import SQLAlchemy
links = dict()
page_cache = dict()
musho_url = "http://localhost:9000"
debug = None
port = int(os.getenv("PORT", "9000"))
musho_url = os.getenv("MUSHO_URL", "http://localhost:%d" % port)
debug = os.getenv("DEBUG", None)
database_url = os.getenv("DATABASE_URL", 'sqlite:////tmp/musho.db')
print("MUSHO_URL:\t\t%s" % musho_url)
print("PORT:\t\t\t%d" % port)
print("DEBUG:\t\t\t%s" % debug)
musho = Flask(__name__)
musho.config['SQLALCHEMY_DATABASE_URI'] = database_url
musho.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
musho.config['SQLALCHEMY_ECHO'] = False
db = SQLAlchemy(musho)
db.create_all()
class Link(db.Model):
id = db.Column(db.Integer, primary_key=True)
short_url = db.Column(db.String(), unique=True)
full_url = db.Column(db.String(), unique=True)
def __init__(self, short, full):
self.full_url = full
self.short_url = short
def __repr__(self):
return "<Link '%s' -> '%s'" % (self.short_url, self.full_url)
def page(path) :
if path not in page_cache :
......@@ -26,7 +53,11 @@ def saveLink() :
hash.update(url.encode('ascii','ignore'))
digest = hash.hexdigest()
short_url = digest[0:8]
links[short_url] = url
new_link = Link(short_url, url)
db.session.add(new_link)
db.session.commit()
full_url = musho_url +"/l/" + short_url
return render_template('shortened_link.html', url=full_url)
......@@ -35,28 +66,15 @@ def saveLink() :
def resolveLink(link_id):
if debug:
print("Risolvo il link '%s'" % link_id)
if link_id in links :
url = links[link_id]
return redirect(url)
target = Link.query.filter_by(short_url=link_id).first()
if target is not None :
print("Target is not none")
return redirect(target.full_url)
else :
if debug:
print("Link non trovato")
print("contenuto del db:")
print(links)
return redirect("/")
debug = os.getenv("DEBUG", None)
if debug is not None :
musho.debug = True
port = 9000
try :
port = int(os.getenv("PORT", "9000"))
except :
pass
musho_url = os.getenv("MUSHO_URL", "http://localhost:%d" % port)
print("MUSHO_URL:\t\t%s" % musho_url)
print("PORT:\t\t\t%d" % port)
print("DEBUG:\t\t\t%s" % debug)
if __name__ == "__main__" :
musho.run(port=port)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment