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

Initial commit

parents
*~
__pycache__
#!/usr/bin/env bash
export DEBUG=true
export PORT=9000
web: python3 musho.py
Musho
========
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 `ENV_SAMPLE.sh` script in the repository.
Such file is meant to be copied as (for example) `DEV_ENV.sh` on the
development machine and then "sourced" before running the application:
cp ENV_SAMPLE.sh DEV_ENV.sh
vi DEV_ENV.sh ## customize your settings
source DEV_ENV.sh
python musho.py
## 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]
@mush.route("/")
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 = hashlib.new('sha1')
hash.update(url.encode('ascii','ignore'))
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)
@mush.route("/l/<string:link_id>")
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 :
pass
musho.run(port=port)
flask==0.10.1
<!doctype html>
<html>
<head>
<title>Musho: a simple micro-url-shortener</title>
</head>
<body>
<center>
<h1>Shorten a new url</h1>
<form name="url_shorten" action="/save" method="POST">
<table>
<tr>
<td>Url:</td>
<td><input name="url" type="text" placeholder="your url here"></td>
<td><input type="submit" value="Shorten!"></td>
</table>
</form>
</center>
</body>
</html>
<!doctype html>
<html>
<head>
<title>You have shortened a link!</title>
</head>
<body>
<center>
<h1>Here is your shortened link!</h1>
<hr style="width:65%;">
<br>
<p><a href="{{ url }}">{{ url }}</a></p>
<br/>
<p> Share on <a href="#">Facebook</a><a href="#">Twitter</a></p>
</center>
</body>
</html>
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