-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (42 loc) · 1.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Year int `json:"year"`
}
var books = map[string]Book{
"1": {ID: "1", Title: "The Go Programming Language", Author: "Alan A. A. Donovan", Year: 2015},
"2": {ID: "2", Title: "Learning Go", Author: "Jon Bodner", Year: 2021},
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/books", getBooks).Methods("GET")
router.HandleFunc("/books/{id}", getBook).Methods("GET")
log.Println("Server is running on port 8000")
log.Fatal(http.ListenAndServe(":8000", router))
}
func getBooks(w http.ResponseWriter, r *http.Request) {
var bookList []Book
for _, book := range books {
bookList = append(bookList, book)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(bookList)
}
func getBook(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
book, exists := books[params["id"]]
if !exists {
http.Error(w, "Book not found", http.StatusNotFound)
return
}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(book)
}