Created
June 6, 2021 17:22
-
-
Save mrcodetastic/c6271d87182f0b766504240fc9325cfa to your computer and use it in GitHub Desktop.
Simple GoLang HTTP Server
This file contains hidden or 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
# go run webserver.go | |
# Run this in whatever directory you're wanting to quickly serve on port 80 | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", serveFile) | |
http.ListenAndServe(":80", nil) | |
} | |
func serveFile(w http.ResponseWriter, r *http.Request) { | |
log.Println(r.URL) | |
if r.Method != "GET" { | |
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | |
return | |
} | |
http.ServeFile(w, r, r.URL.Path[1:]) | |
} | |
func HelloServer(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) // strip the '/' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment