GithubHelp home page GithubHelp logo

shorty's Introduction

A. Description

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

B. How to Start

Please clone this repository and then enter to the project directory.

C. Configuration

The config file is located at:

  • shorty/common/configGlobal.yaml

You can change the values according to your configuration.

D. Build and Run Project

Check Config File

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

Build Project

docker-compose -f docker-compose-local.yml build

Run Project

docker-compose -f docker-compose-local.yml up

E. Consume Service

Below are the sample requests and expected responses for each microservice:

E.1. POST /shorten

Name Value
endpoint localhost:9701/v1/shorten

Positive Scenario 1 - Request

curl --location --request POST 'localhost:9701/v1/shorten' \
--header 'Content-Type: application/json' \
--data-raw '{
    "url" : "https://en.wikipedia.org/",
    "shortcode": "Mcdp4W"
}'

Positive Scenario 1 - Response

201 Created
Content-Type: "application/json"

{
    "shortcode": "Mcdp4W"
}

Negative Scenario 1 - Request

curl --location --request POST 'localhost:9701/v1/shorten' \
--header 'Content-Type: application/json' \
--data-raw '{
    "url" : "https://en.wikipedia.org/",
    "shortcode": "Mcdp4W1"
}'

Negative Scenario 1 - Response

422 Unprocessable Entity
Content-Type: "application/json"

{
    "description": "The shortcode fails to meet the following regexp: ^[0-9a-zA-Z_]{6}$."
}

E.2. GET /:shortcode

Name Value
endpoint localhost:9701/v1/:shortcode

Positive Scenario 1 - Request

curl --location --request GET 'localhost:9701/v1/Mcdp4W' \
--header 'Content-Type: application/json'

Positive Scenario 1 - Response

In this case it will be redirected to the https://en.wikipedia.org/ 

Negative Scenario 1 - Request

curl --location --request GET 'localhost:9701/v1/111111' \
--header 'Content-Type: application/json'

Negative Scenario 1 - Response

404 Not Found

Content-Type: "application/json"

{
    "description": "The shortcode cannot be found in the system"
}

E.3. GET /:shortcode/stats

Name Value
endpoint localhost:9701/v1/:shortcode/stats

Positive Scenario 1 - Request

curl --location --request GET 'localhost:9701/v1/Mcdp4W/stats' \
--header 'Content-Type: application/json'

Positive Scenario 1 - Response

200 OK
Content-Type: "application/json"

{
    "startDate": "2021-04-25T06:30:43+0700",
    "lastSeenDate": "2021-04-25T06:33:09+0700",
    "redirectCount": 2
}

Negative Scenario 1 - Request

curl --location --request GET 'localhost:9701/v1/14/stats' \
--header 'Content-Type: application/json'

Negative Scenario 1 - Response

404 Not Found

Content-Type: "application/json"

{
    "description": "The shortcode cannot be found in the system"   
}

F. Unit Test

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

shorty's People

Contributors

kenanya avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.