Very very crude HTTP server

This commit is contained in:
2024-07-19 17:37:54 +02:00
parent 1e3161c4a9
commit da44e942b3
7 changed files with 83 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
)
func getRoot(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request received: \n")
io.WriteString(w, "This is my first web server. \n")
}
func getHello(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Hello request received: \n")
io.WriteString(w, "Hello, World! \n")
}
func main() {
http.HandleFunc("/", getRoot)
http.HandleFunc("/hello", getHello)
err := http.ListenAndServe(":8080", nil)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("Server Closed! \n")
} else if err != nil {
fmt.Printf("Error: %v \n", err)
os.Exit(1)
}
fmt.Printf("Server started! \n")
}