An open API service indexing awesome lists of open source software.

https://github.com/iris-contrib/gateway

Run Iris powered web applications through AWS Lambda & API Gateway
https://github.com/iris-contrib/gateway

aws-iris go golang iris iris-lambda-function iris-serverless lambda-iris netlify netlify-functions serverless

Last synced: 3 months ago
JSON representation

Run Iris powered web applications through AWS Lambda & API Gateway

Awesome Lists containing this project

README

          

# Gateway

[![build status](https://img.shields.io/github/actions/workflow/status/iris-contrib/gateway/ci.yml?branch=master&style=for-the-badge)](https://github.com/iris-contrib/gateway/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/iris-contrib/gateway) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/iris-contrib/gateway)

Gateway is a simple [iris.Runner](https://github.com/kataras/iris/blob/master/iris.go#L662). It runs Iris Web Applications through AWS Lambda & API Gateway aka **Serverless**. This includes the [Netlify functions (free and paid)](https://docs.netlify.com/functions/overview/) too. Thanks to [apex/gateway](https://github.com/apex/gateway).

## Installation

The only requirement is the [Go Programming Language](https://golang.org/dl).

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

## Getting Started

Simply as:

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

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

### Netlify

*1.* Create an account on [netlify.com](https://app.netlify.com/signup)

*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:

```go
// 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:

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

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

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

**Makefile**

```sh
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](LICENSE).