diff --git a/config.go b/config.go index 6f79436..9ebb8d3 100644 --- a/config.go +++ b/config.go @@ -8,13 +8,17 @@ import ( ) type config struct { - BilderDir string `json:"bilder-dir"` - URLPathPrefix string `json:"url-path-prefix"` - AccessLog string `json:"access-log"` + BilderDir string `json:"bilder-dir"` + URLPathPrefix string `json:"url-path-prefix"` + AccessLog string `json:"access-log"` + Addr string `json:"addr"` + ReloadDelaySeconds int `json:"reload-delay-seconds"` } var defaultConfig = config{ - BilderDir: "bilder", + BilderDir: "bilder", + Addr: ":8173", + ReloadDelaySeconds: 10, } func mustParseConfig() config { @@ -39,5 +43,13 @@ func mustParseConfig() config { c.BilderDir = defaultConfig.BilderDir } + if c.Addr == "" { + c.Addr = defaultConfig.Addr + } + + if c.ReloadDelaySeconds == 0 { + c.ReloadDelaySeconds = defaultConfig.ReloadDelaySeconds + } + return c } diff --git a/config.json b/config.json index e94eaba..8f191f1 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,6 @@ { - "bilder-dir": "tmp", - "access-log": "access.log" + "bilder-dir": "tmp", + "access-log": "access.log", + "reload-delay-seconds": 2, + "addr": ":8111" } diff --git a/main.go b/main.go index fb2e24a..866cd05 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main func main() { conf := mustParseConfig() albums := make(chan []album, 1) - w := newWatcher(conf.BilderDir, conf.URLPathPrefix, albums) - s := newServer(conf.BilderDir, conf.AccessLog, albums) + w := newWatcher(conf.ReloadDelaySeconds, conf.BilderDir, conf.URLPathPrefix, albums) + s := newServer(conf.Addr, conf.BilderDir, conf.AccessLog, albums) go w.start() s.serve() diff --git a/server.go b/server.go index b5e2d92..d270471 100644 --- a/server.go +++ b/server.go @@ -32,6 +32,7 @@ func (sf *syncFile) Write(p []byte) (n int, err error) { type server struct { http.Server sync.RWMutex + addr string albumUpdates <-chan []album dir string accessLog string @@ -39,8 +40,8 @@ type server struct { albums map[string]authHandler } -func newServer(d, al string, au <-chan []album) *server { - return &server{dir: d, accessLog: al, albumUpdates: au} +func newServer(ad, d, al string, au <-chan []album) *server { + return &server{addr: ad, dir: d, accessLog: al, albumUpdates: au} } func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -186,9 +187,9 @@ func (s *server) serve() { mux.Handle("/b/", http.StripPrefix("/b/", s)) - s.Addr = ":8173" + s.Addr = s.addr s.Handler = mux - log.Printf("Serving on http://0.0.0.0:8173") + log.Printf("Serving on http://" + s.addr) log.Fatal(s.ListenAndServe()) } diff --git a/watcher.go b/watcher.go index e1ca1eb..b1bdf98 100644 --- a/watcher.go +++ b/watcher.go @@ -45,14 +45,15 @@ func (a album) hasAuth() bool { type watcher struct { dir string + delaySeconds int urlPathPrefix string configs map[string]dirConfig images map[string]map[string]*imgDetails albumUpdates chan<- []album } -func newWatcher(d, upp string, au chan<- []album) *watcher { - return &watcher{dir: d, urlPathPrefix: upp, albumUpdates: au} +func newWatcher(ds int, d, upp string, au chan<- []album) *watcher { + return &watcher{delaySeconds: ds, dir: d, urlPathPrefix: upp, albumUpdates: au} } type dirConfig struct { @@ -74,7 +75,7 @@ func (w *watcher) start() { w.writeIndexes() w.passAlbumUpdates() w.reset() - <-time.After(10 * time.Second) + <-time.After(time.Duration(w.delaySeconds) * time.Second) } }