Skip to content

Commit

Permalink
Simple Web Server with Go
Browse files Browse the repository at this point in the history
  • Loading branch information
gundamdouble00 committed Oct 25, 2023
1 parent 824b972 commit 6b45ef4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
43 changes: 43 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"log"
"net/http"
)

func helloHandlder(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.StatusNotFound)
}

fmt.Fprintf(w, "Hello!!!")
}
func formHandlder(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 main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandlder)
http.HandleFunc("/hello", helloHandlder)

fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
16 changes: 16 additions & 0 deletions static/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</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>
11 changes: 11 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Static Website</title>
</head>
<body>
<h2>Static website</h2>
</body>
</html>

0 comments on commit 6b45ef4

Please sign in to comment.