Skip to content

Instantly share code, notes, and snippets.

@CullenShane
Last active July 13, 2018 16:38
Show Gist options
  • Save CullenShane/3d5da0e863f73ffad032911e2a276b2d to your computer and use it in GitHub Desktop.
Save CullenShane/3d5da0e863f73ffad032911e2a276b2d to your computer and use it in GitHub Desktop.
Simple "Hello World" HTTP server

Go HTTP Server Example

A really, really simple HTTP server which accepts non-encrypted connections and returns only the last portion of the URI assuming it is the caller's name.

COMPILING

To compile this so that the resulting Docker image is valid you have to compile with a few flags set or you'll get some weird file not found errors.

CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo

Happy Hacking!

FROM scratch
ADD http-server /http-server
EXPOSE 5000/tcp
CMD ["/http-server"]
package main
import (
"net/http"
"strings"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello " + message
w.Write([]byte(message))
}
func main() {
http.HandleFunc("/", sayHello)
if err := http.ListenAndServe(":5000", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment