packagemainimport("context""fmt""log""net""net/http""os""os/signal""syscall""github.com/lestrrat/go-server-starter/listener")funcmain(){log.Printf("start pid %d\n",os.Getpid())listeners,err:=listener.ListenAll()iferr!=nil&&err!=listener.ErrNoListeningTarget{panic(err)}varlnet.Listeneriferr==listener.ErrNoListeningTarget{// Fallback if not running under Server::Starterl,err=net.Listen("tcp",":8080")iferr!=nil{panic("Failed to listen to port 8080")}}else{l=listeners[0]}// 別goroutineでServeしていることに注意!srv:=&http.Server{Handler:newHandler()}gofunc(){srv.Serve(l)}()// mainの中でシグナルの待受をしていることに注意!signal_chan:=make(chanos.Signal)signal.Notify(signal_chan,syscall.SIGTERM)for{s:=<-signal_chanifs==syscall.SIGTERM{log.Printf("SIGTERM!!!!\n")srv.Shutdown(context.Background())return}}}funcnewHandler()http.Handler{mux:=http.NewServeMux()mux.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"from pid %d.\n",os.Getpid())})returnmux}
packagemainimport("fmt""log""net""net/http""os""os/signal""syscall""github.com/lestrrat/go-server-starter/listener""github.com/shogo82148/go-gracedown")funcmain(){log.Printf("start pid %d\n",os.Getpid())signal_chan:=make(chanos.Signal)signal.Notify(signal_chan,syscall.SIGTERM)gofunc(){for{s:=<-signal_chanifs==syscall.SIGTERM{log.Printf("SIGTERM!!!!\n")gracedown.Close()}}}()listeners,err:=listener.ListenAll()iferr!=nil&&err!=listener.ErrNoListeningTarget{panic(err)}varlnet.Listeneriferr==listener.ErrNoListeningTarget{// Fallback if not running under Server::Starterl,err=net.Listen("tcp",":8080")iferr!=nil{panic("Failed to listen to port 8080")}}else{l=listeners[0]}gracedown.Serve(l,newHandler())}funcnewHandler()http.Handler{mux:=http.NewServeMux()mux.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"from pid %d.\n",os.Getpid())})returnmux}
packagemainimport("context""crypto/tls""fmt""log""net""net/http""os""os/signal""syscall""github.com/lestrrat/go-server-starter/listener")// net/http/internal/testcert.go から拝借varlocalhostCert=[]byte(`-----BEGIN CERTIFICATE-----MIICEzCCAXygAwIBAgIQMIMChMLGrR+QvmQvpwAU6zANBgkqhkiG9w0BAQsFADASMRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYwMDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9SjY1bIw4iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZBl2+XsDulrKBxKKtD1rGxlG4LjncdabFn9gvLZad2bSysqz/qTAUStTvqJQIDAQABo2gwZjAOBgNVHQ8BAf8EBAMCAqQwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUwAwEB/zAuBgNVHREEJzAlggtleGFtcGxlLmNvbYcEfwAAAYcQAAAAAAAAAAAAAAAAAAAAATANBgkqhkiG9w0BAQsFAAOBgQCEcetwO59EWk7WiJsG4x8SY+UIAA+flUI9tyC4lNhbcF2Idq9greZwbYCqTTTr2XiRNSMLCOjKyI7ukPoPjo16ocHj+P3vZGfsh1fIw3cSS2OolhloGw/XM6RWPWtPAlGykKLciQrBru5NAPvCMsb/I1DAceTiotQMfblo6RBxUQ==-----END CERTIFICATE-----`)varlocalhostKey=[]byte(`-----BEGIN RSA PRIVATE KEY-----MIICXgIBAAKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9SjY1bIw4iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZBl2+XsDulrKBxKKtD1rGxlG4LjncdabFn9gvLZad2bSysqz/qTAUStTvqJQIDAQABAoGAGRzwwir7XvBOAy5tM/uV6e+Zf6anZzus1s1Y1ClbjbE6HXbnWWF/wbZGOpet3Zm4vD6MXc7jpTLryzTQIvVdfQbRc6+MUVeLKwZatTXtdZrhu+Jk7hx0nTPy8JcbuJqFk541aEw+mMogY/xEcfbWd6IOkp+4xqjlFLBEDytgbIECQQDvH/E6nk+hgN4HqzzVtxxr397vWrjrIgPbJpQvBsafG7b0dA4AFjwVbFLmQcj2PprIMmPcQrooz8vpjy4SHEg1AkEA/v13/5M47K9vCxmb8QeD/asydfsgS5TeuNi8DoUBEmiSJwma7FXYfFUtxuvL7XvjwjN5B30pNEbc6Iuyt7y4MQJBAIt21su4b3sjXNueLKH85Q+phy2UfQtuUE9txblTu14q3N7gHRZB4ZMhFYyDy8CKrN2cPg/Fvyt0Xlp/DoCzjA0CQQDUy2ptGsuSmgUtWj3NM9xuwYPm+Z/F84K6+ARYiZ6PYj013sovGKUFfYAqVXVlxtIXqyUBnu3X9ps8ZfjLZO7BAkEAlT4R5Yl6cGhaJQYZHOde3JEMhNRcVFMO8dJDaFeof9Oeos0UUothgiDktdQHxdNEwLjQf7lJJBzV+5OtwswCWA==-----END RSA PRIVATE KEY-----`)funcmain(){log.Printf("start pid %d\n",os.Getpid())listeners,err:=listener.ListenAll()iferr!=nil&&err!=listener.ErrNoListeningTarget{panic(err)}varlnet.Listeneriferr==listener.ErrNoListeningTarget{// Fallback if not running under Server::Starterl,err=net.Listen("tcp",":8080")iferr!=nil{panic("Failed to listen to port 8080")}}else{l=listeners[0]}cert,err:=tls.X509KeyPair(localhostCert,localhostKey)iferr!=nil{panic(err)}tlsConfig:=&tls.Config{NextProtos:[]string{"h2"},Certificates:[]tls.Certificate{cert},}srv:=&http.Server{Handler:newHandler(),TLSConfig:tlsConfig,}l=tls.NewListener(l,tlsConfig)gofunc(){srv.Serve(l)}()signal_chan:=make(chanos.Signal)signal.Notify(signal_chan,syscall.SIGTERM)for{s:=<-signal_chanifs==syscall.SIGTERM{log.Printf("SIGTERM!!!!\n")srv.Shutdown(context.Background())return}}}funcnewHandler()http.Handler{mux:=http.NewServeMux()mux.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"from pid %d.\n",os.Getpid())})returnmux}