Created
May 11, 2014 22:46
-
-
Save macmania/908dfbf22eda47478c35 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
| package main | |
| import ( | |
| "net/http" | |
| "io/ioutil" | |
| "encoding/json" | |
| "fmt" | |
| ) | |
| //Data model | |
| type Person struct { | |
| Name string | |
| Age int | |
| } | |
| var ryanne Person | |
| func ryanneHandler(w http.ResponseWriter, req *http.Request) { | |
| fmt.Println(req.URL.Path) | |
| switch req.Method { | |
| case "GET": | |
| buf, _ := json.Marshal(&ryanne) | |
| w.Write(buf) | |
| //curl -v -H "Content-Type: application/json" -X PUT --data "stuff.json" http://localhost:8003/ryanne | |
| //this uploads the stuff.json, this curl method checks whether it had upload it successfully | |
| case "PUT": | |
| buf, _ := ioutil.ReadAll(req.Body) | |
| err := json.Unmarshal(buf, &ryanne) | |
| if err != nil { | |
| fmt.Println("error: ", err) | |
| } | |
| fmt.Printf("%s %d", ryanne.Name, ryanne.Age) | |
| case "DELETE": | |
| buf, _ := ioutil.ReadAll(req.Body) | |
| err := json.Unmarshal(buf, &ryanne) | |
| case: "POST": | |
| //to-do stub | |
| default: | |
| w.WriteHeader(400) | |
| } | |
| } | |
| func handler(w http.ResponseWriter, req *http.Request){ | |
| switch req.URL { | |
| case "/register": | |
| //go to this function handler | |
| //and handle the registration process | |
| case "/login": | |
| } | |
| } | |
| func main() { | |
| ryanne.Name = "Ryanne" | |
| ryanne.Age = 25 | |
| http.HandleFunc("/ryanne", ryanneHandler) | |
| http.ListenAndServe(":8003", nil) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment