/golang-runtime

Implement golang runtime based on custom runtime

Primary LanguageGo

Overview

Function Compute currently supports golang runtime, it is recommended to use golang runtime directly

Custom Runtime go sample

Custom Runtime manual

Event Function (No-HTTP-Trigger)

For No-HTTP-Trigger invoke, we provide a simple framework that favors your rapid development.

sample code:

package main

import (
	"encoding/json"

	gr "github.com/awesome-fc/golang-runtime"
)

func initialize(ctx *gr.FCContext) error {
	ctx.GetLogger().Infoln("init golang!")
	return nil
}

func handler(ctx *gr.FCContext, event []byte) ([]byte, error) {
	b, err := json.Marshal(ctx)
	if err != nil {
		ctx.GetLogger().Error("error:", err)
	}
	ctx.GetLogger().Infof("hello golang! \ncontext = %s", string(b))
	return event, nil
}

func main() {
	gr.Start(handler, initialize)
}

HTTP Function

Just implementing an HTTP server, Start the server with port = os.Getenv("FC_SERVER_PORT")

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)

const (
	fcRequestID          = "x-fc-request-id"
	fcLogTailStartPrefix = "FC Invoke Start RequestId: %s" // Start of log tail mark
	fcLogTailEndPrefix   = "FC Invoke End RequestId: %s"  // End of log tail mark
)

func aHandler(w http.ResponseWriter, req *http.Request) {
	requestID := req.Header.Get(fcRequestID )
	fmt.Println(fmt.Sprintf(fcLogTailStartPrefix, requestID))

	defer func() {
		fmt.Println(fmt.Sprintf(fcLogTailEndPrefix, requestID))
	}()

	// your logic
	w.Write([]byte(fmt.Sprintf("Hello, golang  http invoke!")))
}

func bHandler(w http.ResponseWriter, req *http.Request) {
	requestID := req.Header.Get(fcRequestID)
	if requestID == "" {
		requestID = "rid_123456789"
    }
	fmt.Println(fmt.Sprintf(fcLogTailStartPrefix, requestID))

	defer func() {
		fmt.Println(fmt.Sprintf(fcLogTailEndPrefix, requestID))
	}()

	// your logic
	b, err := ioutil.ReadAll(req.Body)
	if err != nil {
		panic(err)
	}
	info := fmt.Sprintf("method =  %+v;\nheaders = %+v;\nbody = %+v", req.Method, req.Header, string(b))
	w.Write([]byte(fmt.Sprintf("Hello, golang  http invoke! detail:\n %s", info)))
}

func main() {
	fmt.Println("FunctionCompute go runtime inited.")
	http.HandleFunc("/a", aHandler) 
	http.HandleFunc("/b", bHandler)
	port := os.Getenv("FC_SERVER_PORT")
	if port == "" {
		port = "9000"
	}
	http.ListenAndServe(":"+port, nil)
}