This is a simple API in Golang that accepts a URL in the query parameter and returns a shortened link that will redirect the user to the original URL.
To run the project, you need to have the following tools installed on your system:
make
: The build automation tool.Docker
: To run the container with the Postgres database.
-
Clone the repository to your local machine:
git clone https://github.com/your-username/shortener-api.git cd shortener-api
-
Install Go in your machine, and follow the instruction in the following url:
https://go.dev
-
Install Make in your machine, and follow the instruction in the following url:
https://www.gnu.org/software/make/
-
Initialize the Postgres database container:
make postgresinit
-
Add environment variables below to your environment:
# Your base URL BASE_URL= # Your url connection string DATABASE_URL=
-
Initialize the Postgres database container:
make createdb
-
Run the database migrations:
make migrateup
-
Finally, run the API locally:
make run
You can use curl or any other HTTP client to interact with the API.
To shorten a URL, make a POST request with the url query parameter. The API will return a shortened link.
curl -X POST "http://localhost:8000/?url=https://www.example.com"
Sample Response:
{
"url": "http://localhost:8080/EawUrx"
}
To be redirected to the original URL, simply open the shortened URL in your browser
http://localhost:8000/EawUrx
This will redirect you to https://www.example.com.