Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "math/rand"
  8. "net/http"
  9. "strconv"
  10. "github.com/gorilla/mux"
  11. )
  12.  
  13. // Book Struct ( Class-like i guess ?) - Looks like ES6 classes or JAVA
  14. type Book struct {
  15. ID string `json:id`
  16. ISBN string `json:isbn`
  17. Title string `json:title`
  18. Author *Author `json:author`
  19. }
  20.  
  21. // Author Struct, sub-struct to book
  22. type Author struct {
  23. Firstname string `json:firstname`
  24. Lastname string `json:lastname`
  25. }
  26.  
  27. // Init books var as a slice (variable array)
  28. var books []Book
  29.  
  30. // Get all books
  31. func getBooks(w http.ResponseWriter, r *http.Request) {
  32. w.Header().Set("Content-Type", "application/json")
  33. json.NewEncoder(w).Encode(books)
  34. }
  35.  
  36. // Get a single book by id
  37. func getBook(w http.ResponseWriter, r *http.Request) {
  38. w.Header().Set("Content-Type", "application/json")
  39. params := mux.Vars(r) // Get params
  40.  
  41. // Find correct book with id r
  42. for _, item := range books {
  43.  
  44. fmt.Println(item.ID, params["id"])
  45. fmt.Printf("%+v\n", books)
  46. if item.ID == params["id"] {
  47. json.NewEncoder(w).Encode(item)
  48. return
  49. }
  50. }
  51.  
  52. json.NewEncoder(w).Encode(&Book{})
  53. }
  54.  
  55. // Create a new book
  56. func createBook(w http.ResponseWriter, r *http.Request) {
  57. w.Header().Set("Content-Type", "application/json")
  58. fmt.Println(r.Body)
  59. var book Book
  60. _ = json.NewDecoder(r.Body).Decode(&book)
  61. book.ID = strconv.Itoa(rand.Intn(10000000)) // Mock ID - shitty duplicates
  62. books = append(books, book)
  63. json.NewEncoder(w).Encode(book)
  64.  
  65. }
  66.  
  67. // Updates an existing book
  68. func updateBook(w http.ResponseWriter, r *http.Request) {
  69.  
  70. }
  71.  
  72. // Deletes a book by id
  73. func deleteBook(w http.ResponseWriter, r *http.Request) {
  74.  
  75. }
  76.  
  77. func main() {
  78.  
  79. // Initialize router
  80. r := mux.NewRouter()
  81.  
  82. // Mock Data - @todo - implement DB
  83. books = append(books, Book{"1", "AS-465234", "The First Title", &Author{"Simon", "Borgmästars"}})
  84. books = append(books, Book{"2", "KL-34545", "The Second Title", &Author{"Simon2", "Borgmästars2"}})
  85. books = append(books, Book{"3", "OP-64545", "The Third Title", &Author{"Simon3", "Borgmästars3"}})
  86. books = append(books, Book{"4", "UI-90875", "The Fourth Title", &Author{"Simon4", "Borgmästars4"}})
  87.  
  88. // Route Handlers
  89. r.HandleFunc("/api/books", getBooks).Methods("GET")
  90. r.HandleFunc("/api/books/{id}", getBook).Methods("GET")
  91. r.HandleFunc("/api/books", createBook).Methods("POST")
  92. r.HandleFunc("/api/books", updateBook).Methods("PUT")
  93. r.HandleFunc("/api/books/{id}", deleteBook).Methods("DELETE")
  94.  
  95. // Running the server
  96. fmt.Println("Starting up the server...")
  97. log.Fatal(http.ListenAndServe(":3000", r))
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement