Created
August 19, 2017 16:41
-
-
Save anhnguyen1618/b6082839d25c150a10a510c62c4986ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // func loginController(w http.ResponseWriter, r *http.Request) { | |
| // if r.Method == "GET" { | |
| // data, err := ioutil.ReadFile("public/index.html") | |
| // if err != nil { | |
| // panic(err) | |
| // } | |
| // fmt.Fprintf(w, string(data)) | |
| // } else { | |
| // r.ParseForm() | |
| // userName := r.Form["userName"][0] | |
| // password := r.Form["password"][0] | |
| // fmt.Println(userName, password) | |
| // fmt.Fprintf(w, "Login succesfull") | |
| // } | |
| // } | |
| // func mainController(w http.ResponseWriter, r *http.Request) { | |
| // http.Redirect(w, r, "/login", 301) | |
| // } | |
| // type Task struct { | |
| // Id string `json:"id"` | |
| // Title string `json:"title"` | |
| // Status string `json:"status"` | |
| // } | |
| // func taskController(w http.ResponseWriter, r *http.Request) { | |
| // data := []Task{ | |
| // {"1", "Clean up", "TODO"}, | |
| // {"2", "Kill", "DOING"}, | |
| // {"4", "Programming", "DONE"}, | |
| // } | |
| // if r.Method == "GET" { | |
| // jdata, err := json.Marshal(data) | |
| // if err != nil { | |
| // http.Error(w, err.Error(), http.StatusInternalServerError) | |
| // return | |
| // } | |
| // w.Write(jdata) | |
| // return | |
| // } | |
| // if r.Method == "POST" { | |
| // body, err := ioutil.ReadAll(r.Body) | |
| // if err != nil { | |
| // http.Error(w, err.Error(), http.StatusInternalServerError) | |
| // return | |
| // } | |
| // var m Task | |
| // json.Unmarshal(body, &m) | |
| // data = append(data, m) | |
| // fmt.Fprintf(w, "Added") | |
| // return | |
| // } | |
| // if r.Method == "PUT" { | |
| // body, err := ioutil.ReadAll(r.Body) | |
| // if err != nil { | |
| // http.Error(w, err.Error(), http.StatusInternalServerError) | |
| // return | |
| // } | |
| // var m Task | |
| // json.Unmarshal(body, &m) | |
| // var modifiedIndex int | |
| // for k, v := range data { | |
| // if v.Id == m.Id { | |
| // modifiedIndex = k | |
| // } | |
| // } | |
| // data[modifiedIndex] = m | |
| // fmt.Println(data) | |
| // fmt.Fprintf(w, "Updated") | |
| // return | |
| // } | |
| // if r.Method == "DELETE" { | |
| // body, err := ioutil.ReadAll(r.Body) | |
| // if err != nil { | |
| // http.Error(w, err.Error(), http.StatusInternalServerError) | |
| // return | |
| // } | |
| // var m Task | |
| // json.Unmarshal(body, &m) | |
| // var modifiedIndex int | |
| // for k, v := range data { | |
| // if v.Id == m.Id { | |
| // modifiedIndex = k | |
| // } | |
| // } | |
| // data = append(data[:modifiedIndex], data[modifiedIndex+1:]...) | |
| // fmt.Println(data) | |
| // fmt.Fprintf(w, "Deleted") | |
| // return | |
| // } | |
| // } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment