Skip to content
Snippets Groups Projects
Commit 77ce18e0 authored by Jonas Leder's avatar Jonas Leder
Browse files

Merge branch 'beta'

parents 692a7fee c124dc2e
Branches
Tags
No related merge requests found
# URL shorter
### made by jonasled
#### About
This is a URL shorter written in Python with Flask als Webhandler. The Webserver I used is Waitress and for the Database I used sqlite, because it's compact and works in this enviorement also with big databases (>2million entries) very quick. I started develloping this URL schorter, because google stopped their own and I can't find a very good one. Every shorter at the time on the market was boring or needed an account. For the first Version I used PHP, but the problem was, I packed everything in one very big file which was very hard to understand. At the time I wanted to use more than one domain I realized it is very hard to implement this, so I started searching for a better solution. The i found Flask, ehich is perfect, because you can split the Website code and the backend into multiple Files. So have fun to use this and contribute to it. This is a URL shorter written in Python with Flask als Webhandler. The Webserver I used is Waitress and for the Database I used sqlite, because it's compact and works in this enviorement also with big databases (>2million entries) very quick. I started develloping this URL schorter, because google stopped their own and I can't find a very good one. Every shorter at the time on the market was boring or needed an account. For the first Version I used PHP, but the problem was, I packed everything in one very big file which was very hard to understand. At the time I wanted to use more than one domain I realized it is very hard to implement this, so I started searching for a better solution. The i found Flask, ehich is perfect, because you can split the Website code and the backend into multiple Files. So have fun to use this and contribute to it.
#### Why is this one better than other
* no ADs
* free to use
* custom urls
* easy user management
* small and modern UI
* posibility to use multiple domains
* open source --> posibility to expand
[![pipeline status](http://gitlab.jonasled.de/jonasled/url_shorter_docker/badges/master/pipeline.svg)](http://gitlab.jonasled.de/jonasled/url_shorter_docker/commits/master) [![pipeline status](http://gitlab.jonasled.de/jonasled/url_shorter_docker/badges/master/pipeline.svg)](http://gitlab.jonasled.de/jonasled/url_shorter_docker/commits/master)
![screenshot of url shorter](https://gitlab.jonasled.de/jonasled/url_shorter_docker/raw/master/Screenshot.png) ![screenshot of url shorter](https://gitlab.jonasled.de/jonasled/url_shorter_docker/raw/master/Screenshot.png)
\ No newline at end of file
...@@ -16,7 +16,8 @@ services: ...@@ -16,7 +16,8 @@ services:
- recaptcha_private= #Please enter here your private Key for google recaptcha - recaptcha_private= #Please enter here your private Key for google recaptcha
- recaptcha_public= #Please enter here your public Key for google recaptcha - recaptcha_public= #Please enter here your public Key for google recaptcha
- host=0.0.0.0 #With this variable you can set the access ip range. 127.0.0.1 means you can only access it from the local network and 0.0.0.0 means everyone can access it. - host=0.0.0.0 #With this variable you can set the access ip range. 127.0.0.1 means you can only access it from the local network and 0.0.0.0 means everyone can access it.
- GITHUB_CLIENT_ID= #You have to set these four variables, if not the shorter will not run. To get the github keys visit https://github.com/settings/developers and register a new oauth application. The callback path is /user/github-callback - login=1 #set this to 0 if you want to disable the oauth login. If you do so, you also don't have to fill in the oauth credentials below.
- GITHUB_CLIENT_ID= #To get the github keys visit https://github.com/settings/developers and register a new oauth application. The callback path is /user/github-callback
- GITHUB_CLIENT_SECRET= - GITHUB_CLIENT_SECRET=
- GOOGLE_CLIENT_ID= - GOOGLE_CLIENT_ID=
- GOOGLE_CLIENT_SECRET= - GOOGLE_CLIENT_SECRET=
......
...@@ -16,7 +16,8 @@ services: ...@@ -16,7 +16,8 @@ services:
- recaptcha_private= #Please enter here your private Key for google recaptcha - recaptcha_private= #Please enter here your private Key for google recaptcha
- recaptcha_public= #Please enter here your public Key for google recaptcha - recaptcha_public= #Please enter here your public Key for google recaptcha
- host=0.0.0.0 #With this variable you can set the access ip range. 127.0.0.1 means you can only access it from the local network and 0.0.0.0 means everyone can access it. - host=0.0.0.0 #With this variable you can set the access ip range. 127.0.0.1 means you can only access it from the local network and 0.0.0.0 means everyone can access it.
- GITHUB_CLIENT_ID= #You have to set these four variables, if not the shorter will not run. To get the github keys visit https://github.com/settings/developers and register a new oauth application. The callback path is /user/github-callback - login=1 #set this to 0 if you want to disable the oauth login. If you do so, you also don't have to fill in the oauth credentials below.
- GITHUB_CLIENT_ID= #To get the github keys visit https://github.com/settings/developers and register a new oauth application. The callback path is /user/github-callback
- GITHUB_CLIENT_SECRET= - GITHUB_CLIENT_SECRET=
- GOOGLE_CLIENT_ID= - GOOGLE_CLIENT_ID=
- GOOGLE_CLIENT_SECRET= - GOOGLE_CLIENT_SECRET=
......
from flask import render_template, abort from flask import render_template, abort
def home(request, builddate, version, domain_prepared, recaptchaPublicKey, showDomainSelect, cookieNotice, domain_to_index, s): def home(request, builddate, version, domain_prepared, recaptchaPublicKey, showDomainSelect, cookieNotice, domain_to_index, s, loginEnabled):
try: try:
loginbar = "Hello " + s.loads(request.cookies.get('username')) + ' (<a href="/user/links" style="color:white">your links</a>, <a href="/user/logout" style="color:white">logout</a>)' loginbar = "Hello " + s.loads(request.cookies.get('username')) + ' (<a href="/user/links" style="color:white">your links</a>, <a href="/user/logout" style="color:white">logout</a>)'
except: except:
loginbar = '<a href="#" onClick="showLogin()" style="color:white">login</a>' loginbar = '<a href="#" onClick="showLogin()" style="color:white">login</a>'
try: try:
return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginbar=loginbar, cookieNotice=cookieNotice ,domain_prefilled=domain_to_index[request.headers["host"]]) #return the default site to create a new shorten link return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginbar=loginbar, cookieNotice=cookieNotice ,domain_prefilled=domain_to_index[request.headers["host"]], loginEnabled=loginEnabled) #return the default site to create a new shorten link
except: except:
abort(500) abort(500)
\ No newline at end of file
#!/usr/bin/env python3 #!/usr/bin/env python3
#Import os Libraries #Import os Libraries
from waitress import serve #Used as webserver (Production) from waitress import serve #Used as webserver (Production)
from flask import Flask, request, redirect, make_response #Used to prepare the dynamic pages from flask import Flask, request, redirect, make_response, abort #Used to prepare the dynamic pages
from os import environ #Used for getting the enviorement variables from os import environ #Used for getting the enviorement variables
from itsdangerous import URLSafeSerializer #used for signing the cookies from itsdangerous import URLSafeSerializer #used for signing the cookies
from random import choice#used for signing the cookies from random import choice#used for signing the cookies
...@@ -77,6 +77,13 @@ try: ...@@ -77,6 +77,13 @@ try:
except: except:
host="127.0.0.1" host="127.0.0.1"
try:
if(environ["login"] == "1"): loginEnabled = True
else: loginEnabled = False
except:
loginEnabled = False
if(loginEnabled):
try: #Try to get the oauth keys, if it fails, abort and print a message to console try: #Try to get the oauth keys, if it fails, abort and print a message to console
GITHUB_CLIENT_ID = environ['GITHUB_CLIENT_ID'] GITHUB_CLIENT_ID = environ['GITHUB_CLIENT_ID']
GITHUB_CLIENT_SECRET = environ['GITHUB_CLIENT_SECRET'] GITHUB_CLIENT_SECRET = environ['GITHUB_CLIENT_SECRET']
...@@ -122,7 +129,7 @@ else: ...@@ -122,7 +129,7 @@ else:
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def home_get(): def home_get():
return home(request, builddate, version, domain_prepared, recaptchaPublicKey, showDomainSelect, cookieNotice, domain_to_index, s) return home(request, builddate, version, domain_prepared, recaptchaPublicKey, showDomainSelect, cookieNotice, domain_to_index, s, loginEnabled)
@app.route('/', methods=['POST']) #This function is used to create a new url @app.route('/', methods=['POST']) #This function is used to create a new url
def home_post(): def home_post():
...@@ -139,16 +146,18 @@ def redirect_short_url(short_url): ...@@ -139,16 +146,18 @@ def redirect_short_url(short_url):
@app.route('/user/login') @app.route('/user/login')
def loginPage(): def loginPage():
return login(request, GITHUB_CLIENT_ID, cookieNotice, GOOGLE_CLIENT_ID, url_scheme, domain) if(loginEnabled): return login(request, GITHUB_CLIENT_ID, cookieNotice, GOOGLE_CLIENT_ID, url_scheme, domain)
else: abort(404)
@app.route("/user/google-callback") @app.route("/user/google-callback")
def authorizeGoogle(): def authorizeGoogle():
return googleCallback(request, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, url_scheme, domain, s) if(loginEnabled): return googleCallback(request, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, url_scheme, domain, s)
else: abort(404)
@app.route('/user/github-callback') #Github redirects to this link after the user authenticated. Then we use the Token we get from github and request via the github api the username and the userid @app.route('/user/github-callback') #Github redirects to this link after the user authenticated. Then we use the Token we get from github and request via the github api the username and the userid
def authorizeGithub(): def authorizeGithub():
return githubCallback(request, GITHUB_CLIENT_SECRET, GITHUB_CLIENT_ID, s) if(loginEnabled): return githubCallback(request, GITHUB_CLIENT_SECRET, GITHUB_CLIENT_ID, s)
else: abort(404)
@app.route('/user/logout') @app.route('/user/logout')
def logout(): def logout():
...@@ -163,11 +172,13 @@ def redirectOwnLinks(): ...@@ -163,11 +172,13 @@ def redirectOwnLinks():
@app.route('/user/links<pageNumber>')#This function gives the user the posibility to see and delete his links @app.route('/user/links<pageNumber>')#This function gives the user the posibility to see and delete his links
def ownLinks(pageNumber): def ownLinks(pageNumber):
return userProfile(request, cookieNotice, s, pageNumber, url_scheme) if(loginEnabled): return userProfile(request, cookieNotice, s, pageNumber, url_scheme)
else: abort(404)
@app.route('/user/delete') #This function is called if a user deletes an entrie @app.route('/user/delete') #This function is called if a user deletes an entrie
def delete(): def delete():
return deleteLink(request, s) if(loginEnabled): return deleteLink(request, s)
else: abort(404)
@app.route('/user/makeqr', methods=['POST']) @app.route('/user/makeqr', methods=['POST'])
def makeQrCode(): def makeQrCode():
...@@ -184,9 +195,11 @@ def apiDocs(): ...@@ -184,9 +195,11 @@ def apiDocs():
return apiGet(request, url_scheme, s, sAPI) return apiGet(request, url_scheme, s, sAPI)
if __name__ == '__main__': def startup():
table_check()# This code checks whether database table is created or not table_check()# This code checks whether database table is created or not
if production: #Check if production variable is set to true use the waitress webserver, else use the buildin flask webserver, with more debug output if production: #Check if production variable is set to true use the waitress webserver, else use the buildin flask webserver, with more debug output
serve(app, host=host, port= 5000, url_scheme=url_scheme) #Start the Webserver for all users on port 5000 serve(app, host=host, port= 5000, url_scheme=url_scheme) #Start the Webserver for all users on port 5000
else: else:
app.run(host=host, port=5000, debug=True) #Start the Webserver in Debug mode. This means, if the script runs in an error, it will show the error message in Browser. app.run(host=host, port=5000, debug=True) #Start the Webserver in Debug mode. This means, if the script runs in an error, it will show the error message in Browser.
if (__name__ == "__main__"): startup()
\ No newline at end of file
...@@ -19,7 +19,9 @@ ...@@ -19,7 +19,9 @@
</head> </head>
<body> <body>
{% if loginEnabled %}
<div id="loginbar">{{loginbar | safe}}</div> <div id="loginbar">{{loginbar | safe}}</div>
{% endif %}
<div class="login-page"> <div class="login-page">
<div class="form"> <div class="form">
{% if not short_url %} {% if not short_url %}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment