|
@@ -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.")
|
|
|
|
+}
|