Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br>
Expand Down
1 change: 1 addition & 0 deletions helloworld-samples/function-python-go-binary/.ceignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.go
17 changes: 17 additions & 0 deletions helloworld-samples/function-python-go-binary/README.md
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions helloworld-samples/function-python-go-binary/__main__.py
Original file line number Diff line number Diff line change
@@ -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,
}
43 changes: 43 additions & 0 deletions helloworld-samples/function-python-go-binary/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# add any additiona python modules you might need