goshorturl

package module
v0.0.0-...-e2aef43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2021 License: Unlicense Imports: 9 Imported by: 0

README

================
Golang Short URL
================

.. image:: https://img.shields.io/badge/Language-Go-blue.svg
   :target: https://golang.org/

.. image:: https://godoc.org/github.com/siongui/goshorturl?status.svg
   :target: https://godoc.org/github.com/siongui/goshorturl

.. image:: https://github.com/siongui/goshorturl/workflows/ci/badge.svg
    :target: https://github.com/siongui/goshorturl/blob/master/.github/workflows/ci.yml

.. image:: https://goreportcard.com/badge/github.com/siongui/goshorturl
   :target: https://goreportcard.com/report/github.com/siongui/goshorturl

.. image:: https://img.shields.io/badge/license-Unlicense-blue.svg
   :target: https://github.com/siongui/goshorturl/blob/master/UNLICENSE


To build the short link engine by Go_.

Development Environment:

  - `Ubuntu 20.04`_
  - `Go 1.17.1`_


Requirement
+++++++++++

- Console app, receive “URL” from any website.
- Implement logic to make a unique shortlink and store in database. Maps the
  original link with short link generated by system.
- The application is able to translate the short link to be original link. This
  can be separate 2 menu / inputs.
- Use Postgres DB, bun_ to connect DB.
- Unit Testing is required.


Solution Concept
++++++++++++++++

The range of Go *uint64* is `[0, 18 446 744 073 709 551 615]`_, so we assume
that at most 18,446,744,073,709,551,615 short links can be stored.

Each record in the table of database contains three value
**(id uint64, originalURL string, shortURL string)**.

- For every **originalURL** is received, search **originalURL** field in the
  table of database:

  * If record found, return **shortURL**.
  * If record not found, randomly generate a *uint64* value. Search the value
    in the table of database. If the value already exists, re-genetate a
    *uint64* value until the value does not exist in the table of database. Then
    use base58_ to encode the value into a string. This string is the
    **shortURL** we need.

- For every **shortURL** is received, search **shortURL** field in the table of
  database:

  * If record found, return **originalURL**.
  * If record not found, return HTTP 404.


Usage
+++++

*go get* this package:

.. code-block:: bash

  $ go get github.com/siongui/goshorturl

Run the console via:

.. code-block:: bash

  $ go run commandline/console.go

The following is my test ouput:

.. code-block:: txt

  Please enter short url code or URL (ctrl+c to quit): https://github.com/siongui/goshorturl
  Inserted - Id: 1232122882728144256 , Short Url Code: 3RTdEPo6pjG , URL: https://github.com/siongui/goshorturl
  Please enter short url code or URL (ctrl+c to quit): https://pkg.go.dev/github.com/siongui/goshorturl
  Inserted - Id: 4666768087814501688 , Short Url Code: bQiaEdTC3NU , URL: https://pkg.go.dev/github.com/siongui/goshorturl
  Please enter short url code or URL (ctrl+c to quit): https://github.com/siongui/goshorturl/blob/main/shorturlcode.go
  Inserted - Id: 18175005311586501991 , Short Url Code: JbWhAa28A6V , URL: https://github.com/siongui/goshorturl/blob/main/shorturlcode.go
  Please enter short url code or URL (ctrl+c to quit): https://goreportcard.com/report/github.com/siongui/goshorturl
  Inserted - Id: 11860174333799028715 , Short Url Code: twKYhrhQbWF , URL: https://goreportcard.com/report/github.com/siongui/goshorturl
  Please enter short url code or URL (ctrl+c to quit): https://github.com/siongui/goshorturl/blob/main/db.go
  Inserted - Id: 9819574194256159787 , Short Url Code: oN2CP25jESg , URL: https://github.com/siongui/goshorturl/blob/main/db.go
  Please enter short url code or URL (ctrl+c to quit): https://github.com/siongui/goshorturl/blob/main/go.mod
  Inserted - Id: 14955371154374818695 , Short Url Code: AHthuoexycz , URL: https://github.com/siongui/goshorturl/blob/main/go.mod
  Please enter short url code or URL (ctrl+c to quit): https://github.com/siongui/goshorturl/blob/main/go.mod
  short url code:  AHthuoexycz
  Please enter short url code or URL (ctrl+c to quit): https://github.com/siongui/goshorturl
  short url code:  3RTdEPo6pjG
  Please enter short url code or URL (ctrl+c to quit): bQiaEdTC3NU
  original URL:  https://pkg.go.dev/github.com/siongui/goshorturl
  Please enter short url code or URL (ctrl+c to quit): bQiaEdTC3N 
  HTTP 404 not found
  Please enter short url code or URL (ctrl+c to quit): twKYhrhQbWF
  original URL:  https://goreportcard.com/report/github.com/siongui/goshorturl
  Please enter short url code or URL (ctrl+c to quit): ^Csignal: interrupt


UNLICENSE
+++++++++

Released in public domain. See UNLICENSE_.


References
++++++++++

.. [1] | `algorithm for url shortening - Google search <https://www.google.com/search?q=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - DuckDuckGo search <https://duckduckgo.com/?q=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - Ecosia search <https://www.ecosia.org/search?q=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - Qwant search <https://www.qwant.com/?q=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - Bing search <https://www.bing.com/search?q=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - Yahoo search <https://search.yahoo.com/search?p=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - Baidu search <https://www.baidu.com/s?wd=algorithm+for+url+shortening>`_
       | `algorithm for url shortening - Yandex search <https://www.yandex.com/search/?text=algorithm+for+url+shortening>`_

.. [2] `How to design a tiny URL or URL shortener? - GeeksforGeeks <https://www.geeksforgeeks.org/how-to-design-a-tiny-url-or-url-shortener/>`_

.. [3] | `golang url shortener - Google search <https://www.google.com/search?q=golang+url+shortener>`_
       | `golang url shortener - DuckDuckGo search <https://duckduckgo.com/?q=golang+url+shortener>`_
       | `golang url shortener - Ecosia search <https://www.ecosia.org/search?q=golang+url+shortener>`_
       | `golang url shortener - Qwant search <https://www.qwant.com/?q=golang+url+shortener>`_
       | `golang url shortener - Bing search <https://www.bing.com/search?q=golang+url+shortener>`_
       | `golang url shortener - Yahoo search <https://search.yahoo.com/search?p=golang+url+shortener>`_
       | `golang url shortener - Baidu search <https://www.baidu.com/s?wd=golang+url+shortener>`_
       | `golang url shortener - Yandex search <https://www.yandex.com/search/?text=golang+url+shortener>`_

.. [4] `How to Create a Custom URL Shortener Using Golang and Redis <https://intersog.com/blog/how-to-write-a-custom-url-shortener-using-golang-and-redis/>`_
.. [5] | `Let's build a URL shortener in Go - Final Part : Forwarding <https://www.eddywm.com/lets-build-a-url-shortener-in-go-part-iv-forwarding/>`_
       | `GitHub - eddywm/go-shortener-wm: A  super fast url shortener service written in Go <https://github.com/eddywm/go-shortener-wm>`_
.. [6] `Using PostgreSQL with Go - Calhoun.io <https://www.calhoun.io/using-postgresql-with-go/>`_
.. [7] `[Golang] Seed Pseudorandom Number Generator (PRNG) Properly <https://siongui.github.io/2017/03/21/go-seed-pseudo-random-number-generator-properly/>`_


.. _Go: https://golang.org/
.. _Ubuntu 20.04: https://releases.ubuntu.com/20.04/
.. _Go 1.17.1: https://golang.org/dl/
.. _UNLICENSE: https://unlicense.org/
.. _bun: https://github.com/uptrace/bun
.. _[0, 18 446 744 073 709 551 615]: https://stackoverflow.com/a/6878625
.. _base58: https://github.com/itchyny/base58-go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateShortUrlTable

func CreateShortUrlTable() (sql.Result, error)

CreateShortUrlTable creates table in the database to store short links.

func GetRandomId

func GetRandomId() uint64

GetRandomId generate a pseudo random number.

func GetShortUrlCodeFromId

func GetShortUrlCodeFromId(id uint64) string

GetShortUrlCodeFromId uses base58 encoding to convert number to short url code.

func InitSQLite

func InitSQLite(verbose bool) (err error)

InitSQLite initialize in-memory database to store data. The verbose flag indicates whether to print all queries to stdout.

func InsertShortUrl

func InsertShortUrl(u ShortUrl) (result sql.Result, err error)

InsertShortUrl inserts one record of short links in the database.

Types

type ShortUrl

type ShortUrl struct {
	Id           string
	ShortUrlCode string
	OriginalUrl  string
}

ShortUrl represents the structure of short links.

func SelectAllShortUrl

func SelectAllShortUrl() (us []ShortUrl, err error)

SelectAllShortUrl reads all short links records from the database.

func SelectById

func SelectById(id uint64) (us ShortUrl, err error)

SelectById selects the record by id in the database.

func SelectByOriginalUrl

func SelectByOriginalUrl(oriurl string) (us ShortUrl, err error)

SelectByOriginalUrl selects the record by original URL in the database.

func SelectByShortUrlCode

func SelectByShortUrlCode(code string) (us ShortUrl, err error)

SelectByShortUrlCode selects the record by short url code in the database.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL