Commit 49be16ed authored by Emanuele Santoro's avatar Emanuele Santoro

Initial commit

#!/usr/bin/env bash
export DEBUG=true
export PORT=9000
web: python3
Musho is the **M**icro **u**rl **sho**rtener.
It's a very very basic web application built as a demo of what you can
do in terms of web development with Python.
Of course, you can do much more than this, but this is meant to be a
super-quick demo, as short as possible, as fast as it can be.
## Running on your desktop
In order to be able to run this application on the Heroku public
cloud, we use some particular programming tecniques, like setting some
options via environment variables.
For this reason, there is a `` script in the repository.
Such file is meant to be copied as (for example) `` on the
development machine and then "sourced" before running the application:
vi ## customize your settings
## Running on the Heroku cloud platform
Things get just a little bit trickier, but not much.
When using the Heroku platform, you still have to set options via
environment variables, but in order to set such options project-wide,
you will have to use the `heroku` utility, in the project directory:
heroku config:set DEBUG:false
git push heroku master
import os
import hashlib
from flask import *
musho = Flask(__name__)
links = dict()
page_cache = dict()
def page(path) :
if path not in page_cache :
contents = open(path, "r").read()
page_cache[path] = contents
return page_cache[path]
def index() :
return page("templates/index.html")
#return open("views/index.html", "r").read()
@mush.route("/save", methods=['POST'])
def saveLink() :
url = request.form["url"]
hash ='sha1')
digest = hash.hexdigest()
short_url = digest[0:8]
links[short_url] = url
full_url = "http://localhost:9000/l/" + short_url
return render_template('shortened_link.html', url=full_url)
def resolveLink(link_id):
if link_id in links :
url = links[link_id]
return redirect(url)
else :
return redirect("/")
if __name__ == "__main__" :
debug = os.getenv("DEBUG", None)
if debug is not None :
mush.debug = True
port = 9000
try :
port = int(os.getenv("PORT", "9000"))
except :
<!doctype html>
<title>Musho: a simple micro-url-shortener</title>
<h1>Shorten a new url</h1>
<form name="url_shorten" action="/save" method="POST">
<td><input name="url" type="text" placeholder="your url here"></td>
<td><input type="submit" value="Shorten!"></td>
<!doctype html>
<title>You have shortened a link!</title>
<h1>Here is your shortened link!</h1>
<hr style="width:65%;">
<p><a href="{{ url }}">{{ url }}</a></p>
<p> Share on <a href="#">Facebook</a><a href="#">Twitter</a></p>
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