login-validator: add signal handling
This commit is contained in:
parent
d7705fa372
commit
07f0ea3711
1 changed files with 39 additions and 2 deletions
|
@ -1,10 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tftpDir string
|
var tftpDir string
|
||||||
|
@ -42,6 +47,16 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare graceful shutdown
|
||||||
|
interrupt := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGHUP)
|
||||||
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||||
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGINT)
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: *bind,
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("Registering handlers...")
|
log.Println("Registering handlers...")
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/", Index)
|
mux.HandleFunc("/", Index)
|
||||||
|
@ -49,6 +64,28 @@ func main() {
|
||||||
mux.HandleFunc("/logout", logout)
|
mux.HandleFunc("/logout", logout)
|
||||||
http.HandleFunc("/", mux.ServeHTTP)
|
http.HandleFunc("/", mux.ServeHTTP)
|
||||||
|
|
||||||
log.Println("Ready, listening on port", *bind)
|
// Serve content
|
||||||
http.ListenAndServe(*bind, nil)
|
go func() {
|
||||||
|
log.Fatal(srv.ListenAndServe())
|
||||||
|
}()
|
||||||
|
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||||
|
|
||||||
|
// Wait shutdown signal
|
||||||
|
mloop: for {
|
||||||
|
switch <-interrupt {
|
||||||
|
case syscall.SIGHUP:
|
||||||
|
log.Println("Reloading students files...")
|
||||||
|
if lc.students, err = readStudentsList(studentsFile); err != nil {
|
||||||
|
log.Println("Error during students.csv reload:", err)
|
||||||
|
}
|
||||||
|
case syscall.SIGTERM:
|
||||||
|
break mloop;
|
||||||
|
case syscall.SIGINT:
|
||||||
|
break mloop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print("The service is shutting down...")
|
||||||
|
srv.Shutdown(context.Background())
|
||||||
|
log.Println("done")
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue