gateway

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2020 License: MIT Imports: 11 Imported by: 0

README

Usage

基于github.com/diemus/gateway适配而来,用于golang版SCF接入gin框架或者普通http框架

http

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/diemus/gateway"
)

func Example() {
	http.HandleFunc("/", hello)
	log.Fatal(gateway.ListenAndServe(":3000", nil))
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello World from Go")
}

gin

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/diemus/gateway"
	"github.com/gin-gonic/gin"
)

func helloHandler(c *gin.Context) {
	name := c.Param("name")
	c.String(http.StatusOK, "Hello %s", name)
}

func welcomeHandler(c *gin.Context) {
	c.String(http.StatusOK, "Hello World from Go")
}

func rootHandler(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"text": "Welcome to gin lambda server.",
	})
}

func routerEngine() *gin.Engine {
	// set server mode
	gin.SetMode(gin.DebugMode)

	r := gin.New()

	// Global middleware
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	r.GET("/welcome", welcomeHandler)
	r.GET("/user/:name", helloHandler)
	r.GET("/", rootHandler)

	return r
}

func main() {
	addr := ":" + os.Getenv("PORT")     // 此处addr无实际意义,因为请求是通过事件传入的
	log.Fatal(gateway.ListenAndServe(addr, routerEngine()))
}

GoDoc

Documentation

Overview

Package gateway provides a drop-in replacement for net/http.ListenAndServe for use in AWS Lambda & API Gateway.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/diemus/gateway"
)

func main() {
	http.HandleFunc("/", hello)
	log.Fatal(gateway.ListenAndServe(":3000", nil))
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello World from Go")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, h http.Handler) error

ListenAndServe is a drop-in replacement for http.ListenAndServe for use within AWS Lambda.

ListenAndServe always returns a non-nil error.

func NewRequest

func NewRequest(ctx context.Context, e events.APIGatewayRequest) (*http.Request, error)

NewRequest returns a new http.Request from the given Lambda event.

func RequestContext

func RequestContext(ctx context.Context) (events.APIGatewayRequestContext, bool)

RequestContext returns the APIGatewayProxyRequestContext value stored in ctx.

Types

type ResponseWriter

type ResponseWriter struct {
	// contains filtered or unexported fields
}

ResponseWriter implements the http.ResponseWriter interface in order to support the API Gateway Lambda HTTP "protocol".

func NewResponse

func NewResponse() *ResponseWriter

NewResponse returns a new response writer to capture http output.

func (*ResponseWriter) CloseNotify

func (w *ResponseWriter) CloseNotify() <-chan bool

CloseNotify notify when the response is closed

func (*ResponseWriter) End

End the request.

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

Header implementation.

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(b []byte) (int, error)

Write implementation.

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

WriteHeader implementation.

Jump to

Keyboard shortcuts

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