github.com/iris-contrib/gateway

Run Iris powered web applications through AWS Lambda & API Gateway


Keywords
aws-iris, go, golang, iris, iris-lambda-function, iris-serverless, lambda-iris, netlify, netlify-functions, serverless
License
MIT
Install
go get github.com/iris-contrib/gateway

Documentation

Gateway

build status report card godocs

Gateway is a simple iris.Runner. It runs Iris Web Applications through AWS Lambda & API Gateway aka Serverless. This includes the Netlify functions (free and paid) too. Thanks to apex/gateway.

Installation

The only requirement is the Go Programming Language.

$ go get github.com/iris-contrib/gateway@master

Getting Started

Simply as:

app := iris.New()
// [...]

runner, configurator := gateway.New(gateway.Options{})
app.Run(runner, configurator)

Netlify

1. Create an account on netlify.com

2. Link a new website with a repository (GitHub or GitLab, public or private)

3. Add a main.go in the root of that repository:

// Read and Write JSON only.
package main

func main() {
    app := iris.New()
    app.OnErrorCode(iris.StatusNotFound, notFound)

    app.Get("/", index)
    app.Get("/ping", status)

    // IMPORTANT:
    runner, configurator := gateway.New(gateway.Options{
        URLPathParameter: "path",
    })
    app.Run(runner, configurator)
}

func notFound(ctx iris.Context){
    code := ctx.GetStatusCode()
    msg := iris.StatusText(code)
    if err := ctx.GetErr(); err!=nil{
        msg = err.Error(),
    }

    ctx.JSON(iris.Map{
        "Message": msg,
        "Code": code,
    })
}

func index(ctx iris.Context) {
    var req map[string]interface{}
    ctx.ReadJSON(req)
    ctx.JSON(req)
}

func status(ctx iris.Context) {
    ctx.JSON(iris.Map{"Message": "OK"})
}

4. Create or open the netlify.toml file, edit its contents so they look like the following:

[build]
  publish = "public"
  command = "make build"
  functions = "./functions"
  

[build.environment]
  GO_VERSION = "1.14.7"
  GIMME_GO_VERSION = "1.14.7"
  GO_IMPORT_PATH = "github.com/your_username/your_repo"

[[redirects]]
   from = "/api/*"
   to = '/.netlify/functions/my_iris_function/:splat'
   status = 200

Makefile

build:
	go build -o ./functions/my_iris_function
	chmod +x ./functions/my_iris_function

5. Use git push to deploy to Netlify.

The serverless Iris application of will be reachable through your_site.com/api, e.g. https://example.com/api?path=ping. Have fun!

License

This software is licensed under the MIT License.