This is a simple URL shortener API built using Go and Gin framework. It uses GORM as the ORM and a SQLite database to store the shortened URLs.
- Docker
-
Clone the repository and navigate to the project directory.
git clone https://github.com/C-m3-Codin/url_shortner.git cd url_shortner
-
Build the Docker image using the provided Dockerfile.
docker build -t url-shortener .
-
Run the Docker container using the provided docker-compose.yml file.
docker-compose up -d
The API provides three endpoints:
POST /user/register
- Registers a new user.POST /token
- Generates an auth token.POST /create
- Creates a new shortened URL.GET /:shortenedUrl
- Redirects to the original URL associated with the given shortened URL.
To register a new user, send a JSON POST request to the /user/register
endpoint with the following format:
{
"name":"Cyril",
"username":"c1337_2",
"email":"c@c_2.com",
"password":"password@123"
}
To create a new shortened URL, send a JSON POST request to the /create
endpoint with the following format:
{
"original_url": "https://www.google.com"
}
To redirect to the original URL associated with a shortened URL, send a GET request to the /:shortenedUrl
endpoint with the shortened URL as a parameter. For example, if the shortened URL is abc123
, the request URL would be http://localhost:8000/abc123
.
The main Go code is contained in main.go
. It creates a database connection and defines the endpoints for the API.
The services
package contains the code for creating the database connection.
The ShortLink
struct represents a shortened URL and is defined in main.go
. The generateShortenedURL
function generates a random 8-character string to use as the shortened URL.
This project is licensed under the MIT License. See the LICENSE
file for details.