Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created April 1, 2015 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuken/8b0cee1b55b85a044c54 to your computer and use it in GitHub Desktop.
Save suzuken/8b0cee1b55b85a044c54 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/hydrogen18/stoppableListener"
"net"
"net/http"
"sync"
"time"
)
func Hello(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, "Hello\n")
}
func main() {
l, err := net.Listen("tcp", ":3333")
if err != nil {
panic(err)
}
sl, err := stoppableListener.New(l)
if err != nil {
panic(err)
}
http.HandleFunc("/", Hello)
s := http.Server{}
var wg sync.WaitGroup
go func() {
wg.Add(1)
defer wg.Done()
s.Serve(sl)
}()
fmt.Printf("Serving HTTP\n")
fmt.Println("stopping listener..")
sl.Stop()
fmt.Println("waiting on server")
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment