-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_09_2018Example2.go
More file actions
92 lines (82 loc) · 2.49 KB
/
Copy path24_09_2018Example2.go
File metadata and controls
92 lines (82 loc) · 2.49 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type A struct {
Id int `json:"id"`
}
type Art struct {
Id int `json:"id"`
Articles Articles `json:"articles"`
}
type Article struct {
Id int `json : "Id"`
Title string `json:"Title"`
Desc string `json:"desc"`
Content string `json:"Content"`
}
type Articles []Article
var articlesGlobal Articles
func allArticles(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin","GET")
if (request.Method == "GET") {
articles := Articles{
Article{Id: 1, Title: "Harry Potter and The Philosopher's stone", Desc: " It's a story", Content: "Story"},
Article{Id: 2, Title: "Harry Potter and The Chamber Of Secrets", Desc: " It's a story", Content: "Story"},
}
articlesGlobal = articles
fmt.Println("Endpoint Hit!!!!!!!! allArticles")
json.NewEncoder(writer).Encode(articles)
}
}
type Arts []Art
var artsGlobal Arts
func all(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin","*")
arts := Arts{
Art{Id: 1, Articles: articlesGlobal},
Art{Id: 2, Articles: articlesGlobal},
}
artsGlobal = arts
fmt.Println("Endpoint Hit!!!!!!!! all")
json.NewEncoder(writer).Encode(arts)
}
func handleRequests() {
http.HandleFunc("/homepage", homePage)
http.HandleFunc("/", nothing)
http.HandleFunc("/post", post)
http.HandleFunc("/all", all)
http.HandleFunc("/allArticles", allArticles)
http.HandleFunc("/a", a)
//http.HandleFunc("/single/{Id}", single)
log.Fatal(http.ListenAndServe(":8000", nil))
}
func post(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin","*")
if (request.Method == "POST") {
json.NewEncoder(writer).Encode(artsGlobal)
fmt.Println("fROM POST")
}
}
func a(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin","*")
variableA:=A{Id:1}
fmt.Println("Endpoint Hit!!!!!!!! variableA ")
json.NewEncoder(writer).Encode(variableA)
}
func homePage(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin","*")
fmt.Fprintf(writer, "<h1>Welcome to the HomePage!</h1>")
fmt.Println("Endpoint Hit: homePage")
}
func nothing(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin","*")
fmt.Fprintf(writer, "<h1>Wrong hit!!!!!!!!!</h1>")
fmt.Println("Endpoint Hit: Nothing")
}
func main() {
handleRequests()
}