Giter Site home page Giter Site logo

highoncarbs / shorty Goto Github PK

View Code? Open in Web Editor NEW
106.0 5.0 24.0 353 KB

:link: A URL shortening service built using Flask and MySQL

License: MIT License

Python 61.68% CSS 7.78% HTML 30.54%
python mysql url-shortener flask urlshortener webapp uwsgi analytics nginx database

shorty's Introduction

๐Ÿ”— Shorty

A self-hosted URL shortening service built using Flask & Mysql.

Demo image of Shorty

๐ŸŽ‰ Surprise : Click Analytics

Tracking for Operating system and platform used to access the link is available.

Demo image for analytics

Getting Started

Clone or download this repository.

git clone https://github.com/PadamSethia/shorty.git

cd shorty/

python ./app.py

Prerequisites

This project requires Flask and MySQL . And MySQLdb python module for connection. You can install it using the following commands .

pip install flask

For mysql as backend

sudo apt install mysql-server

sudo apt-get install libmysqlclient-dev

pip install MySQL-python

Now run the following command to create the MySQL table

python ./create_table.py

Configure the MySQL database credentials in config.py Set the host , user , password and database name for MySQL connection.

Also under MySQL shell set

set autocommit = 0

This takes care of the concurrency issue.

RESTful API

POST /api/v1/shorten with form data url=https://github.com/PadamSethia , custom=blah & tag=test custom and tag are optional.

{
    "custom": "blah",
    "long_url": "https://github.com/PadamSethia",
    "short_url": "http://localhost:5454/blah",
    "tag": "test" 
}

GET /api/v1/expand/ with form data custom=blah

{
    "click_browser": {
        "CHROME": 0,
        "FIREFOX": 0,
        "OTHER_BROWSER": 0,
        "SAFARI": 0
    },
    "click_platform": {
        "ANDROID": 0,
        "IOS": 0,
        "LINUX": 0,
        "MAC": 0,
        "OTHER_PLATFORM": 0,
        "WINDOWS": 0
    },
    "clicks": 0,
    "custom": "blah",
    "long_url": "http://github.com/PadamSethia",
    "tag" : "test"
}

for now the web app and api run on different ports. app : 5000 , api : 8000

Running for production

This section outlines how to setup and run shorty on web servers. In this case I'll consider AWS EC2 instance.

  • Set up the EC2 instance.

...Learn more

  • Clone this git repository.

Change the Host in app.run() to 0.0.0.0 , for both shorty_api.py and app.py

Now if you run python ./app.py , shorty should run under the server IP as <server host>:<port> By default the port is 5000 , make sure to open the port before running the app.

Creating Script for running the app

The inbuilt flask server is not built for production , it might do down after a couple of days . So we need to use production ready web servers , for this we'll use uWSGI and Nginx.

First install uwsgi and nginx on EC2 instance

pip install uwsgi

If it doesn't work install from distro repo

For Ubuntu

sudo apt-get install uwsgi-core uwsgi-plugin-python & nginx

For CentOS / Amazon Linux

sudo yum install uwsgi & nginx

Using uWSGI to run app.py and shorty_api.py

The uwsgi file is already setup in this repo , just change the to your user name . For eg ,

wsgi-file = /home/<user>/shorty/app.py

Just change the <user> to your sepecified user name , for me it would be

wsgi-file = /home/padam/shorty/app.py

So update the uwsgi_app and uwsgi_api files.

Now to run the script , run

uwsgi --ini uwsgi_app &

The & is used to run app.py in background.

For the API run

uwsgi --ini uwsgi_api &

Both the processes will run in background .

Projects Used

License

This project is licensed under the MIT Licene.

shorty's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

shorty's Issues

Getting installing errror

Traceback (most recent call last):
File "c:/Users/madhav/Desktop/Hafta-master/shorty/shorty/app.py", line 9, in
import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'

SQL injection risk

Hello,

I noticed that in function app.py:reroute() the variable short_url is used to build an SQL query without prior sanitization (lines 138-146). This can lead to an SQL injection.

An easy way to fix it would be using prepared statements as everywhere else in file app.py.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.