Skip to content

mathisve/serverless-link-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

serverless-link-shortener

build

A stateful serverless API built on top of AWS Lambda and Upstash Redis.

Special thanks to Upstash for making projects like this possible!

About:

The codebase consists of 2 functions:

  • new to add links to the database, returns the shortened hash/code
  • get to retrieve the links given the hash/code, returns HTML

Code snippet:

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {
    // extract hash from API Gateway headers
    hash := request.Headers["hash"]
    
    // get from Redis compatible Upstash database
    val, err := client.Get(ctx, hash).Result()
    
    if err == redis.Nil {
        // hash not found
        return Response{
            StatusCode: 404,
            Body:       notFoundResponse,
            Headers:    headers,
        }, nil
    }

    // did find hash
    return Response{
        StatusCode: 200,
        Body:       fmt.Sprintf(redirectResponse, val),
        Headers:    headers,
    }, nil
}

Video:

If you prefer to have me explain it to you, you're in luck! Watch the video here! (or click the thumbnail!)

About

100% serverless link shortener built on AWS Lambda and Upstash

Topics

Resources

Stars

Watchers

Forks

Languages