Show stop message after shutdown simple server.
@@ -6,6 +6,8 @@ import (
"io"
"log"
"net/http"
+ "os"
+ "os/signal"
"strings"
)
@@ -46,8 +48,24 @@ var builtinSimpleServerInstanceMethods = []*BuiltInMethodObject{
port = portVar.(*StringObject).Value
}
- fmt.Println("Start listening on port: " + port)
- log.Fatal(http.ListenAndServe(":"+port, nil))
+ log.Println("SimpleServer start listening on port: " + port)
+
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt)
+
+ go func() {
+ for range c {
+ log.Println("SimpleServer gracefully stopped")
+ os.Exit(0)
+ }
+ }()
+
+ err := http.ListenAndServe(":"+port, nil)
+
+ if err != http.ErrServerClosed { // HL
+ log.Fatalf("listen: %s\n", err)
+ }
+
return receiver
}
},
