diff --git a/cmd/megauploader/main.go b/cmd/megauploader/main.go index 5e39bd6..4456ed5 100644 --- a/cmd/megauploader/main.go +++ b/cmd/megauploader/main.go @@ -35,7 +35,7 @@ func main() { os.Exit(0) } - mu := megauploader.MegaUploader{Conf: cfg} + mu := megauploader.NewMegaUploader(cfg) mu.SetupRoutes() graceful.Run(*addr, 15*time.Second, http.DefaultServeMux) } diff --git a/http.go b/http.go index 9b85283..ba2ffde 100644 --- a/http.go +++ b/http.go @@ -12,6 +12,7 @@ import ( "path" "path/filepath" "strings" + "sync" rice "github.com/GeertJohan/go.rice" "github.com/c2h5oh/datasize" @@ -20,7 +21,28 @@ import ( // MegaUploader acts as controller for all the application. Since this is inherently a web-focused // application, it will include http utils 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