Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e65cef018 |
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module example.com/main
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require github.com/OpenFunction/functions-framework-go v0.5.0
|
||||||
55
main.go
Normal file
55
main.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RequestData represents the JSON structure for incoming requests
|
||||||
|
type RequestData struct {
|
||||||
|
A int `json:"a"`
|
||||||
|
B int `json:"b"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// mainHandler handles HTTP requests and processes the data
|
||||||
|
func mainHandler(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")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Main() {
|
||||||
|
// Set up the HTTP server
|
||||||
|
http.HandleFunc("/", mainHandler)
|
||||||
|
|
||||||
|
fmt.Println("Server is running on http://localhost:8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user