Created
September 25, 2024 20:55
-
-
Save samuelastech/a26d49b4aad559fc05869d74827c634a to your computer and use it in GitHub Desktop.
Simple HTTP server with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Server home") | |
}) | |
http.HandleFunc("/resource", func(w http.ResponseWriter, r *http.Request) { | |
data, err := io.ReadAll(r.Body) | |
if err != nil { | |
http.Error(w, "Something bad occurred", http.StatusBadRequest) | |
return | |
} | |
log.Printf("Data was sent: %v\n", data) | |
}) | |
log.Println("Server is running at 8000") | |
http.ListenAndServe(":8000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment