-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (86 loc) · 2.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START gke_hello_app]
// [START container_hello_app]
package main
import (
"fmt"
"log"
"net/http"
"os"
"bytes"
"encoding/json"
"io/ioutil"
)
func main() {
// register hello function to handle all requests
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
// use PORT environment variable, or default to 8080
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// start the web server on port and accept requests
log.Printf("Server listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, mux))
}
// hello responds to the request with a plain-text "Hello, world" message.
func hello(w http.ResponseWriter, r *http.Request) {
var bodyBytes []byte
var err error
log.Printf("version 1.3")
log.Printf("Serving request: %s", r.URL.Path)
host, _ := os.Hostname()
fmt.Fprintf(w, "Hello, world!\n")
fmt.Fprintf(w, "Version: 1.0.0\n")
fmt.Fprintf(w, "Hostname: %s\n", host)
for name, values := range r.Header {
// Loop over all values for the name.
for _, value := range values {
fmt.Fprintf(w, "Request Header: %s = %s\n", name, value)
}
}
if r.Body != nil {
bodyBytes, err = ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("Body reading error: %v", err)
return
}
defer r.Body.Close()
}
fmt.Printf("Headers: %+v\n", r.Header)
str1 := string(bodyBytes[:])
fmt.Println("byte array string =",str1)
if len(bodyBytes) > 0 {
fmt.Println("length of bytearray",len(bodyBytes))
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, bodyBytes, "", "\t"); err != nil {
fmt.Printf("JSON parse error: %v", err)
return
}
fmt.Println(string(prettyJSON.Bytes()))
} else {
fmt.Printf("Body: No Body Supplied\n")
}
// CORS headers
//w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8000")
//w.Header().Set("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
//w.Header().Set("Access-Control-Allow-Credentials", "true")
//w.Header().Set("Access-Control-Allow-Headers", "Accept-Encoding,Authorization,X-Forwarded-For,Content-Type,Origin,Server")
}
// [END container_hello_app]
// [END gke_hello_app]