GithubHelp home page GithubHelp logo

guoyu07 / golang-serverless-restapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from qiangxue/golang-serverless-restapi

0.0 0.0 0.0 8 KB

A starter kit for serverless REST API in Golang

License: MIT License

Go 100.00%

golang-serverless-restapi's Introduction

Serverless REST API in Go

This is a starter kit for creating a serverless REST API using Go and apex.

Installation

Install apex and the Go packages as instructed below:

# install apex if you don't have it yet
curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sudo sh

# download this package
go get github.com/qiangxue/golang-serverless-restapi

# download the go-apex package
go get github.com/apex/go-apex

Deploying Lambda

First, follow the apex documentation to set up the AWS credentials needed for deploying Lambdas using apex.

Then, modify the project.json file by inserting the correct AWS role and profile values.

Run the following command to build and deploy the Lambda:

apex deploy --region us-east-1

And use the AWS console or the following command to verify that the Lambda is successfully deployed:

apex invoke apis

Configuring AWS API Gateway

  1. Log into the AWS console and switch to the API Gateway page. On that page, choose "Create new API" and enter the API name as "hello" (or any other name you prefer).
  2. In the "Resources" tab of the "hello" API, click on the "Actions" dropdown button and select "Create Resource". In the "New Child Resource" page, select "Configure as proxy resource", and then click on the "Create Resource" button.
  3. In the "/{proxy+} - ANY - Setup" page, choose "Lambda Region" as us-east-1 (or the actual AWS region that you used to deploy the Lambda function) and enter the "Lambda Function" name as rest_apis. Click the "Save" button to complete the proxy resource setup.
  4. Click on the "Actions" dropdown button and select "Deploy API". In the popup window, choose [New Stage] in the "Deployment stage" dropdown list, and enter prod in the "Stage name" input field. Click on the "Deploy" button to complete the deployment.
  5. At this point, you should be redirected to a page showing the deployment information about the "hello" API. You should see an "Invoke URL" on the page. The URL may look like "https://bm7empvth7.execute-api.us-east-1.amazonaws.com/prod". This is the base URL that everyone can use to hit our APIs.

Trying it Out

Run the following commands to verify the API is accessible and working as expected:

# replace InvokeURL with the actual Invoke URL found in Step 5
> curl InvokeURL/foo
hello

> curl InvokeURL/bar?hello=world
GET /bar?hello=world

In the AWS console, locate the Lambda function named rest_apis and check its monitoring result to verify that the Lambda was invoked.

What's Next

In the above demo, we used the standard HTTP ServerMux to wire up the HTTP handlers for handling different API endpoints. You may replace it with your favorite third-party HTTP routers (e.g. ozzo-routing, echo, gin). This can be done easily by modifying the functions/apis/api.go file.

Because we are using AWS API Gateway as a proxy to invoke the Lambda, we may implement more API endpoints without the need of reconfiguring the Gateway. That is, each time we make changes to our REST API project, we only need to run apex deploy to deploy it to AWS. We do not need to reconfigure AWS API Gateway.

golang-serverless-restapi's People

Contributors

qiangxue 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.