Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/aden-q/short-url
- Owner: Aden-Q
- License: mit
- Created: 2023-11-28T05:28:51.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-01T17:14:45.000Z (4 months ago)
- Last Synced: 2024-11-16T00:38:37.081Z (2 months ago)
- Topics: cache, docker, docker-compose, go, golang, mysql, redis, urlshortener
- Language: Go
- Homepage:
- Size: 120 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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: shortURLExample 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 redirectionExample 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: 57Moved 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)