Skip to content

Instantly share code, notes, and snippets.

@macmania
Created May 11, 2014 22:46
Show Gist options
  • Select an option

  • Save macmania/908dfbf22eda47478c35 to your computer and use it in GitHub Desktop.

Select an option

Save macmania/908dfbf22eda47478c35 to your computer and use it in GitHub Desktop.
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