Skip to content

Commit

Permalink
adds option to customize addr and reload delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgeller committed Jul 30, 2016
1 parent 5e57138 commit 06238df
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
20 changes: 16 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
6 changes: 4 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"bilder-dir": "tmp",
"access-log": "access.log"
"bilder-dir": "tmp",
"access-log": "access.log",
"reload-delay-seconds": 2,
"addr": ":8111"
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ 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
logFile *syncFile
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) {
Expand Down Expand Up @@ -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())
}
7 changes: 4 additions & 3 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 06238df

Please sign in to comment.