Compare commits

...

1 Commits
main ... v3

Author SHA1 Message Date
FunctionsAPI
1454d1ff7b Automatic push from FunctionsAPI 2025-01-27 12:51:04 +00:00
3 changed files with 60 additions and 2 deletions

View File

@ -1,2 +1 @@
# 63b0a0a0e0e44ed2928ebec9b7f6186c
# go hello-world

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module example.com/hello
go 1.17
require github.com/OpenFunction/functions-framework-go v0.5.0

54
hello.go Normal file
View File

@ -0,0 +1,54 @@
package hello
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/OpenFunction/functions-framework-go/functions"
)
// RequestData represents the JSON structure for incoming requests
type RequestData struct {
A int `json:"a"`
B int `json:"b"`
}
func init() {
functions.HTTP("Main", Main,
functions.WithFunctionPath("/"))
}
// Main handles HTTP requests and processes the data
func Main(w http.ResponseWriter, r *http.Request) {
log.Println("Function called with request")
// Decode the JSON body into the RequestData struct
var data RequestData
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
log.Println("Error decoding JSON:", err)
return
}
log.Printf("Received data: %+v\n", data)
// Compute the result
result := data.A + data.B
// Prepare the response
response := map[string]int{"result": result}
// Encode the response as JSON
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, "Error encoding response", http.StatusInternalServerError)
log.Println("Error encoding JSON:", err)
return
}
log.Println("Response sent successfully")
}