Skip to content

Instantly share code, notes, and snippets.

@davecheney
Created April 10, 2013 04:55
Show Gist options
  • Select an option

  • Save davecheney/5351910 to your computer and use it in GitHub Desktop.

Select an option

Save davecheney/5351910 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"runtime/pprof"
"strconv"
)
type httpHandler struct{}
func (handler *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
const response = "abcdefghijklmnopqrstuvwxyz"
w.Header().Add("Content-Type", "text/plain")
w.Header().Add("Content-Length", strconv.Itoa(len(response)))
io.WriteString(w, response)
}
func main() {
profile := false
if profile {
f, _ := os.Create("profile.cpu")
pprof.StartCPUProfile(f)
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
if profile {
pprof.StopCPUProfile()
}
fmt.Println("Caught interrupt.. shutting down.")
os.Exit(0)
}()
handler := &httpHandler{}
server := &http.Server{
Addr: "0.0.0.0:8081",
Handler: handler,
}
server.ListenAndServe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment