Skip to content

Instantly share code, notes, and snippets.

@kyriediculous
Created May 8, 2019 12:10
Show Gist options
  • Save kyriediculous/8a8678612db26ac307cc7fe7f896e9ba to your computer and use it in GitHub Desktop.
Save kyriediculous/8a8678612db26ac307cc7fe7f896e9ba to your computer and use it in GitHub Desktop.
func main() {
// STEP 1, STEP 2, STEP 3
// ...
// Start the server in a child routine
go func() {
if err := s.Serve(listener); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}()
fmt.Println("Server succesfully started on port :50051")
// Right way to stop the server using a SHUTDOWN HOOK
// Create a channel to receive OS signals
c := make(chan os.Signal)
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
// Ignore other incoming signals
signal.Notify(c, os.Interrupt)
// Block main routine until a signal is received
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
<-c
// After receiving CTRL+C Properly stop the server
fmt.Println("\nStopping the server...")
s.Stop()
listener.Close()
fmt.Println("Closing MongoDB connection")
db.Disconnect(mongoCtx)
fmt.Println("Done.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment