-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (54 loc) · 1.63 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
var tmpl *template.Template
func main() {
fmt.Println()
fileServer := http.FileServer(http.Dir("./static")) //searches for index.html itself
http.Handle("/", fileServer)
http.HandleFunc("/about", aboutHandler)
http.HandleFunc("/exit", exitHandler)
http.HandleFunc("/hello", helloHandler)
http.HandleFunc("/form", formHandler)
fmt.Println("starting the server")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("error in starting the server : %v", err)
}
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" && r.Method != "GET" {
http.NotFound(w, r)
return
}
fmt.Fprintln(w, "Hello Ho gya Ji!! Balle balle!!")
}
func formHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Println("error in parsing form, :", err)
}
fmt.Fprintln(w, "POST request success, Data sent okay!")
fmt.Fprintln(w, "username :=", r.FormValue("name"))
fmt.Fprintln(w, "Address :=", r.FormValue("address"))
}
// parsing files using ParseFiles & Execute
func aboutHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("./static/about.html")
if err != nil {
fmt.Println("error in parsing about.html,", err)
return
}
tmpl.Execute(w, nil)
}
// parsing files using ParseGlob & ExecuteTemplate for passing data to static file
func exitHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("./static/*html")
if err != nil {
fmt.Println("error in parsing byebye.html,", err)
return
}
tmpl.ExecuteTemplate(w, "bye.html", "Messiiiii")
}