https://github.com/anijackich/go-url-shortener
Simple URL shortener written in Go
https://github.com/anijackich/go-url-shortener
docker gin github-actions go gorm rest-api swagger
Last synced: 3 months ago
JSON representation
Simple URL shortener written in Go
- Host: GitHub
- URL: https://github.com/anijackich/go-url-shortener
- Owner: anijackich
- License: mit
- Created: 2025-02-07T15:26:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-01T22:16:12.000Z (about 1 year ago)
- Last Synced: 2025-04-01T23:24:55.623Z (about 1 year ago)
- Topics: docker, gin, github-actions, go, gorm, rest-api, swagger
- Language: Go
- Homepage: https://ozon.anjk.ru/swagger/index.html
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go URL Shortener
[](https://ozon.tech/)
[](https://github.com/anijackich/go-url-shortener/actions/workflows/ci.yml)
[](https://github.com/anijackich/go-url-shortener)
[](https://goreportcard.com/report/github.com/anijackich/go-url-shortener)
Simple Go API application for URL shortener service.
## ⨠Features
- [x] Convert long URL to a short link
- [x] Expand long URL from a short link
- [x] In-memory storage
- [x] PostgreSQL storage
## đšī¸ Play with Demo
The application is deployed on [ozon.anjk.ru](https://ozon.anjk.ru/api/v1), and you can try to interact with it
through [Swagger](https://ozon.anjk.ru/swagger/index.html).
### `POST` `/api/v1/shorten`
#### Shorten link
> Converts a long URL to a short link
#### Request body
```json
{
"long_url": "https://some-very-very.com/very/very/long/url"
}
```
#### Response body
```json
{
"short_url": "https://example.com/a1b2c3d4e5"
}
```
### `GET` `/api/v1/expand?u=`
#### Expand link
> Returns a long URL for the specified short link
#### Request params
`u`: `https://example.com/a1b2c3d4e5`
#### Response body
```json
{
"long_url": "https://some-very-very.com/very/very/long/url"
}
```
## âŦī¸ Clone & Setup
1. Clone this repository
```shell
git clone https://github.com/anijackich/go-url-shortener
```
2. Go to `go-url-shortener` directory
```shell
cd go-url-shortener
```
3. Rename `.env.example` to `.env`
```shell
mv .env.example .env
```
4. 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 memory
- `postgres` persistent storage in PostgreSQL
Once selected, replace `` in following commands with one of these values.
## đ Docker
### Run
```shell
docker run -it -p 8080:8080 --env-file .env anijack/go-url-shortener --storage
```
Server will start on `localhost:8080`
### Run using Docker Compose (with PostgreSQL)
```shell
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
```shell
docker build -t anijack/go-url-shortener .
```
## đ¨ Build
```shell
make build
```
## đ Run
```shell
make run STORAGE=
```
Server is listening and serving on `localhost:8080`
## âī¸ Code quality
### Run Formatter
```shell
make fmt
```
### Run Linter
```shell
make lint
```
[golangci-lint](https://github.com/golangci/golangci-lint) is required
### Run Tests
```shell
make test
```
Coverage report in `coverage.out`