Code Monkey home page Code Monkey logo

golang-runtime's Introduction

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)
}

golang-runtime's People

Contributors

rsonghuster 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.