-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadOperationUsingRouter.go
61 lines (47 loc) · 1.39 KB
/
readOperationUsingRouter.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
type Article struct {
Id string `json:"Id"`
Title string `json:"Title"`
Desc string `json:"Desc"`
Content string `json:"content"`
}
var Articles []Article
func Homepage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to Our Home Page")
fmt.Println("Endpoint Hit: homePage")
}
func returnAllArticles(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnAllArticles")
json.NewEncoder(w).Encode(Articles)
}
func returnSingleArticle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["Id"]
for _, article := range Articles {
if article.Id == key {
json.NewEncoder(w).Encode(article)
}
}
}
func handleRequest() {
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/", Homepage)
myRouter.HandleFunc("/articles", returnAllArticles)
myRouter.HandleFunc("/article/{Id}", returnSingleArticle)
log.Fatal(http.ListenAndServe(":2060", myRouter))
}
func main() {
Articles = []Article{
Article{Id: "1", Title: "Article 1", Desc: "Article 1 Description", Content: "Articles 1 Content"},
Article{Id: "2", Title: "Article 2", Desc: "Article 2 Description", Content: "Articles 2 Content"},
Article{Id: "3", Title: "Article 3", Desc: "Article 3 Description", Content: "Articles 3 Content"},
}
handleRequest()
}