1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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
- }
- log.Printf("Parse User login.", usr)
- bty, err := json.Marshal(usr)
- if err != nil {
- log.Printf("[ERROR] Marshal %s.", err.Error())
- http.Error(w, err.Error(), 500)
- return
- }
- fmt.Fprint(w, bty)
- log.Printf("End login.")
- }
|