reload htpasswd on SIGHUP

This commit is contained in:
Aaron Bieber 2019-03-04 16:55:29 -07:00
parent a87d968870
commit f18a5c16be
No known key found for this signature in database
GPG key ID: 279160AB1BE1236B

View file

@ -30,8 +30,10 @@ import (
"encoding/csv" "encoding/csv"
"log" "log"
"os" "os"
"os/signal"
"regexp" "regexp"
"sync" "sync"
"syscall"
"time" "time"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -54,6 +56,8 @@ type HtpasswdFile struct {
// NewHtpasswdFromFile reads the users and passwords from a htpasswd file and returns them. If an error is encountered, // NewHtpasswdFromFile reads the users and passwords from a htpasswd file and returns them. If an error is encountered,
// it is returned, together with a nil-Pointer for the HtpasswdFile. // it is returned, together with a nil-Pointer for the HtpasswdFile.
func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) { func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
stat, err := os.Stat(path) stat, err := os.Stat(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,6 +77,17 @@ func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
// Start a goroutine that limits reload checks to once per CheckInterval // Start a goroutine that limits reload checks to once per CheckInterval
go h.throttleTimer() go h.throttleTimer()
go func() {
for range c {
err := h.Reload()
if err == nil {
log.Printf("Reloaded htpasswd file")
} else {
log.Printf("Could not reload htpasswd file: %v", err)
}
}
}()
return h, nil return h, nil
} }