Advertisement
krovn

Untitled

Oct 24th, 2019
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.30 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "net/http"
  5.  
  6.     "github.com/jkrovnl/rkods/controllers"
  7.     "github.com/julienschmidt/httprouter"
  8. )
  9.  
  10. func main() {
  11.     r := httprouter.New()
  12.     uc := controllers.NewUserController()
  13.     r.GET("/user/:id", uc.GetUser)
  14.     r.POST("/user", uc.CreateUser)
  15.     r.DELETE("/user/:id", uc.DeleteUser)
  16.     http.ListenAndServe("localhost:8080", r)
  17. }
  18. ===========================================
  19. package models
  20.  
  21. // capital U because its gonna be exported
  22. type User struct {
  23.     Name   string `json:"name"`
  24.     Gender string `json:"gender"`
  25.     Age    int    `json:"age"`
  26.     Id     string `json:"id"`
  27. }
  28. =============================================
  29. package controllers
  30.  
  31. import (
  32.     "encoding/json"
  33.     "fmt"
  34.     "net/http"
  35.  
  36.     "github.com/jkrovnl/rkods/models"
  37.     "github.com/julienschmidt/httprouter"
  38. )
  39.  
  40. type UserController struct{}
  41.  
  42. func NewUserController() *UserController {
  43.     return &UserController{}
  44. }
  45.  
  46. //Methods have to be capitalized to be exported, eg, GetUser and not getUser
  47. func (uc UserController) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
  48.     u := models.User{
  49.         Name:   "James Bond",
  50.         Gender: "male",
  51.         Age:    32,
  52.         Id:     p.ByName("id"),
  53.     }
  54.     uj, err := json.Marshal(u)
  55.     if err != nil {
  56.         fmt.Println(err)
  57.     }
  58.     w.Header().Set("Content-Type", "application/json")
  59.     w.WriteHeader(http.StatusOK) // 200
  60.     fmt.Fprintf(w, "%s\n", uj)
  61. }
  62.  
  63. func (uc UserController) CreateUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  64.     u := models.User{}
  65.     json.NewDecoder(r.Body).Decode(&u)
  66.     u.Id = "007"
  67.     uj, err := json.Marshal(u)
  68.     if err != nil {
  69.         fmt.Println(err)
  70.     }
  71.     w.Header().Set("Content-Type", "application/json")
  72.     w.WriteHeader(http.StatusCreated) // 201
  73.     fmt.Fprintf(w, "%s\n", uj)
  74. }
  75.  
  76. func (uc UserController) DeleteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
  77.     //TODO: only write code to delete user
  78.     w.WriteHeader(http.StatusOK) // 200
  79.     fmt.Fprint(w, "Write code to delete user \n")
  80. }
  81. =====================================================
  82. Start your server
  83. Enter this at the terminal
  84. curl http://localhost:8080/user/1
  85. curl -X POST -H "Content-Type: application/json" -d '{"Name":"James Bond","Gender":"male","Age":32,"Id":"777"}' http://localhost:8080/user
  86. curl -X DELETE -H "Content-Type: application/json" http://localhost:8080/user/777
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement