gateway

package module
v0.0.0-...-bf2a2bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: MIT Imports: 6 Imported by: 0

README

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

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts Options) (iris.Runner, iris.Configurator)

New returns a pair of iris Runner and Configurator to convert the http application to a lambda function using the Apex Gateway. That allows Iris-powered web application to be deployed and ran on host services like Netlify and Amazon AWS.

Usage: app := iris.New() [...routes] runner, configurator := gateway.New(gateway.Options{URLPathParameter: "path"}) app.Run(runner, configurator)

Get the original API Gateway Request object through: req, ok := gateway.GetRequest(ctx.Request().Context())

Types

type Options

type Options struct {
	// When not empty then a URL parameter of that key will be used to route the requests,
	// changes the default Iris Router behavior based on the Request's URI's Path, e.g. "path".
	//
	// This is extremely useful when the deployment allows only one request path to be ran
	// under a particular lambda function.
	//
	// Defaults to empty.
	URLPathParameter string
	// When not empty then a URL parameter of that key will be used
	// to route the requests based on the given method.
	//
	// Defaults to empty.
	URLMethodParameter string
}

Options holds the gateway options. All fields are optional.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL