Giter VIP home page Giter VIP logo

bce-cfc-go's Introduction

定义handler

用户代码需要实现InvokeHandler接口,该接口定义如下:

type InvokeHandler interface {
	Handle(input io.Reader, output io.Writer, context InvokeContext) error
}

input 用户输入的事件内容
output 接收函数运行的输出结果
context 函数运行时环境。保存了函数的相关信息,以及访问其他服务所需的AK/SK/Session Token

注册handler

注册handler需要调用RegisterNamedHandler接口。 该函数第一个参数为handler名称,要与前端界面配置保持一致。

定义main

代码库中实现了函数调用的基础框架,需要用户定义的main函数中调用框架的Main开始事件处理。

完整示例代码

demo.go中定义并注册handler

package main

import (
	"io"
	"log"

	"github.com/baidubce/bce-cfc-go/pkg/cfc"
)

func init() {
    // 注册handler
    cfc.RegisterNamedHandler("echo_handler", &EchoHandler{})
}

// 定义handler
type EchoHandler struct {
}

func (h *EchoHandler) Handle(input io.Reader, output io.Writer, context cfc.InvokeContext) error {
	n, err := io.Copy(output, input)
	log.Printf("copy %d bytes\n", n)
	if err != nil {
		log.Println(err)
	}
	return nil
}

main.go执行自定义初始化逻辑,并调用框架Main函数开始事件处理

import (
	"flag"

	"github.com/baidubce/bce-cfc-go/pkg/cfc"
)

func main() {
	flag.Parse()
    // 进入框架,处理函数事件
	cfc.Main()
}

编译代码

编译代码时,需要设置GOOSGOARCH两个环境变量来指定对应的运行时环境。

export GOOS=linux
export GOARCH=amd64
go build

打包输出

用户可以将生成的二进制执行文件打包到zip文件中,并上传zip包即可。

自定义程序启动参数

可以通过在zip包中添加一个bootstrap脚本文件,实现自定义的程序启动参数。 该文件中定义一个名为ExecStart的数组。示例如下:

#!/bin/bash

ExecStart=(
    ./golang-runtime
    "--alsologtostderr"
    "-v7"
)

bce-cfc-go's People

Contributors

xinglsh6937 avatar flyingfang avatar lizhe-baidu avatar smilewhxlucky avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.