From 6f08cb566465893732411197de967dce5fad41c2 Mon Sep 17 00:00:00 2001 From: Amruta Khamkar Date: Fri, 28 Mar 2025 09:17:19 +0000 Subject: [PATCH] code update recorded at: 28/03/25 09:17:19 --- 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) +}