serverlessplus

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

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

Go to latest
Published: Apr 24, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

README

Serverless + Go

简介

serverlessplus 是一个简单易用的工具,它可以帮助你将现有的 beego / go gin 等框架构建的应用借助 API 网关 迁移到 腾讯云无服务云函数(Tencent Cloud Serverless Cloud Function)上。

开始使用

$ go get github.com/serverlessplus/go

假设有如下 go gin 应用:

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	r.GET("/go-gin-example", func(c *gin.Context) {
		c.Data(200, "text/html", []byte("hello world"))
	})
	r.Run()
}

进行如下简单修改, 即可迁移到腾讯云无服务云函数(Tencent Cloud Serverless Cloud Function)上

  • 指定 HTTP 服务监听的端口
  • r.Run 替换为 net.Listenhttp.Serve
  • 初始化 Handler, 指定端口及需要进行 base64 编码的 MIME 类型
package main

import (
	"context"
	"fmt"
	"net"
	"net/http"

	"github.com/gin-gonic/gin"
	serverlessplus "github.com/serverlessplus/go"
	"github.com/tencentyun/scf-go-lib/cloudfunction"
)

const (
	port = 1216
)

var handler *serverlessplus.Handler

func init() {
	// start your server
	r := gin.Default()
	r.GET("/go-gin-example", func(c *gin.Context) {
		c.Data(200, "text/html", []byte("hello world"))
	})
	l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", serverlessplus.Host, port))
	if err != nil {
		fmt.Printf("failed to listen on port %d: %v\n", port, err)
		// panic to force the runtime to restart
		panic(err)
	}
	go http.Serve(l, r)

	// setup handler
	types := []string{"image/png"}
	handler = serverlessplus.NewHandler(port).WithBinaryMIMETypes(types)
}

func entry(ctx context.Context, req *serverlessplus.APIGatewayRequest) (*serverlessplus.APIGatewayResponse, error) {
	return handler.Handle(ctx, req)
}

func main() {
	cloudfunction.Start(entry)
}

示例

支持的框架

serverlessplus 被设计为 HTTP 协议与框架进行交互, 对框架并没有限制

路线图

serverlessplus 处于活跃开发中,API 可能在未来的版本中发生变更,我们十分欢迎来自社区的贡献,你可以通过 pull request 或者 issue 来参与。

Documentation

Index

Constants

View Source
const (
	// Host is the host of HTTP server
	Host = "127.0.0.1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIGatewayRequest

type APIGatewayRequest struct {
	Headers     map[string]string        `json:"headers"`
	Method      string                   `json:"httpMethod"`
	Path        string                   `json:"path"`
	QueryString map[string]interface{}   `json:"queryString"`
	Body        string                   `json:"body"`
	Context     APIGatewayRequestContext `json:"requestContext"`
}

APIGatewayRequest represents an API gateway request

type APIGatewayRequestContext

type APIGatewayRequestContext struct {
	ServiceID string `json:"serviceId"`
	RequestID string `json:"requestId"`
	Method    string `json:"httpMethod"`
	Path      string `json:"path"`
	SourceIP  string `json:"sourceIp"`
	Stage     string `json:"stage"`
	Identity  struct {
		SecretID *string `json:"secretId"`
	} `json:"identity"`
}

APIGatewayRequestContext represents a request context

type APIGatewayResponse

type APIGatewayResponse struct {
	IsBase64Encoded bool              `json:"isBase64Encoded"`
	StatusCode      int               `json:"statusCode"`
	Headers         map[string]string `json:"headers"`
	Body            string            `json:"body"`
}

APIGatewayResponse represents a API gateway response

type Handler

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

Handler represents a request handler

func NewHandler

func NewHandler(port int) *Handler

NewHandler creates a new handler

func (*Handler) Handle

Handle processes the incoming request

func (*Handler) WithBinaryMIMETypes

func (h *Handler) WithBinaryMIMETypes(types []string) *Handler

WithBinaryMIMETypes allows user to specify MIME types that should be base64 encoded

func (*Handler) WithClient

func (h *Handler) WithClient(c *http.Client) *Handler

WithClient allows user to specify a custom `http.Client`

Jump to

Keyboard shortcuts

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