Files
teapot-server/teapot-server.go
Vladimir Zagainov ce50369ac7
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
fixed go test command
2025-03-23 00:09:03 +03:00

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))
}