-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
47 lines (39 loc) · 1.07 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
customHandlerPort, exists := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT")
if !exists {
customHandlerPort = "8080"
}
mux := http.NewServeMux()
mux.HandleFunc("/api/hello", helloHandler)
fmt.Println("Go server Listening on: ", customHandlerPort)
log.Fatal(http.ListenAndServe(":"+customHandlerPort, mux))
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
w.Write([]byte(`{"message": "hello world"}`))
} else {
// To read form data
// err := r.ParseForm()
body, _ := io.ReadAll(r.Body)
defer r.Body.Close()
// decode JSON - To remove spaces basically
var jsonData map[string]interface{}
json.Unmarshal([]byte(string(body)), &jsonData)
// encode JSON
rjson, _ := json.Marshal(jsonData)
// Just returning the same body as we got in POST
w.Write([]byte(rjson))
// http: superfluous response.WriteHeader call from main.helloHandler
// w.WriteHeader(http.StatusOK)
}
}