Skip to content

Commit 0db77b7

Browse files
Luke-Roy-IBMreggeenr
authored andcommittedFeb 3, 2025
1 parent 091833f commit 0db77b7

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ is demonstrating.
170170
This example shows how to create NodeJS functions with can perfome a http request without additional modules
171171
- [function-http-python](helloworld-samples/function-http-python)
172172
This example shows how to create Python functions which can perfome a http request without additional modules
173+
- [function-python-go-binary](helloworld-samples/function-python-go-binary/README.md)
174+
This example shows how to create a Python function which includes and executes a Go binary
173175

174176
#### Eventing
175177
- [cron](cron)<br>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python Function with an additional Go binary
2+
3+
A sample Python function which lets you add an additional go binary and call it as part of the function call.
4+
5+
Build the go binary:
6+
7+
```bash
8+
GOOS=linux GOARCH=amd64 go build -o "my-program" -ldflags="-s -w" ./main.go
9+
```
10+
11+
Deploy the function straight to Code Engine by running the following command from this directory
12+
13+
```bash
14+
ibmcloud ce fn create -n py-go-func -runtime python-3.11 --build-source .
15+
```
16+
17+
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import subprocess
2+
import json
3+
4+
output_dict={}
5+
statusCode = 0
6+
binary="my-program"
7+
8+
def main(params):
9+
command = f'./{binary} \'{json.dumps(params)}\''
10+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
11+
12+
if result.returncode == 0:
13+
statusCode = 200
14+
output_dict = json.loads(result.stdout)
15+
else:
16+
statusCode = 500
17+
output_dict = {"error":"an error as occured"}
18+
19+
return {
20+
"headers": {
21+
'Content-Type': 'application/json; charset=utf-8',
22+
},
23+
"statusCode": statusCode,
24+
"body": output_dict,
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
)
8+
9+
type Response struct {
10+
Key string `json:"key"`
11+
Value string `json:"value"`
12+
Data interface{} `json:"data"`
13+
}
14+
15+
func main() {
16+
// recive data as json string and unmarshal into variable
17+
var inputData map[string]interface{}
18+
if len(os.Args) > 1 {
19+
jsonString := os.Args[1]
20+
err := json.Unmarshal([]byte(jsonString), &inputData)
21+
if err != nil {
22+
os.Exit(1)
23+
}
24+
}
25+
26+
// Here comes your logic
27+
name := "placeholder"
28+
if len(inputData) != 0 {
29+
name = inputData["name"].(string)
30+
}
31+
32+
// return the response json (to the python code)
33+
respones := Response{
34+
Key: "New Key",
35+
Value: name,
36+
Data: inputData,
37+
}
38+
responseJSON, err := json.Marshal(respones)
39+
if err != nil {
40+
os.Exit(1)
41+
}
42+
fmt.Println(string(responseJSON))
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# add any additiona python modules you might need

0 commit comments

Comments
 (0)
Please sign in to comment.