login-validator: add signal handling

This commit is contained in:
nemunaire 2019-02-24 03:27:38 +01:00
parent d7705fa372
commit 07f0ea3711

View File

@ -1,10 +1,15 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
)
var tftpDir string
@ -42,6 +47,16 @@ func main() {
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...")
mux := http.NewServeMux()
mux.HandleFunc("/", Index)
@ -49,6 +64,28 @@ func main() {
mux.HandleFunc("/logout", logout)
http.HandleFunc("/", mux.ServeHTTP)
log.Println("Ready, listening on port", *bind)
http.ListenAndServe(*bind, nil)
// Serve content
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")
}