This is a simple HTTP server written in Go that serves static files and handles form submissions.
- Serves static files from the
STATIC
directory - Handles form submissions via
POST
requests - Provides a simple
/hello
endpoint
Ensure you have Go installed. You can download it from golang.org.
- Clone the repository:
git clone <repository_url> cd <repository_name>
- Create a
STATIC
directory and place your static files (e.g.,index.html
). - Run the server:
go run main.go
- Open
http://localhost:8080
in your browser.
- The server serves files from the
STATIC
directory. - Access static files via
http://localhost:8080/filename
.
Handles form submissions.
<form action="/form" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address">
<br>
<button type="submit">Submit</button>
</form>
POST REQUEST SUCCESSFUL
Name = John Doe
Address = 123 Street, City
- Returns a simple
hello
message. - Access it via
http://localhost:8080/hello
.
package main
import (
"fmt"
"log"
"net/http"
)
// Handles form submission
func formhandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not supported", http.StatusNotFound)
return
}
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "parse form err %v", err)
return
}
fmt.Fprintf(w, "POST REQUEST SUCCESSFUL \n")
fmt.Fprintf(w, "Name = %s\n", r.FormValue("name"))
fmt.Fprintf(w, "Address = %s\n", r.FormValue("address"))
}
// Handles /hello endpoint
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 != http.MethodGet {
http.Error(w, "method is not supported", http.StatusNotFound)
return
}
fmt.Fprintf(w, "hello")
}
// Main function
func main() {
fmt.Println("MAIN SERVER")
fileServer := http.FileServer(http.Dir("./STATIC"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formhandler)
http.HandleFunc("/hello", hellohandler)
fmt.Println("Server started at http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}