├── .gitignore └── http-api.go /.gitignore: -------------------------------------------------------------------------------- 1 | golang-basic-http-api.exe -------------------------------------------------------------------------------- /http-api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | "strconv" 7 | 8 | "github.com/gorilla/mux" 9 | ) 10 | 11 | // User is a struct that represents a user in our application 12 | type User struct { 13 | FullName string `json:"fullName"` 14 | Username string `json:"username"` 15 | Email string `json:"email"` 16 | } 17 | 18 | // Post is a struct that represents a single post 19 | type Post struct { 20 | Title string `json:"title"` 21 | Body string `json:"body"` 22 | Author User `json:"author"` 23 | } 24 | 25 | var posts []Post = []Post{} 26 | 27 | func main() { 28 | router := mux.NewRouter() 29 | 30 | router.HandleFunc("/posts", addItem).Methods("POST") 31 | 32 | router.HandleFunc("/posts", getAllPosts).Methods("GET") 33 | 34 | router.HandleFunc("/posts/{id}", getPost).Methods("GET") 35 | 36 | router.HandleFunc("/posts/{id}", updatePost).Methods("PUT") 37 | 38 | router.HandleFunc("/posts/{id}", patchPost).Methods("PATCH") 39 | 40 | router.HandleFunc("/posts/{id}", deletePost).Methods("DELETE") 41 | 42 | http.ListenAndServe(":5000", router) 43 | } 44 | 45 | func getPost(w http.ResponseWriter, r *http.Request) { 46 | // get the ID of the post from the route parameter 47 | var idParam string = mux.Vars(r)["id"] 48 | id, err := strconv.Atoi(idParam) 49 | if err != nil { 50 | // there was an error 51 | w.WriteHeader(400) 52 | w.Write([]byte("ID could not be converted to integer")) 53 | return 54 | } 55 | 56 | // error checking 57 | if id >= len(posts) { 58 | w.WriteHeader(404) 59 | w.Write([]byte("No post found with specified ID")) 60 | return 61 | } 62 | 63 | post := posts[id] 64 | 65 | w.Header().Set("Content-Type", "application/json") 66 | json.NewEncoder(w).Encode(post) 67 | } 68 | 69 | func getAllPosts(w http.ResponseWriter, r *http.Request) { 70 | w.Header().Set("Content-Type", "application/json") 71 | json.NewEncoder(w).Encode(posts) 72 | } 73 | 74 | func addItem(w http.ResponseWriter, r *http.Request) { 75 | // get Item value from the JSON body 76 | var newPost Post 77 | json.NewDecoder(r.Body).Decode(&newPost) 78 | 79 | posts = append(posts, newPost) 80 | 81 | w.Header().Set("Content-Type", "application/json") 82 | 83 | json.NewEncoder(w).Encode(posts) 84 | } 85 | 86 | func updatePost(w http.ResponseWriter, r *http.Request) { 87 | // get the ID of the post from the route parameters 88 | var idParam string = mux.Vars(r)["id"] 89 | id, err := strconv.Atoi(idParam) 90 | if err != nil { 91 | w.WriteHeader(400) 92 | w.Write([]byte("ID could not be converted to integer")) 93 | return 94 | } 95 | 96 | // error checking 97 | if id >= len(posts) { 98 | w.WriteHeader(404) 99 | w.Write([]byte("No post found with specified ID")) 100 | return 101 | } 102 | 103 | // get the value from JSON body 104 | var updatedPost Post 105 | json.NewDecoder(r.Body).Decode(&updatedPost) 106 | 107 | posts[id] = updatedPost 108 | 109 | w.Header().Set("Content-Type", "application/json") 110 | json.NewEncoder(w).Encode(updatedPost) 111 | } 112 | 113 | func patchPost(w http.ResponseWriter, r *http.Request) { 114 | // get the ID of the post from the route parameters 115 | var idParam string = mux.Vars(r)["id"] 116 | id, err := strconv.Atoi(idParam) 117 | if err != nil { 118 | w.WriteHeader(400) 119 | w.Write([]byte("ID could not be converted to integer")) 120 | return 121 | } 122 | 123 | // error checking 124 | if id >= len(posts) { 125 | w.WriteHeader(404) 126 | w.Write([]byte("No post found with specified ID")) 127 | return 128 | } 129 | 130 | // get the current value 131 | post := &posts[id] 132 | json.NewDecoder(r.Body).Decode(post) 133 | 134 | w.Header().Set("Content-Type", "application/json") 135 | json.NewEncoder(w).Encode(post) 136 | } 137 | 138 | func deletePost(w http.ResponseWriter, r *http.Request) { 139 | // get the ID of the post from the route parameters 140 | var idParam string = mux.Vars(r)["id"] 141 | id, err := strconv.Atoi(idParam) 142 | if err != nil { 143 | w.WriteHeader(400) 144 | w.Write([]byte("ID could not be converted to integer")) 145 | return 146 | } 147 | 148 | // error checking 149 | if id >= len(posts) { 150 | w.WriteHeader(404) 151 | w.Write([]byte("No post found with specified ID")) 152 | return 153 | } 154 | 155 | // Delete the post from the slice 156 | // https://github.com/golang/go/wiki/SliceTricks#delete 157 | posts = append(posts[:id], posts[id+1:]...) 158 | 159 | w.WriteHeader(200) 160 | } 161 | --------------------------------------------------------------------------------