support reload (just trying)

This commit is contained in:
boyska 2017-12-08 19:29:07 +01:00
parent 19c287b4c8
commit 5edd16b326
2 changed files with 24 additions and 2 deletions

View file

@ -35,7 +35,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
mu := megauploader.MegaUploader{Conf: cfg} mu := megauploader.NewMegaUploader(cfg)
mu.SetupRoutes() mu.SetupRoutes()
graceful.Run(*addr, 15*time.Second, http.DefaultServeMux) graceful.Run(*addr, 15*time.Second, http.DefaultServeMux)
} }

22
http.go
View file

@ -12,6 +12,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
rice "github.com/GeertJohan/go.rice" rice "github.com/GeertJohan/go.rice"
"github.com/c2h5oh/datasize" "github.com/c2h5oh/datasize"
@ -21,6 +22,27 @@ import (
// application, it will include http utils // application, it will include http utils
type MegaUploader struct { type MegaUploader struct {
Conf Config Conf Config
configLock *sync.RWMutex
}
// NewMegaUploader create a new mega uploader
func NewMegaUploader(c Config) MegaUploader {
return MegaUploader{Conf: c, configLock: new(sync.RWMutex)}
}
func (mu *MegaUploader) ChangeConf(newconf Config) {
mu.configLock.Lock()
mu.Conf = newconf
mu.configLock.Unlock()
}
// confAcquire is a middleware to read-lock configuration
func (mu *MegaUploader) confAcquire(inner func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
mu.configLock.RLock()
inner(w, r)
mu.configLock.RUnlock()
}
} }
// SetupRoutes adds API routes // SetupRoutes adds API routes