Categorygithub.com/anijackich/go-url-shortener

# README

Go URL Shortener

Ozon Tech CI pipeline Go version Go Report Card

Simple Go API application for URL shortener service.

✨ Features

  • Convert long URL to a short link
  • Expand long URL from a short link
  • In-memory storage
  • PostgreSQL storage

đŸ•šī¸ Play with Demo

The application is deployed on ozon.anjk.ru, and you can try to interact with it through Swagger.

POST /api/v1/shorten

Shorten link

Converts a long URL to a short link

Request body

{
  "long_url": "https://some-very-very.com/very/very/long/url"
}

Response body

{
  "short_url": "https://example.com/a1b2c3d4e5"
}

GET /api/v1/expand?u=<short_url>

Expand link

Returns a long URL for the specified short link

Request params

u: https://example.com/a1b2c3d4e5

Response body

{
  "long_url": "https://some-very-very.com/very/very/long/url"
}

âŦ‡ī¸ Clone & Setup

  1. Clone this repository

    git clone https://github.com/anijackich/go-url-shortener
    
  2. Go to go-url-shortener directory

    cd go-url-shortener
    
  3. Rename .env.example to .env

    mv .env.example .env
    
  4. Set environment variables

    VariableDescription
    HOSTHost serving the API
    PORTPort serving the API
    DOMAINBase domain for shortened links
    LINK_CODE_LENGTHLength of code in path of shortened links
    LINK_CODE_ALPHABETAcceptable chars of code in path of shortened links
    POSTGRES_USERPostgreSQL User
    POSTGRES_PASSWORDPostgreSQL Password
    POSTGRES_HOSTPostgreSQL Host
    POSTGRES_PORTPostgreSQL Port
    POSTGRES_DATABASEPostgreSQL Database name

💾 Storage

The application supports two possible options for storing links:

  • memory temporary storage in memory
  • postgres persistent storage in PostgreSQL

Once selected, replace <STORAGE> in following commands with one of these values.

🐋 Docker

Run

docker run -it -p 8080:8080 --env-file .env anijack/go-url-shortener --storage <STORAGE>

Server will start on localhost:8080

Run using Docker Compose (with PostgreSQL)

STORAGE=<STORAGE> docker compose up

However, it makes no sense to run application with memory storage type using Docker Compose, since PostgreSQL is launched with it, which is necessary only for postgres storage type.

Build from sources

docker build -t anijack/go-url-shortener .

🔨 Build

make build

🚀 Run

make run STORAGE=<STORAGE>

Server is listening and serving on localhost:8080

âœ”ī¸ Code quality

Run Formatter

make fmt

Run Linter

make lint

golangci-lint is required

Run Tests

make test

Coverage report in coverage.out

# Packages

No description provided by the author
No description provided by the author
Package docs Code generated by swaggo/swag.
No description provided by the author