Skip to content

Instantly share code, notes, and snippets.

@mrcodetastic
Created June 6, 2021 17:22
Show Gist options
  • Save mrcodetastic/c6271d87182f0b766504240fc9325cfa to your computer and use it in GitHub Desktop.
Save mrcodetastic/c6271d87182f0b766504240fc9325cfa to your computer and use it in GitHub Desktop.
Simple GoLang HTTP Server
# 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