@@ -18,6 +18,7 @@ import (
1818 "sync"
1919
2020 "github.com/claudiodangelis/qrcp/qr"
21+ "github.com/eiannone/keyboard"
2122
2223 "github.com/claudiodangelis/qrcp/config"
2324 "github.com/claudiodangelis/qrcp/pages"
@@ -89,8 +90,14 @@ func (s Server) Wait() error {
8990 return nil
9091}
9192
93+ // Shutdown the server
94+ func (s Server ) Shutdown () {
95+ s .stopChannel <- true
96+ }
97+
9298// New instance of the server
9399func New (cfg * config.Config ) (* Server , error ) {
100+
94101 app := & Server {}
95102 // Get the address of the configured interface to bind the server to
96103 bind , err := util .GetInterfaceAddress (cfg .Interface )
@@ -156,13 +163,27 @@ func New(cfg *config.Config) (*Server, error) {
156163 app .stopChannel = make (chan bool )
157164 // Create cookie used to verify request is coming from first client to connect
158165 cookie := http.Cookie {Name : "qrcp" , Value : "" }
159- // Gracefully shutdown when an OS signal is received
166+ // Gracefully shutdown when an OS signal is received or when "q" is pressed
160167 sig := make (chan os.Signal , 1 )
161168 signal .Notify (sig , os .Interrupt )
162169 go func () {
163170 <- sig
164171 app .stopChannel <- true
165172 }()
173+ if err := keyboard .Open (); err != nil {
174+ panic (err )
175+ }
176+ defer func () {
177+ keyboard .Close ()
178+ }()
179+ go func () {
180+ for {
181+ char , _ , _ := keyboard .GetKey ()
182+ if string (char ) == "q" {
183+ app .stopChannel <- true
184+ }
185+ }
186+ }()
166187 // The handler adds and removes from the sync.WaitGroup
167188 // When the group is zero all requests are completed
168189 // and the server is shutdown
0 commit comments