From 083b4f4bf3ac332632f1fb3ba784b4d814cdb6e2 Mon Sep 17 00:00:00 2001 From: Sukhpreet Lohiya Date: Mon, 9 Feb 2026 06:36:41 +0000 Subject: [PATCH] code update recorded at: 09/02/26 06:36:41 --- handler.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 handler.go diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..48e1988 --- /dev/null +++ b/handler.go @@ -0,0 +1,34 @@ +// package main provides a set of HTTP Cloud Functions samples. +package main + +import ( + "encoding/json" + "net/http" +) + +// handler is an example of setting CORS headers. +// For more information about CORS and CORS preflight requests, see +// https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request. +func handler(w http.ResponseWriter, r *http.Request) { + // Set CORS headers for the preflight request + if r.Method == http.MethodOptions { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "POST") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + w.Header().Set("Access-Control-Max-Age", "3600") + w.WriteHeader(http.StatusNoContent) + return + } + // Set CORS headers for the main request. + w.Header().Set("Access-Control-Allow-Origin", "*") + // Initialize a map to hold the response body + response := map[string]string{ + "message": "Hello, World!", // Set the message to "Hello, World!" + } + + // Set the Content-Type header to application/json + w.Header().Set("Content-Type", "application/json") + + // Encode the response map into JSON and write it to the response + json.NewEncoder(w).Encode(response) +}