···11+package main
22+33+import (
44+ "log"
55+ "net/http"
66+ "os"
77+)
88+99+func main() {
1010+ if len(os.Args) < 2 {
1111+ log.Fatalf("Failed to start: serve dir not provided")
1212+ }
1313+1414+ // Define the directory where your static HTML files are located
1515+ staticDir := os.Args[1]
1616+1717+ // Create a FileServer to serve files from the static directory
1818+ fs := http.FileServer(http.Dir(staticDir))
1919+2020+ // Create a custom handler function
2121+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2222+ // Log the request
2323+ log.Printf("%s %s %s",
2424+ r.Method,
2525+ r.URL.Path,
2626+ r.RemoteAddr,
2727+ )
2828+2929+ // Check if the request path points to a file that exists
3030+ // without the .html extension, and if so, append it internally.
3131+ // This allows requests like "/about" to serve "about.html".
3232+ filePath := r.URL.Path
3333+ if _, err := http.Dir(staticDir).Open(filePath); err != nil {
3434+ // If the file without .html doesn't exist, try with .html
3535+ filePathWithExt := filePath + ".html"
3636+ if _, err := http.Dir(staticDir).Open(filePathWithExt); err == nil {
3737+ r.URL.Path += ".html"
3838+ }
3939+ }
4040+4141+ // Serve the modified request using the FileServer
4242+ fs.ServeHTTP(w, r)
4343+ })
4444+4545+ log.Println("Serving on :8080")
4646+ err := http.ListenAndServe(":8080", nil)
4747+ if err != nil {
4848+ log.Fatal(err)
4949+ }
5050+}