Soe Thu пре 5 година
родитељ
комит
33aeeba236
2 измењених фајлова са 49 додато и 0 уклоњено
  1. 3 0
      go.mod
  2. 46 0
      test.go

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module /home/soet/ws/gcloud/function
+
+go 1.12

+ 46 - 0
test.go

@@ -0,0 +1,46 @@
+package login
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net/http"
+)
+
+type User struct {
+	UserID   string `json:"user_id"`
+	Password string `json:"password"`
+}
+
+func HelloGet(w http.ResponseWriter, r *http.Request) {
+	log.Printf("Starting my hello world logging.")
+	fmt.Fprint(w, "Hello, World!")
+}
+
+func MyWorld(w http.ResponseWriter, r *http.Request) {
+	log.Printf(" My world logging.")
+	fmt.Fprint(w, "My World!")
+}
+
+func Login(w http.ResponseWriter, r *http.Request) {
+	log.Printf("Start login.")
+	// Read body
+	b, err := ioutil.ReadAll(r.Body)
+	defer r.Body.Close()
+	if err != nil {
+		log.Printf("[ERROR] Read body.", err.Error())
+		http.Error(w, err.Error(), 500)
+		return
+	}
+	// Unmarshal
+	var usr User
+	err = json.Unmarshal(b, &usr)
+	if err != nil {
+		log.Printf("[ERROR] Unmarshal %s.", err.Error())
+		http.Error(w, err.Error(), 500)
+		return
+	}
+	fmt.Fprint(w, usr)
+	log.Printf("End login.")
+}