Code example, first pass.
This commit is contained in:
		
							parent
							
								
									3ba2901a0b
								
							
						
					
					
						commit
						06cf147005
					
				| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					.#*
 | 
				
			||||||
 | 
					*~
 | 
				
			||||||
 | 
					*#
 | 
				
			||||||
 | 
					*.aux
 | 
				
			||||||
 | 
					*.pyc
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,11 @@
 | 
				
			||||||
 | 
					[[source]]
 | 
				
			||||||
 | 
					url = "https://pypi.python.org/simple"
 | 
				
			||||||
 | 
					verify_ssl = true
 | 
				
			||||||
 | 
					name = "pypi"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[packages]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[dev-packages]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[requires]
 | 
				
			||||||
 | 
					python_version = "3.8"
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,20 @@
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    "_meta": {
 | 
				
			||||||
 | 
					        "hash": {
 | 
				
			||||||
 | 
					            "sha256": "e2a8a78582d100dc86a0694f5ad982ca341a6b861fd871c6306733562f9e16cc"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "pipfile-spec": 6,
 | 
				
			||||||
 | 
					        "requires": {
 | 
				
			||||||
 | 
					            "python_version": "3.8"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "sources": [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                "name": "pypi",
 | 
				
			||||||
 | 
					                "url": "https://pypi.python.org/simple",
 | 
				
			||||||
 | 
					                "verify_ssl": true
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    "default": {},
 | 
				
			||||||
 | 
					    "develop": {}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,11 @@
 | 
				
			||||||
 | 
					# TinyURL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Getting started
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					git checkout <repository>
 | 
				
			||||||
 | 
					cd tinyurl
 | 
				
			||||||
 | 
					pipenv shell
 | 
				
			||||||
 | 
					python ./tinyurl.py
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,10 @@
 | 
				
			||||||
 | 
					<!DOCTYPE html>
 | 
				
			||||||
 | 
					<html>
 | 
				
			||||||
 | 
					  <head>
 | 
				
			||||||
 | 
					    <meta charset="utf-8">
 | 
				
			||||||
 | 
					    <title>Error {{code}}</title>
 | 
				
			||||||
 | 
					  </head>
 | 
				
			||||||
 | 
					  <body>
 | 
				
			||||||
 | 
						<h1>Error {{code}}</h1>
 | 
				
			||||||
 | 
					  </body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,47 @@
 | 
				
			||||||
 | 
					from flask import Flask, json, redirect, request, render_template, jsonify
 | 
				
			||||||
 | 
					import short_url
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app = Flask(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					found = {}
 | 
				
			||||||
 | 
					database = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@app.route('/', methods=['POST'])
 | 
				
			||||||
 | 
					def post_tinyurl():
 | 
				
			||||||
 | 
					    body = request.get_json(silent=True, force=True)
 | 
				
			||||||
 | 
					    if body is None or 'url' not in body:
 | 
				
			||||||
 | 
					        return (render_template("error.html", code=400), 400)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    url = body['url']
 | 
				
			||||||
 | 
					    if url in found:
 | 
				
			||||||
 | 
					        return jsonify({
 | 
				
			||||||
 | 
					            'url': url,
 | 
				
			||||||
 | 
					            'short_url_code': '/' + found[url]
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    counter = len(database) + 1
 | 
				
			||||||
 | 
					    newurl = short_url.encode_url(counter)
 | 
				
			||||||
 | 
					    database[newurl] = url
 | 
				
			||||||
 | 
					    found[url] = newurl
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return jsonify({
 | 
				
			||||||
 | 
					        'url': url,
 | 
				
			||||||
 | 
					        'short_url_code': '/' + newurl
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@app.route('/<url>', methods=["GET"])
 | 
				
			||||||
 | 
					def get_tinyurl(url):
 | 
				
			||||||
 | 
					    if url not in database:
 | 
				
			||||||
 | 
					        return render_template('error.html', code=404), 404
 | 
				
			||||||
 | 
					    return redirect(database[url], 301)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@ app.errorhandler(404)
 | 
				
			||||||
 | 
					def not_found(error):
 | 
				
			||||||
 | 
					    return render_template('error.html', code=404), 404
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    app.run(debug=True)
 | 
				
			||||||
		Loading…
	
		Reference in New Issue