Categorygithub.com/diemus/gateway
repositorypackage
1.1.2
Repository: https://github.com/diemus/gateway.git
Documentation: pkg.go.dev

# 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