Sign inSign up

mengluo668/urlshorter

By mengluo668

•Updated almost 8 years ago

Urlshorter is a RESTful web service used to help organizations to achieve short URLs.

Image
0

303

mengluo668/urlshorter repository overview

⁠Supported tags

1.0.1, latest 1.0(does not support redis re-connect)

⁠Prerequisites

Because of urlshorter use redis as its persist store, so a redis server should be available before run urlshorter.

NOTE: urlshorter is a fail-fast image, so the startup order of redis and urlshorter is not important, urlshorter can detect redis automatically.

⁠How to use this image

⁠Standalone container

docker run -d -p 8080:8080 mengluo668/urlshorter:1.0

⁠Compose or stack deploy
version: "3.7"
services:
  redis:
    image: redis:5.0.0
  urlshorter:
    image: mengluo668/urlshorter:1.0
    environment:
      - SU_ERROR_URL=http://www.baidu.com
    ports:
      - 8080:8080

⁠APIs

⁠Generate a short url
// Request
POST /api/v1/generation HTTP/1.1
Host: urlshorter.example.com
Content-Type: application/json
Body: 
{
"shortBaseUrl": "http://www.example.com",
"originalUrl":"http://test.example.com?hello=world&author=wenbo&license=ISC"
}

// Response
Content-Type: application/json
Body: http://www.example.com/fzjxXVMnV
⁠Redirect to original url*
// Request
GET /api/v1/code/fzjxXVMnV HTTP/1.1
Host: urlshorter.example.com

// Response (redirect to the orignal URL with 3xx status code)
301 http://test.example.com?hello=world&author=wenbo&license=ISC

⁠Environment

The urlshorter allows you customize its behaviors based on environment. Available envs are:

  1. SU_PORT The port of service, default: 8080
  2. SU_PREFIX_CODE The prefix of redis key for generated code, default: SU:CODE:
  3. SU_PREFIX_ORI The prefix of redis key for original url, default: SU:ORI:
  4. SU_REDIS_HOST The host of redis, default: redis
  5. SU_REDIS_PORT The port of redis, default: 6379
  6. SU_REDIS_OPTIONS The options of redis, default: '{}'. the value of this environment must be a string and can cast to json object.
  7. SU_ERROR_URL The target error page if short url is not correct, default is undefined

⁠ERROR Handler

  1. Q1: If the code is not exist, how the urlshorter deal with it?

A1: If the ERROR_URI environment is set, the urlshorter will redirect to the ERROR_URI, otherwise return a error in JSON(content-type: application-json)

  1. Q2: If users call /api/v1/generation⁠, how many short url are generated?

A2: Only 1 short url will be generate, and the urlshorter will ignore the second request!

⁠Test Client

A test client for urlshorter and docker-compose.yaml file can be found here⁠

Tag summary

Content type

Image

Digest

Size

22.6 MB

Last updated

almost 8 years ago

docker pull mengluo668/urlshorter