urlsh is URL shortener application built on Go language.

urlsh

urlsh is URL shortener application built on Go language.

It does not use external libraries except the gorm for postgres database and minimal redis module redigo.

It registers itself as Go module github.com/adhocore/urlsh (however it has not been submitted to Go package registry for public usage).

URLSH

Usage

Just visit urlssh.xyz. You can also integrate API for programmatic usage. Read below for self hosting, API integration and/or contributing to urlsh.


Getting source

git clone [email protected]:adhocore/urlsh.git
cd urlsh

Configuring

It should be configured using env variables.

Please check .env.example for available variables and explanation.

DATABASE_URL is always required and is string of the following form:

DATABASE_URL=postgres://user:pass@host:port/dbname

When running urlsh with docker-compose, the preferred way, DATABASE_URL is automatically set from POSTGRES_* variables.

Please note that urlsh does not ship with .env loader so to run it in bare metal, one needs to use export KEY=VALUE or source .env manually.

If REDIS_URL is provided with redis://host:port, then it will cache popular urls, the popularity threshold is hard coded to minimal value in common.constant. The cache will sync when short url is deleted and sometimes when expired. The hit counters will however sync in realtime.

Setting up docker

To set up dockerized urlsh, run the commands below:

# first time only
cp .example.env .env

# change auth token for admin if you want in `.env` file
# APP_ADMIN_TOKEN=

docker-compose up

After a few seconds, you should be able to browse to localhost:2000.

Testing

For running tests,

docker-compose exec urlsh sh -c "APP_ENV=test go test ./..."

# for coverage
docker-compose exec urlsh sh -c "APP_ENV=test go test -cover ./..."

APP_ENV=test is not required but ensures that tests are run against clone database with name prefixed by test_. Normally test db is already prepared if you use docker-compose.


API Endpoints

GET /status

Status route for health/status check.

Response payload

{
    "status": 200,
    "message": "it works"
}

POST /api/urls

Creates a new short code for given URL.

Request example

  • url: string, required, http/https/ftp only
  • expires_on: string, optional, utc date yyyy-mm-dd hh:mm:ss, default=9999-01-01
  • keywords: array of strings, 2-25 chars each, max 10 keywords
{
    "url": "http://somedomain.com/some/very/long/url",
    "expires_on": "",
    "keywords": ["key", "word"]
}

Response example

{
    "status": 200,
    "short_code": "qaFxz",
    "short_url": "http://localhost:2000/qaFxz"
}

If env var APP_ALLOW_DUPE_URL is set to 0 or empty, then trying to shorten same URL again will return status 409 and payload will contain existing short_code. However if existing short_code is deleted, it will be shortened as usual.


GET /{shortCode}

Redirects the shortcode to original long URL.

Response payload

In case short code exists it responds with 301 redirect.

If the short code is expired or deleted, it responds like so:

{
    "status": 410,
    "message": "requested resource is not available"
}

GET /api/admin/urls

Authentication

Token required in Authorization header like so:

Authorization: Bearer 

Request query

The query params are optional.

page=
short_code=
keyword=

Examples:

  • /api/admin/urls?short_code=somecode
  • /api/admin/urls?page=1&keyword=something

Response example

Response contains multiple matching url object inside urls array.

{
    "status": 200,
    "urls": [
        {
            "short_code": "X5JkFd",
            "origin_url": "http://somedomain.com/some/very/long/url",
            "hits": 1,
            "is_deleted": false,
            "expires_on": "9999-01-01T00:00:00Z"
        }
    ]
}

DELETE /api/admin/urls

Authentication

Token required in Authorization header like so:

Authorization: Bearer 

Request query

Query param short_code is requied.

Example: /api/admin/urls?short_code=somecode

Response example

If delete success:

{
    "status": 200,
    "deleted": true
}

If the code does not exist:

{
    "status": 404,
    "message": "the given short code is not found"
}

Using postman

urlsh comes with postman collection and environment to aid manual testing of endpoints.

Open the postman app, click Import at top left, select Folder and drag/choose postman folder of this repo. You may need to adjust the token in postman urlsh env if you have configured APP_ADMIN_TOKEN.

The collection comes with post/pre request hooks for requests so you can just run the endpoints one after another in postman UI.

For redirect request, you have to disable postman follow redirects from Settings > General > Automatically follow redirects.

License

Please check license file.

Owner
Jitendra Adhikari
ISTJ. Thinker. Solver. Creator.
Jitendra Adhikari
Similar Resources

Cfurlshort - Url Shortener service with golang

Url Shortener This respository contains the code for my url shortener service. D

Jan 26, 2022

Goshort - Just another simple url shortener to use with curl

qnd.be Just another simple url shortener to use with curl. Kinda like 0x0.st. Ju

Jan 29, 2022

OMUS - One More URL Shortener

OMUS OMUS - One More URL Shortener; For now - planned only API. Functions: Gener

Feb 12, 2022

Short url backend - Go musthave shortener tpl

go-musthave-shortener-tpl Шаблон репозитория для практического трека «Go в веб-р

Feb 15, 2022

The 'Scalable' URL shortener

Chinnaswamy The 'Scalable' URL shortener Running tests To run end to end tests: CHINNASWAMY_TEST_HOST="host:port" go test -count=1 ./integration_tes

Dec 2, 2022

urlhunter is a recon tool that allows searching on URLs that are exposed via shortener services such as bit.ly and goo.gl.

urlhunter is a recon tool that allows searching on URLs that are exposed via shortener services such as bit.ly and goo.gl.

a recon tool that allows searching on URLs that are exposed via shortener services

Jan 7, 2023

GoogleBookAPI is built on top of flogo, a flow based application.

GoogleBookAPI Example GoogleBookAPI is built on top of flogo, a flow based application. Upon launch for first time, the application creates a topic go

Nov 19, 2021

This codebase was created to demonstrate a fully fledged fullstack application built with Golang/Echo including CRUD operations, authentication, routing, pagination, and more.

This codebase was created to demonstrate a fully fledged fullstack application built with Golang/Echo including CRUD operations, authentication, routing, pagination, and more.

This codebase was created to demonstrate a fully fledged fullstack application built with Golang/Echo including CRUD operations, authentication, routing, pagination, and more.

Jan 6, 2023

Go-watchdog - a web application observability tool built for Go

Go-watchdog - a web application observability tool built for Go

Go-watchdog is a web application observability tool built for Go, it exposes a status endpoint for application services like databases, caches, message-brokers, mails and storages.

Jul 11, 2022
Comments
  • Error

    Error "invalid memory address or nil pointer dereference" using docker-compose

    Hi! This seems like an awesome service, especially for self-hosters like myself.

    Running urlsh using docker-compose, the server and its requirements all seem to run fine, but when I click the "Shorten" button I receive a HTTP 500 error with the following message:

    {"message":"runtime error: invalid memory address or nil pointer dereference","status":500}
    

    I'm not seeing any logs in the docker-compose run, even when launching using APP_ENV=dev.

    What could be causing this issue? Any way I can debug it?

    Is it possible the error message is acutally coming from Docker (and not from urlsh)?

    Thanks.

  • Support redis cache

    Support redis cache

    closes #22

    • cache popular urls whose hits exceed certain threshold (hardcoded in constant.go)
    • on delete, inactive status is synced to cache
    • configure using APP_CACHE_HOST=host:port env var.
A URL shortener using http://is.gd/ and the Go programming language (http://golang.org/)

goisgd A simple command line URL shortener using http://is.gd/. Getting the Code go get github.com/NickPresta/GoURLShortener Usage Import this librar

Apr 6, 2022
A simple URL shortener application.

url-shortener A simple URL shortener application. Features The service supports 2 data stores (in-memory and PostgreSQL). The -db parameter is used to

Dec 9, 2021
a url shortener made using go and redis

__ | |--.--.--.----.-----. .-----.-----. | <| | | _|-- __|__| _ | _ | |__|__|_____|__| |_____|__|___

Dec 10, 2020
A barebones URL Shortener implementation in Go using Gin and MySQL. Also features a basic frontend.

URL Shortener in Go This is a barebones URL Shortener implementation in Go using the Gin web framework and MySQL. Also features a basic frontend. Loca

Dec 22, 2021
Go URL shortener

Golang Short URL To build the short link engine by Go. Development Environment: Ubuntu 20.04 Go 1.17.1 Requirement Console app, receive “URL” from any

Nov 4, 2021
Yandex practicum go url shortener service

go-musthave-shortener-tpl Шаблон репозитория для практического трека «Go в веб-р

Feb 26, 2022
Url Shortener Api For Golang

Url Shortener Api Using Go Endpoints Get and redirect to long url GET: "/:id" Cr

May 29, 2022
Simple URL shortener written in Golang

Alviss Introduction Simple URL shortener project, written in Golang. Setup and r

Aug 25, 2022
A sample url shortener app to test Keploy integration capabilities
A sample url shortener app to test Keploy integration capabilities

Example URL Shortener A sample url shortener app to test Keploy integration capabilities Installation git clone https://github.com/keploy/example-url-

Oct 22, 2022
API from AnonURL website, an anonymous URL shortener

API from AnonURL website, an anonymous URL shortener

Jan 23, 2022