shortener-go

command module
v0.0.0-...-97650a1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Shortener

Solution for shortening URLs using aliases. With this it's possible to:

  • Shorten URLs
  • Use a shortened URL to redirect to the original one
  • Check the top 10 most visited URLs

How It Works

About the alias generation

The generator uses a seed that is based on the current UTC time to generate random numbers. Since the alias is a string composed of the english alphanumerical, the algorithm generates a number between 0 and the length of a string containing all upper and lower case letters of the alphabet + decimal digits. It then uses that as an index to get a character from the string. It repeats this process 6 times to get an alias that is easy to remember and also checks if it doesn't already exist to avoid conflicts.

Project Setup

Local

You need to create a .env file. All the environment variables are available in the .env.example file located in the root of the project. You can also use its current values to ease the process.

The database is inside a Docker container. There's a couple of ways to run it using the docker-compose.yaml provided:

Docker Compose (for Compose V1 use "docker-compose" instead)
docker compose up -d
GNU Make
make up

You also need to run the database migration scripts. To do that you can either run the db/schema.sql file manually or use dbmate to run the scritps inside db/migrations/ from the root folder (which is why there's a DBMATE_MIGRATIONS_TABLE environment variable).

Finally, you can run the API:

Development build
go run main.go
Production build
go build
./shortener-go
Docker

You can also run the API from within a Docker container:

Docker Compose
docker compose --profile release up
GNU Make
make deploy

To check the logs:

Docker Compose
docker compose logs -tf
GNU Make
make logs

To stop the containers:

Docker Compose
docker compose down --remove-orphans
GNU Make
make down

Tests

To run both unit and integration tests run:

go test ./...

Client

The solution comes with a client to consume the API. To use it, run the following commands inside client/:

Development build
npm install
npm run dev
Production build
npm install
npm run build
npm run preview

You may also use Yarn or PNPM, if desired

API Reference

Shorten URL
POST http://<HOST>:<PORT>/create?url=<string>&CUSTOM_ALIAS=<string?>
Retrieve URL
GET http://<HOST>:<PORT>/url/<alias>
Get Most Visited URLs
GET http://<HOST>:<PORT>/most-visited

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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