35 lines
923 B
Go
35 lines
923 B
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
// Read the HTML content from the file
|
|
content, err := ioutil.ReadFile("teapot.html")
|
|
if err != nil {
|
|
http.Error(w, "Internal Server Error: Could not read HTML file", http.StatusInternalServerError)
|
|
log.Println("Error reading HTML file:", err)
|
|
return
|
|
}
|
|
|
|
// Write the content to the response writer
|
|
w.WriteHeader(http.StatusTeapot) // Set the 418 status code
|
|
fmt.Fprintf(w, "%s", string(content)) //Write the entire content as a string
|
|
}
|
|
|
|
func main() {
|
|
portPtr := flag.Int("port", 8080, "Port to listen on")
|
|
flag.Parse()
|
|
|
|
port := *portPtr // Dereference the pointer
|
|
|
|
fmt.Printf("Server listening on port %d...\n", port)
|
|
http.HandleFunc("/", handler) // Register the handler for all requests on root path "/"
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
|
}
|