This project is a simple URL shortener built using Golang, Gin, and GORM. It provides RESTful APIs to create, update, delete, and retrieve shortened URLs. The project uses MySQL (or any GORM-supported database) to store the original and shortened URLs.
- Shorten a URL with a randomly generated code.
- Retrieve all shortened URLs.
- Update or delete existing shortened URLs.
- Redirect to the original URL using the shortened URL.
Before you begin, ensure you have the following installed on your machine:
- Golang`
- MySQL (or any GORM-compatible database)
- Git
- Postman or any API testing tool (for testing the API)
-
Clone the repository:
git clone https://github.com/Rajar01/golang-url-shortener.git cd golang-url-shortener
-
Install dependencies:
go mod tidy
-
Create a database with name "golang_url_shortener"
-
Run database migrations and start the server: This will create the necessary tables in your database.
go run main.go
-
The server will be running at
http://127.0.0.1:8080
.
- Endpoint:
/shorted-links
- Method:
POST
- Request Body:
{ "original_url": "https://example.com" }
- Response:
{}
- Endpoint:
/shorted-links
- Method:
GET
- Response:
{ "short_links": [ { "ID": 1, "OriginalURL": "https://example.com", "ShortenedURL": "http://localhost:8080/abc123", "CreatedAt": "2024-10-02T16:17:46.268+08:00", "UpdatedAt": "2024-10-02T16:20:18.851+08:00" } ] }
- Endpoint:
/shorted-links/:id
- Method:
GET
- Response:
{ "short_links": [ { "ID": 1, "OriginalURL": "https://example.com", "ShortenedURL": "http://localhost:8080/abc123", "CreatedAt": "2024-10-02T16:17:46.268+08:00", "UpdatedAt": "2024-10-02T16:20:18.851+08:00" } ] }
- Endpoint:
/shorted-links/:id
- Method:
PUT
- Request Body:
{ "original_url": "https://newexample.com" }
- Response:
200 OK
- Endpoint:
/shorted-links/:id
- Method:
DELETE
- Response:
200 OK
- Endpoint:
/:shorted-link
- Method:
GET
- Response: HTTP 301 Redirect to the original URL.
This project uses GORM for database operations. You can change the database connection in the database.Connect()
function to use any other database supported by GORM, such as PostgreSQL or SQLite.
Use Postman or Curl to test the API endpoints listed above. You can shorten URLs and retrieve the shortened versions using the API, then test the redirect functionality by accessing the shortened URL in your browser.
For more details and to contribute, visit the repository: Golang URL Shortener.
This project is licensed under the MIT License. See the LICENSE file for more details.
This project is maintained by Rajar01.