-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
62 lines (52 loc) · 1.12 KB
/
helper.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
package main
import (
"fmt"
"net/http"
)
/*
* Validate Http request.
*/
func validateHttpRequest(path string, method string, r *http.Request, w http.ResponseWriter) bool {
if r.URL.Path != path {
http.Error(
w,
"404: File Not Found",
http.StatusNotFound)
return false
}
if r.Method != method {
http.Error(
w,
"Method is not supported",
http.StatusNotFound)
return false
}
return true
}
/*
* Function to handle Form web server call.
* * is a pointer for request address
*/
func formHandler(w http.ResponseWriter, r *http.Request) {
if !validateHttpRequest("/form", "POST", r, w) {
return
}
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "Parseform() err: %v", err)
return
}
fmt.Fprintf(w, "POST request successful\n")
name := r.FormValue("name")
address := r.FormValue("address")
fmt.Fprintf(w, "Name : %v\n Address : %v\n", name, address)
}
/*
* Function to handle hello web server call.
* * is a pointer for request address
*/
func helloHandler(w http.ResponseWriter, r *http.Request) {
if !validateHttpRequest("/hello", "GET", r, w) {
return
}
fmt.Fprintf(w, "Hello!!")
}