Soe Thu 5 years ago
parent
commit
33aeeba236
2 changed files with 49 additions and 0 deletions
  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.")
+}