Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More endpoints for Go Web Server #25

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go-web-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ to start http server on [`localhost:8080`](http://localhost:8080)
## Endpoints

- `GET /` - displays [`index.html`](static/index.html) inside [`static`](static/) directory
- `GET /hello` - displays a greeting
- `GET /form.html` - displays [`form.html`](static/form.html) inside [`static`](static/) directory
- `POST /form` - displays post request successful with form values

## Contributors

Expand Down
34 changes: 34 additions & 0 deletions go-web-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,43 @@ import (
"net/http"
)

func formHandler(w http.ResponseWriter, r *http.Request) {

if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}

if r.URL.Path != "/form" {
http.Error(w, "404 not found", http.StatusNotFound)
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 supported", http.StatusMethodNotAllowed)
return
}
fmt.Fprintf(w, "Hello from /hello endpoint!")
}

func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/hello", helloHandler)
http.HandleFunc("/form", formHandler)

fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions go-web-server/static/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>
<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>
</div>
</body>
</html>