Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aden-q/short-url

A URL shortener web application built in Go
https://github.com/aden-q/short-url

cache docker docker-compose go golang mysql redis urlshortener

Last synced: about 1 month ago
JSON representation

A URL shortener web application built in Go

Awesome Lists containing this project

README

        

# Short URL

A fully functional URL shortener using MySQL as the storage server and Redis as the cache server.

The current components include:

+ a web server for URL shortening and redirection
+ regex to verify validity of a URL
+ Base62 encoding to compress a long URL
+ an external Redis cache server
+ a local dev enviroment setup with `docker-compose`
+ automatic configuration management with a single `.env` file
+ logging
+ docs generated by swagger
+ request timeout control
+ in-memory cache
+ [TODO] integration test running on CI
+ [TODO] IP based rate limiting

## Docs

Docs are generated by [gin-swagger](https://github.com/swaggo/gin-swagger). To access the docs, open your browser and go to the following link after launching the web server on your localhost.

```text
http://127.0.0.1:8000/swagger/index.html
```

## Usages

We provide two API endpoints: one for URL shortening and another for URL redirection.

### URL shortening

`POST /api/v1/data/shorten`

+ Request parameter: {longURL: string}
+ Rreturn: shortURL

Example usage:

```bash
$ curl -iX POST 'http://localhost:8080/api/v1/data/shorten?longURL=www.google.com'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Fri, 01 Dec 2023 05:10:33 GMT
Content-Length: 18

{"shortURL":"aA4"}
```

### URL redirection

`shortURL`: The request URI path

`GET /api/v1/:shortURL`

+ Request parameter: no, using URI path binding instead
+ Return: longURL for HTTP redirection

Example usage:

```bash
$ curl -i 'http://localhost:8080/api/v1/aA4'
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: /api/v1/www.google.com
Date: Fri, 01 Dec 2023 05:11:54 GMT
Content-Length: 57

Moved Permanently.
```

## Command Runner

We use the command runner [just](https://github.com/casey/just) as an alternative to makefile. Execute `just help` to access a complete list of available commands. If you do not have `just` installed, check the [justfile](./justfile) for all receipts.

## Build and Run Locally

We use `docker-compose` to set up local development pretty easily. It exposes a web server forwarded to host port 8080, a MySQL server forwarded to host port 3306, and a Redis server forwarded to host port 6379. To launch, simply run:

```bash
$ docker-compose up -d
```

Then you are ready to go.

To build a binary, run:

```bash
$ just build
```

To connect to the mysql server, run:

```bash
$ just mysql
```

To connect to the redis server, run:

```bash
$ just redis
```

## License

[MIT License](./LICENSE)