httprouter.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/julienschmidt/httprouter"
  7. )
  8. func List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  9. fmt.Fprint(w, "You see user list\n")
  10. }
  11. func Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  12. fmt.Fprintf(w, "you try to see user %s\n", ps.ByName("id"))
  13. }
  14. /*
  15. curl -v -X PUT -H "Content-Type: application/json" -d '{"login":"rvasily"}' http://localhost:8080/users
  16. */
  17. func Create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  18. fmt.Fprintf(w, "you try to create new user\n")
  19. }
  20. /*
  21. curl -v -X POST -H "Content-Type: application/json" -d '{"name":"Vasily Romanov"}' http://localhost:8080/users/rvasily
  22. */
  23. func Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  24. fmt.Fprintf(w, "you try to update %s\n", ps.ByName("login"))
  25. }
  26. func main() {
  27. router := httprouter.New()
  28. router.GET("/", List)
  29. router.GET("/users", List)
  30. router.PUT("/users", Create)
  31. router.GET("/users/:id", Get)
  32. router.POST("/users/:login", Update)
  33. fmt.Println("starting server at :8080")
  34. log.Fatal(http.ListenAndServe(":8080", router))
  35. }