-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW4_2.go
182 lines (152 loc) · 3.79 KB
/
HW4_2.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"html/template"
"log"
"net"
"net/http"
"strings"
)
type User struct {
Name string
Occupation string
}
type Data struct {
Users []User
}
var data = Data{
Users: []User{
{Name: "John Doe", Occupation: "gardener"},
{Name: "Roger Roe", Occupation: "driver"},
{Name: "Peter Smith", Occupation: "teacher"},
{Name: "Jo Do", Occupation: "trader"},
{Name: "eger Rew", Occupation: "programer"},
{Name: "Olivia Smith", Occupation: "hairdresser"},
},
}
var tmp = template.Must(template.ParseFiles("layout.html"))
func main() {
http.HandleFunc("/get", getProcess)
http.HandleFunc("/delete", deleteProcess)
http.HandleFunc("/put", putProcess)
http.HandleFunc("/patch", patchProcess)
http.HandleFunc("/post", postProcess)
http.HandleFunc("/ip", ipProcess)
log.Println("Listening...")
http.ListenAndServe(":80", nil)
}
func getProcess(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/get" {
http.Error(w, "404 Not Found.", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
tmp.Execute(w, data)
default:
fmt.Fprintf(w, "Method %v Not Allowed.\n", r.Method)
http.Error(w, "Status Code 405.", http.StatusMethodNotAllowed)
}
}
func deleteProcess(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/delete" {
http.Error(w, "404 Not Found.", http.StatusNotFound)
return
}
switch r.Method {
case "DELETE":
//Removing last User in User Table
// data.Users = append(data.Users[:len(data.Users)-1])
// tmp.Execute(w, data)
fmt.Fprintf(w, "DELETE !!!")
default:
fmt.Fprintf(w, "Method %v Not Allowed.\n", r.Method)
http.Error(w, "Status Code 405.", http.StatusMethodNotAllowed)
}
}
func patchProcess(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/patch" {
http.Error(w, "404 Not Found.", http.StatusNotFound)
return
}
switch r.Method {
case "PATCH":
fmt.Fprintf(w, "PATCH !!!")
default:
fmt.Fprintf(w, "Method %v Not Allowed.\n", r.Method)
http.Error(w, "Status Code 405.", http.StatusMethodNotAllowed)
}
}
func putProcess(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/put" {
http.Error(w, "404 Not Found.", http.StatusNotFound)
return
}
switch r.Method {
case "PUT":
fmt.Fprintf(w, "PUT !!!")
default:
fmt.Fprintf(w, "Method %v Not Allowed.\n", r.Method)
http.Error(w, "Status Code 405.", http.StatusMethodNotAllowed)
}
}
func postProcess(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/post" {
http.Error(w, "404 Not Found.", http.StatusNotFound)
return
}
switch r.Method {
case "POST":
fmt.Fprintf(w, "POST !!!")
default:
fmt.Fprintf(w, "Method %v Not Allowed.\n", r.Method)
http.Error(w, "Status Code 405.", http.StatusMethodNotAllowed)
}
}
func ipProcess(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ip" {
http.Error(w, "404 Not Found.", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
fmt.Fprintf(w, "Your IP = ")
ip, err := getIP(r)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("No valid ip"))
}
w.WriteHeader(200)
w.Write([]byte(ip))
default:
fmt.Fprintf(w, "Method %v Not Allowed.\n", r.Method)
http.Error(w, "Status Code 405.", http.StatusMethodNotAllowed)
}
}
func getIP(r *http.Request) (string, error) {
//Get IP from the X-REAL-IP header
ip := r.Header.Get("X-REAL-IP")
netIP := net.ParseIP(ip)
if netIP != nil {
return ip, nil
}
//Get IP from X-FORWARDED-FOR header
ips := r.Header.Get("X-FORWARDED-FOR")
splitIps := strings.Split(ips, ",")
for _, ip := range splitIps {
netIP := net.ParseIP(ip)
if netIP != nil {
return ip, nil
}
}
//Get IP from RemoteAddr
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}
netIP = net.ParseIP(ip)
if netIP != nil {
return ip, nil
}
return "", fmt.Errorf("No valid ip found")
}