diff --git a/README.md b/README.md index f66554374..4aa80927d 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,8 @@ is demonstrating. This example shows how to create NodeJS functions with can perfome a http request without additional modules - [function-http-python](helloworld-samples/function-http-python) This example shows how to create Python functions which can perfome a http request without additional modules +- [function-python-go-binary](helloworld-samples/function-python-go-binary/README.md) + This example shows how to create a Python function which includes and executes a Go binary #### Eventing - [cron](cron)
diff --git a/helloworld-samples/function-python-go-binary/.ceignore b/helloworld-samples/function-python-go-binary/.ceignore new file mode 100644 index 000000000..7c1d5c80a --- /dev/null +++ b/helloworld-samples/function-python-go-binary/.ceignore @@ -0,0 +1 @@ +main.go diff --git a/helloworld-samples/function-python-go-binary/README.md b/helloworld-samples/function-python-go-binary/README.md new file mode 100644 index 000000000..bea83ce2e --- /dev/null +++ b/helloworld-samples/function-python-go-binary/README.md @@ -0,0 +1,17 @@ +# Python Function with an additional Go binary + +A sample Python function which lets you add an additional go binary and call it as part of the function call. + +Build the go binary: + +```bash +GOOS=linux GOARCH=amd64 go build -o "my-program" -ldflags="-s -w" ./main.go +``` + +Deploy the function straight to Code Engine by running the following command from this directory + +```bash +ibmcloud ce fn create -n py-go-func -runtime python-3.11 --build-source . +``` + +For more information follow blog -> [IBM Cloud Code Engine: Running Binaries inside the IBM Cloud Code Engine Function Runtimes](https://medium.com/@luke.roy/ibm-cloud-code-engine-running-binarys-inside-the-ibm-cloud-code-engine-function-runtimes-6216e34cad54) diff --git a/helloworld-samples/function-python-go-binary/__main__.py b/helloworld-samples/function-python-go-binary/__main__.py new file mode 100644 index 000000000..9d6acf959 --- /dev/null +++ b/helloworld-samples/function-python-go-binary/__main__.py @@ -0,0 +1,25 @@ +import subprocess +import json + +output_dict={} +statusCode = 0 +binary="my-program" + +def main(params): + command = f'./{binary} \'{json.dumps(params)}\'' + result = subprocess.run(command, shell=True, capture_output=True, text=True) + + if result.returncode == 0: + statusCode = 200 + output_dict = json.loads(result.stdout) + else: + statusCode = 500 + output_dict = {"error":"an error as occured"} + + return { + "headers": { + 'Content-Type': 'application/json; charset=utf-8', + }, + "statusCode": statusCode, + "body": output_dict, + } \ No newline at end of file diff --git a/helloworld-samples/function-python-go-binary/main.go b/helloworld-samples/function-python-go-binary/main.go new file mode 100644 index 000000000..028f8b081 --- /dev/null +++ b/helloworld-samples/function-python-go-binary/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" +) + +type Response struct { + Key string `json:"key"` + Value string `json:"value"` + Data interface{} `json:"data"` +} + +func main() { + // recive data as json string and unmarshal into variable + var inputData map[string]interface{} + if len(os.Args) > 1 { + jsonString := os.Args[1] + err := json.Unmarshal([]byte(jsonString), &inputData) + if err != nil { + os.Exit(1) + } + } + + // Here comes your logic + name := "placeholder" + if len(inputData) != 0 { + name = inputData["name"].(string) + } + + // return the response json (to the python code) + respones := Response{ + Key: "New Key", + Value: name, + Data: inputData, + } + responseJSON, err := json.Marshal(respones) + if err != nil { + os.Exit(1) + } + fmt.Println(string(responseJSON)) +} \ No newline at end of file diff --git a/helloworld-samples/function-python-go-binary/requirements.txt b/helloworld-samples/function-python-go-binary/requirements.txt new file mode 100644 index 000000000..a02867d0e --- /dev/null +++ b/helloworld-samples/function-python-go-binary/requirements.txt @@ -0,0 +1 @@ +# add any additiona python modules you might need \ No newline at end of file