This project contains of 1 microservice that aim to shorten urls. This microservice provides 3 endpoint:
- POST /shorten
- GET /:shortcode
- GET /:shortcode/stats
The data will be stored to MongoDB. Tech Stack: Golang, echo, MongoDB, docker
Please clone this repository and then enter to the project directory.
The config file is located at:
- shorty/common/configGlobal.yaml
You can change the values according to your configuration.
To run this service using docker, please select datastore:27017 as db_host, or according to your local setting in config file that is written at the point C.
local_conf:
db_host_test: localhost:32768
db_host: datastore:27017 # docker use
db_host: localhost:27017 # without using docker
docker-compose -f docker-compose-local.yml build
docker-compose -f docker-compose-local.yml up
Below are the sample requests and expected responses for each microservice:
Name | Value |
---|---|
endpoint | localhost:9701/v1/shorten |
curl --location --request POST 'localhost:9701/v1/shorten' \
--header 'Content-Type: application/json' \
--data-raw '{
"url" : "https://en.wikipedia.org/",
"shortcode": "Mcdp4W"
}'
201 Created
Content-Type: "application/json"
{
"shortcode": "Mcdp4W"
}
curl --location --request POST 'localhost:9701/v1/shorten' \
--header 'Content-Type: application/json' \
--data-raw '{
"url" : "https://en.wikipedia.org/",
"shortcode": "Mcdp4W1"
}'
422 Unprocessable Entity
Content-Type: "application/json"
{
"description": "The shortcode fails to meet the following regexp: ^[0-9a-zA-Z_]{6}$."
}
Name | Value |
---|---|
endpoint | localhost:9701/v1/:shortcode |
curl --location --request GET 'localhost:9701/v1/Mcdp4W' \
--header 'Content-Type: application/json'
In this case it will be redirected to the https://en.wikipedia.org/
curl --location --request GET 'localhost:9701/v1/111111' \
--header 'Content-Type: application/json'
404 Not Found
Content-Type: "application/json"
{
"description": "The shortcode cannot be found in the system"
}
Name | Value |
---|---|
endpoint | localhost:9701/v1/:shortcode/stats |
curl --location --request GET 'localhost:9701/v1/Mcdp4W/stats' \
--header 'Content-Type: application/json'
200 OK
Content-Type: "application/json"
{
"startDate": "2021-04-25T06:30:43+0700",
"lastSeenDate": "2021-04-25T06:33:09+0700",
"redirectCount": 2
}
curl --location --request GET 'localhost:9701/v1/14/stats' \
--header 'Content-Type: application/json'
404 Not Found
Content-Type: "application/json"
{
"description": "The shortcode cannot be found in the system"
}
Firstly we have to create database that is defined in shorty/common/configGlobal.yaml
To run the test without using docker, please select localhost:27017 as db_host, or according to your local setting.
local_conf:
db_host_test: localhost:32768
db_host: datastore:27017 # docker use
db_host: localhost:27017 # without using docker
These are the steps to run unit test for each microservice:
APP_ENV=local go test ./controllers/v1 -tags=unit_create_shorten_url -v
APP_ENV=local go test ./controllers/v1 -tags=unit_get_url -v
APP_ENV=local go test ./controllers/v1 -tags=unit_get_url_stat -v