mirror of
https://github.com/yudai/gotty.git
synced 2025-09-21 21:40:49 +02:00
Shutdown server gracefully with Ctrl-C
This commit is contained in:
parent
94a6230355
commit
e613b29cc3
3 changed files with 45 additions and 2 deletions
19
app/app.go
19
app/app.go
|
@ -17,6 +17,7 @@ import (
|
|||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/braintree/manners"
|
||||
"github.com/elazarl/go-bindata-assetfs"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kr/pty"
|
||||
|
@ -26,6 +27,7 @@ type App struct {
|
|||
options Options
|
||||
|
||||
upgrader *websocket.Upgrader
|
||||
server *manners.GracefulServer
|
||||
|
||||
preferences map[string]interface{}
|
||||
titleTemplate *template.Template
|
||||
|
@ -157,16 +159,21 @@ func (app *App) Run() error {
|
|||
}
|
||||
|
||||
var err error
|
||||
app.server = manners.NewWithServer(
|
||||
&http.Server{Addr: endpoint, Handler: siteHandler},
|
||||
)
|
||||
if app.options.EnableTLS {
|
||||
cert, key := app.loadTLSFiles()
|
||||
err = http.ListenAndServeTLS(endpoint, cert, key, siteHandler)
|
||||
err = app.server.ListenAndServeTLS(cert, key)
|
||||
} else {
|
||||
err = http.ListenAndServe(endpoint, siteHandler)
|
||||
err = app.server.ListenAndServe()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Exiting...")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -217,6 +224,14 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
|
|||
context.goHandleClient()
|
||||
}
|
||||
|
||||
func (app *App) Exit() (firstCall bool) {
|
||||
if app.server != nil {
|
||||
log.Printf("Received Exit command, waiting for all clients to close sessions...")
|
||||
return app.server.Close()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func wrapLogger(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%s %s", r.Method, r.URL.Path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue