# simple-web-server
[Github](https://github.com/careful12/simple-web-server)
ref :[Build A Web Server With Golang In 20 Mins (2021) - Beginner Friendly!](https://www.youtube.com/watch?v=ASBUp7stqjo&ab_channel=AkhilSharma)
---

## Server code
```python
package main
import (
"fmt"
"log"
"net/http"
)
func formHandler(w http.ResponseWriter, r *http.Request) {
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 = %s\n", name)
fmt.Fprintf(w, "Address = %s\n", address)
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "method is not support", http.StatusNotFound)
return
}
fmt.Fprintf(w, "hello!")
}
func main() {
FileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", FileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
fmt.Println("Starting server at port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
```
## Index.html
```html
<html>
<head>
<title>Static Website</title>
</head>
<body>
<h2>Static Website</h2>
</body>
</html>
```
## Form.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<form method="POST" action="/form">
<label>Name</label><input name="name" type="text" value="">
<label>Address</label><input name="address" type="text" value="">
<input type="submit" value="submit" />
</form>
</body>
</html>
```
---
# Test
|Start the server (execute the code)|
|-----------|
||
|Open a browser and type `localhost:8080`|
|--|
||
|we can see the `index.html`|
|Enter `localhost:8080/hello`|
|--|
||
|This is `helloHander()`|
|Enter `localhost:8080/form.html`|
|--|
||
|Fill the blank|
|After you submit|
|--|
||