Skip to content

Simple URL shortener using FastAPI, SQLAlchemy and SQLite3

License

Notifications You must be signed in to change notification settings

smartgic/shortgic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT contributions welcomeDiscord

Shortgic

A minimalist and lightweight URL shortener using FastAPI and SQLAlchemy and SQLite3.

Installation

Shortgic requires few Python libraries such as FastAPI, SQLAlchemy for the database integration and Uvicorn for serving the application.

$ python3 -m venv ~/.venvs/shortgic
$ source ~/venvs/shortgic/bin/activate
$ git clone https://github.com/smartgic/shortgic.git
$ cd shortgic
$ pip install -r requirements.txt
$ uvicorn app.main:app

A Docker image is availble on Docker Hub (cf. https://hub.docker.com/repository/docker/smartgic/shortgic)

$ docker run -d --name shortgic -p 8000:8000 -e SHORTGIC_DB_PATH=/db/shortgic.db smartgic/shortgic:latest

Usage

As any FastAPI project, a Swagger documention is available at http://127.0.0.1:8000/docs

Create a basic URL shortened.

$ curl -s -X POST http://127.0.0.1:8000 -H "accept: application/json" -H "Content-Type: application/json" -d '{"target": "https://smartgic.io"}'
{
  "link": "BEN9S"
}

Create an URL shortened with extras information, extras must be a JSON dictionary.

$ curl -s -X POST http://127.0.0.1:8000 -H "accept: application/json" -H "Content-Type: application/json" -d '{"target": "https://smartgic.io", "extras": {"version": "0.2b-1", "validated": true}}'
{
  "link": "UY9JN"
}

Redirection to the target URL, the -L option should be used with curl to follow the 302 redirection.

$ curl -s -X GET http://127.0.0.1:8000/UY9JN -H "accept: application/json" -L

Get link information, this will output the target and the extras information.

$ curl -s -X GET http://127.0.0.1:8000/UY9JN/info -H "accept: application/json"
{
  "target": "https://smartgic.io",
  "extras": {
    "version": "0.2b-1",
    "validated": true
  }
}

Remove permanently an URL shortened from the database.

$ curl -s -X DELETE http://127.0.0.1:8000/UY9JN -H "accept: application/json"