-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·98 lines (89 loc) · 3.33 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
// packages that enables our task
import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"net/http"
)
// define the data type
type article struct {
ID string `json:"ID"`
Title string `json:"Title"`
Content string `json:"Content"`
}
// A slice mock data of all articles described by the article struct type
var articles = []article{
article{ID: "1",Title: "Introduction to Go", Content: "This is a brief about Go and its awesomeness"},
article{ID: "2",Title: "The simplicity of Go", Content: "There is so much Awesomeness in Go"},
article{ID: "3",Title: "Go Mastery", Content: "If you love simplicity then you should master Go"},
}
// implement a retrival of all existing articles
func getAllArticles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(articles)
w.WriteHeader(http.StatusOK)
}
// implement a single article retrival by id
func getAnArticle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for _, article := range articles {
if article.ID == params["id"] {
json.NewEncoder(w).Encode(article)
return
}
}
json.NewEncoder(w).Encode(&article{})
w.WriteHeader(http.StatusOK)
}
func createAnArticle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var newArticle article
_ = json.NewDecoder(r.Body).Decode(&newArticle)
articles = append(articles, newArticle) //{"ID": "4","Title": "Go: Clean, Readable and Fast","Content": "It is clean in design, easy to read and outputs very fast."}
json.NewEncoder(w).Encode(newArticle)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"success": "article created"}`))
}
// implement the updating of a given article by id
func updateAnArticle(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
var updatedArticle article
_ = json.NewDecoder(r.Body).Decode(&updatedArticle)
params := mux.Vars(r)
for index, article := range articles {
if article.ID == params["id"] {
updatedArticle.ID = params["id"]
article.Title = updatedArticle.Title
article.Content = updatedArticle.Content
articles = append(articles[:index], updatedArticle) //{"ID":"4","Title":"Go is Clean and Readable","Content":"It is clean in design, easy to read and outputs very fast."}
json.NewEncoder(w).Encode(updatedArticle)
w.WriteHeader(http.StatusAccepted)
w.Write([]byte(`{"success": "article updated"}`))
}
}
}
// implements the deletion of a specific article by id
func deleteAnArticle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, article := range articles {
if article.ID == params["id"] {
articles = append(articles[:index],articles[index+1:]...)
}
}
json.NewEncoder(w).Encode(&article{})
w.WriteHeader(http.StatusOK)
}
// program entry point
func main() {
r := mux.NewRouter()
api := r.PathPrefix("/api/v1").Subrouter()
api.HandleFunc("/articles", getAllArticles).Methods("GET")
api.HandleFunc("/articles/{id}", getAnArticle).Methods("GET")
api.HandleFunc("/articles", createAnArticle).Methods("POST")
api.HandleFunc("/articles/{id}", updateAnArticle).Methods("PUT")
api.HandleFunc("/articles/{id}", deleteAnArticle).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080",r))
}