This application provides a simple solution for shortening URLs.Developed using Golang, it exposes REST services to perform operations related to URL shortner application.The application is integrated with Redis
as the database to store mappings between shortened URLs and their original long URLs.
This application is a learning tool to understand golang
programming concepts.
Below are the features implemented in this sample application, Refer for more:
- Generate a short url with the given original url.
- The short url (or link) should redirect users to original url (link).
- Provide option to create custom short url as given by end user.
- Provide count score metrics for any given short url.
The REST services can be test through cURL calls or, any API Test tool.
Also, a sample front-end applicatin is also written in React to get the feel for complete development for URL shortner application. Clone From Here
- Go
- Redis
- Your Favorite IDE. I have used Visual Studio Code Editor
- URL Shortner Web Application
- Clone the project.
- Redis is installed and the service is up and running.
- Run command from terminal
go build .
to build the project. - Run command from terminal
go run .
to run the application or use your IDE to startmain.go
indebug or non-debug
mode. - Open Browser, Run url
http://localhost:9999/test
to test the server is up and running. - Change/modify the desired port number if you wish. Refer function
GetHttpServerConfig()
at Server Configuration. - Use command
Ctrl+C
orStop
from IDE to shutdown server.
Endpoint Url | HTTP Method | HTTP Response |
---|---|---|
localhost:9999/key/get-key | GET | Get unique key for long url from redis database |
localhost:9999/key/get-all-keys | GET | Get all keys (multiple) from redis database |
localhost:9999/key/add-key | POST | Add or store a redis key into the database |
Sample cURL calls:
- Add Key into Redis
curl --location 'http://127.0.0.1:9999/key/add-key' \
--header 'Content-Type: application/json' \
--data '{
"KeyName" : "this is just a test key"
}'
- Get Key From Redis
curl --location --request GET 'localhost:9999/key/get-key' \
--header 'Content-Type: application/json' \
--data '{
"KeyName" : "MyTestKey"
}'
- Gell All Keys From Redis
curl --location 'localhost:9999/key/get-all-keys'
Endpoint Url | HTTP Method | HTTP Response |
---|---|---|
localhost:9999/test | GET | Server Uptime |
localhost:9999/urls/get-short-url | GET | Get short url |
localhost:9999/urls/create-short-url | POST | Create short url |
localhost:9999/urls/update-short-url | POST | Update short url |
localhost:9999/urls/delete-short-url | GET | Delete short url |
localhost:9999/urls/custom-short-url | POST | Create Custom short url |
localhost:9999/urls/get-click-count | POST | Total Clicks For a short url |
Sample cuRL calls:
- Create Short Url
curl --location 'localhost:9999/urls/create-short-url' \
--header 'Content-Type: application/json' \
--data '{
"longUrl" : "https://go.dev/doc/tutorial/getting-started#code"
}'
- Get Short Url
curl --location --request GET 'http://localhost:9999/urls/get-short-url?longUrl=https%3A%2F%2Fgo.dev%2Fdoc%2Ftutorial%2Fgetting-started%23code' \
--header 'Content-Type: application/json'
- Update Short Url
curl --location 'http://localhost:9999/urls/update-short-url' \
--header 'Content-Type: application/json' \
--data '{
"longUrl" : "https://go.dev/doc/tutorial/getting-started#code"
}'
- Delete Short Url
curl --location --request GET 'http://localhost:9999/urls/delete-short-url?longUrl=https%3A%2F%2Fgo.dev%2Fdoc%2Ftutorial%2Fgetting-started%23code' \
--header 'Content-Type: application/json'
- Create Custom Short Url
curl --location 'http://127.0.0.1:9999/urls/custom-short-url' \
--header 'Content-Type: application/json' \
--data '{
"longUrl":"https://training.linuxfoundation.org/",
"shortUrl":"1L9zOa2"
}'
- Get Total Count of Clicks for a Short Url
curl --location 'localhost:9999/urls/get-click-count' \
--header 'Content-Type: application/json' \
--data '{
"shortUrlId": "1L9zO9P"
}'
- Command to check
Go
is installed on your machine.
go version
- Initialize/Create a
Go
project or module.Enable dependency tracking for your project.
go mod init <my-project-name>
Note: Command will create go.mod
file in project directory. The go.mod
file provides information of:
name
of your project- current
Go
version in use - details of
libraries (project's dependencies)
Example to create module/project from Command line client:
mkdir go-for-url-shortner
cd go-for-url-shortner
go mod init github.com/KumarVariable/go-for-url-shortner
touch main.go
- How to build a
Go
application ?
go build main.go
- Command will compile packages and dependencies in a specific file (ex: main.go)
- Command generates an executable file in the current directory, (on Unix, it's typically named after the directory; on Windows, it will have an .exe suffix).
go build .
- Here
.
(dot) represents a current directory. - Command to compile the package that is in the current directory, along with any dependent files in that package.
- Command generates an executable file if the package is named as
main
, otherwise it compiles the package and produces a package archive.
- How to run a
Go
application ?
go run main.go
- Command to tell
Go
compiler to run and runmain.go
(a specific file). This command is helpful to quickly test a single file.
go run .
- Here
.
(dot) represents a current directory. - Command tells
Go
to compile and run the entire package in the current directory, not just a single file. - Command also compiles multiple
.go
files which are part of package.
- How to run executable created through command at point #3 ?
- Locate the executable and run
./go-for-url-shortner
from the terminal
./go-for-url-shortner
- How to tidy/clean up unnecessary dependencies from your
Go
project ?
go mod tidy
To start redis-client or Redis command line interface. Redis client is available in Redis package that will be installed when we install Redis
on our machine.
redis-cli
- Use
CONFIG GET databases
command to know the number of databases. - Use
INFO keyspace
command to list the databases that contains keys.
- Use
SELECT <index>
command to select the database based on zero-index. Default is 0. - Use
FLUSHDB
command to clear currently active database. - Use
FLUSHALL
command to clear all the existing database. - Use
SCAN <cursor>
command to iterate the set of keys into selected Redis database.
- Use
SET <key-name> <key-value>
command to create a key with value in redis database. - Use
GET <key-name>
command to get the stored value for corresponding key in redis database.
- Use
KEYS *
command to list all keys stored in redis database. - Use
SCAN <cursor>
command as an alternative forKEYS
command.Better option.
Happy Learning !