Coder Social home page Coder Social logo

url-shortener-go's Introduction

url-shortener-go

Description

url-shortener-go is a service to shorten URL using AWS Lambda and DynamoDB

sequence

Production

prerequisites

You have to prepare credentials with proper policies.

And,

  • install aws-cli
  • install aws-sam-cli. Docker is also required. Follow the instruction here.
  • install direnv
  • install saw
    • you can watch CloudWatch logs on your terminal
  • set environment variables to .envrc.sample and remove .sample.
    • LINK_TABLE is DynamoDB table name where maps of your original URLs and shortend resource are gonna be saved.
    • REGION is where your project is gonna be deployed.
    • STACK_BUCKET is S3 bucket name for artifacts of SAM and should be unique globally.

deploy

$ dep ensure                       # to resolve dependency
$ aws s3 mb "s3://${STACK_BUCKET}" # for artifacts of SAM
$ make deploy
# Your endpoint is gonna be pirinted.

# You can change url for your own.
$ curl -X POST https://yyyyyyyyyy.execute-api.[your region].amazonaws.com/Prod/links -d '{"url":"http://toshi0607.com/"}'
xxxxxxxxx

then you can access https://yyyyyyyyyy.execute-api.[your region].amazonaws.com/Prod/links/xxxxxxxxx and it'll be redirected to the original URL.

log

Deploy is required before checking behavior.

$ saw groups
/aws/lambda/url-shortener-lambda-go-Redirect-XXXXXXXXXXXX
/aws/lambda/url-shortener-lambda-go-Shorten-XXXXXXXXXXXX

$ saw watch /aws/lambda/url-shortener-lambda-go-Redirect-XXXXXXXXXXXX &
$ saw watch /aws/lambda/url-shortener-lambda-go-Shorten-XXXXXXXXXXXX &

# open another window
$ curl -X POST https://yyyyyyyyyy.execute-api.[your region].amazonaws.com/Prod/links -d '{"url":"http://toshi0607.com/"}'

Local

You can test AWS Lambda & API Gateway locally with AWS SAM (Serverless Application Model).

hosting

$ sam local start-api # you have to use DynamoDB

# You can change url for your own.
$ curl -X POST http://127.0.0.1:3000/links -d '{"url":"http://toshi0607.com/"'}
xxx

then you can access http://127.0.0.1:3000/links/xxxxxxxxx and it'll be redirected to the original URL.

testing

DynamoDB local is useful as well.

You can try to use the DynamoDB local in test as below.

$ docker pull amazon/dynamodb-local
$ docker run -p 8000:8000 amazon/dynamodb-local
$ make test

Articles (Japanese)

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.