-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
48 lines (44 loc) · 1.1 KB
/
Copy pathserver.go
File metadata and controls
48 lines (44 loc) · 1.1 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
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/hello", helloHandler)
http.HandleFunc("/form", formHandler)
fmt.Println("Starting server at port 8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func formHandler(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "POST" {
http.Error(writer, "Method not supported", http.StatusNotFound)
return
}
err := request.ParseForm()
if err != nil {
fmt.Println("An error occured: ", err)
return
}
name := request.FormValue("name")
address := request.FormValue("address")
fmt.Println("name:", name, "\n", "address:", address)
}
func helloHandler(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/hello" {
http.Error(writer, "Route not found", http.StatusNotFound)
return
}
if request.Method != "GET" {
http.Error(writer, "Method not supported", http.StatusNotFound)
return
}
response := "Hello there!"
//writer.t
writer.Write([]byte(response))
}