# README
Go URL Shortener
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
-
Clone this repository
git clone https://github.com/anijackich/go-url-shortener
-
Go to
go-url-shortener
directorycd go-url-shortener
-
Rename
.env.example
to.env
mv .env.example .env
-
Set environment variables
Variable Description HOST
Host serving the API PORT
Port serving the API DOMAIN
Base domain for shortened links LINK_CODE_LENGTH
Length of code in path of shortened links LINK_CODE_ALPHABET
Acceptable chars of code in path of shortened links POSTGRES_USER
PostgreSQL User POSTGRES_PASSWORD
PostgreSQL Password POSTGRES_HOST
PostgreSQL Host POSTGRES_PORT
PostgreSQL Port POSTGRES_DATABASE
PostgreSQL Database name
đž Storage
The application supports two possible options for storing links:
memory
temporary storage in memorypostgres
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