Skip to content

Commit

Permalink
new released api version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
loujr committed May 1, 2024
1 parent 25cdf0c commit b78eb23
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ urls.db
.profile
.bashrc
.bash*
.sqlite_history
Binary file modified __pycache__/webapp.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/wsgi.cpython-310.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test lol
33 changes: 19 additions & 14 deletions webapp.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from flask import Flask, request, redirect, render_template, jsonify, url_for
from flask_caching import Cache
from dotenv import load_dotenv
import os
import json
import random
import string
import sqlite3
from flask_restful import Api

load_dotenv()

app = Flask(__name__, subdomain_matching=True)
cache = Cache()
app.config['SERVER_NAME'] = os.getenv("SERVER_NAME")
app.config['APIENDPOINT'] = os.getenv("APIENDPOINT")
cache = Cache()
api = Api(app)
shortened_urls = {}

Expand Down Expand Up @@ -41,19 +44,21 @@ def generate_short_url(length=6):
short_url = "".join(random.choice(chars) for _ in range(length))
return short_url

@app.route("/", methods=["GET", "POST"])
def index():

@app.route('/shorten_url', methods=['POST'])
def shorten_url():
data = request.get_json()
long_url = data['long_url']
short_url = generate_short_url()
conn = get_db_connection()
if request.method == "POST":
long_url = request.form['long_url']
short_url = generate_short_url()
conn.execute('INSERT INTO urls (short_url, long_url) VALUES (?, ?)',
(short_url, long_url))
conn.commit()
conn.close()
return render_template("short_url.html", short_url=url_for('redirect_url', short_url=short_url, _external=True))
else:
return render_template("index.html")
conn.execute('INSERT INTO urls (short_url, long_url) VALUES (?, ?)',
(short_url, long_url))
conn.commit()
conn.close()
return jsonify(short_url=url_for('redirect_url', short_url=short_url, _external=True, _scheme='https'))

#curl -X POST -H "Content-Type: application/json" -d '{"long_url":"http://youtube.com"}' https://api.fwd2.app/shorten_url
#{"short_url":"https://localhost/3lupAP"}

@cache.cached(timeout=60)
@app.route("/<short_url>")
Expand All @@ -80,4 +85,4 @@ def api_ip_endpoint():
create_table()

if __name__ == "__main__":
app.run()
app.run()

0 comments on commit b78eb23

Please sign in to comment.