// Watches the html/*.html templates for changes: func WatchTemplates(name, templatePath, glob string, preParse func(*template.Template) *template.Template, uiTmpl **template.Template) (watcher *notify.Watcher, deferClean func(), err error) { if preParse == nil { preParse = func(t *template.Template) *template.Template { return t } } // Parse template files: ui, err := preParse(template.New(name)).ParseGlob(path.Join(base.CanonicalPath(templatePath), glob)) if err != nil { return nil, nil, err } *uiTmpl = ui // Watch template directory for file changes: watcher, err = notify.NewWatcher() if err != nil { return nil, nil, err } deferClean = func() { watcher.RemoveWatch(templatePath); watcher.Close() } // Process watcher events go func() { for { select { case ev := <-watcher.Event: if ev == nil { break } //log.Println("event:", ev) // Update templates: var err error ui, err := preParse(template.New(name)).ParseGlob(path.Join(base.CanonicalPath(templatePath), glob)) if err != nil { log.Println(err) break } *uiTmpl = ui case err := <-watcher.Error: if err == nil { break } log.Println("watcher error:", err) } } }() // Watch template file for changes: watcher.Watch(templatePath) return }
func main() { // Define our commandline flags: fs := flag.String("fs", ".", "Root directory of served files and templates") xrGifArg := flag.String("xrg", "", "X-Accel-Redirect header prefix for serving images or blank to disable") xrThumbArg := flag.String("xrt", "", "X-Accel-Redirect header prefix for serving thumbnails or blank to disable") fl_listen_uri := flag.String("l", "tcp://0.0.0.0:8080", "listen URI (schemes available are tcp, unix)") flag.Parse() // Parse all the URIs: listen_addr, err := base.ParseListenable(*fl_listen_uri) base.PanicIf(err) // Parse the flags and set values: flag.Parse() // Make directories we need: base_folder = base.CanonicalPath(path.Clean(*fs)) os.MkdirAll(store_folder(), 0775) os.MkdirAll(thumb_folder(), 0775) os.MkdirAll(tmp_folder(), 0775) xrGif = *xrGifArg xrThumb = *xrThumbArg // Create/update the DB schema if needed: api, err := NewAPI() if err != nil { log.Fatal(err) return } api.Close() // Watch the html templates for changes and reload them: _, cleanup, err := web.WatchTemplates("ui", html_path(), "*.html", nil, &uiTmpl) if err != nil { log.Println(err) return } defer cleanup() // Start profiler: go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() // Start the server: _, err = base.ServeMain(listen_addr, func(l net.Listener) error { return http.Serve(l, web.ReportErrors(web.Log(web.DefaultErrorLog, web.ErrorHandlerFunc(requestHandler)))) }) if err != nil { log.Println(err) return } }
func main() { flag.StringVar(&html_path, "html", "./html", "local path to html templates") flag.StringVar(&proxyRoot, "p", "/", "root of web requests to process") flag.StringVar(&jailRoot, "r", ".", "local filesystem path to bind to web request root path") flag.StringVar(&accelRedirect, "xa", "", "Root of X-Accel-Redirect paths to use)") flag.StringVar(&jplayerUrl, "jp-url", "", `Web path to jPlayer files (e.g. "/js")`) flag.StringVar(&jplayerPath, "jp-path", "", `Local filesystem path to jPlayer files`) fl_listen_uri := flag.String("l", "tcp://0.0.0.0:8080", "listen URI (schemes available are tcp, unix)") flag.Parse() if jplayerUrl != "" { useJPlayer = true } jailRoot = base.CanonicalPath(jailRoot) html_path = base.CanonicalPath(html_path) listen_addr, err := base.ParseListenable(*fl_listen_uri) base.PanicIf(err) // Watch the html templates for changes and reload them: _, cleanup, err := web.WatchTemplates("ui", html_path, "*.html", nil, &uiTmpl) if err != nil { log.Println(err) return } defer cleanup() // Start the server: _, err = base.ServeMain(listen_addr, func(l net.Listener) error { return http.Serve(l, web.ReportErrors(web.Log(web.DefaultErrorLog, web.ErrorHandlerFunc(processRequest)))) }) if err != nil { log.Println(err) return } }
func main() { fl_listen_uri := flag.String("l", "tcp://0.0.0.0:8080", "listen URI (schemes available: tcp, unix)") flag.StringVar(&proxyRoot, "p", "/", "root of web requests to process") flag.StringVar(&jailRoot, "r", ".", "local filesystem path to bind to web request root path") htmlPath := flag.String("html", ".", "local filesystem path to HTML templates") flag.StringVar(&accelRedirect, "xa", "", "Root of X-Accel-Redirect paths to use)") flag.StringVar(&jplayerUrl, "jp-url", "", `Web path to jPlayer files (e.g. "/js" or "//static.somesite.com/jp")`) flag.StringVar(&jplayerPath, "jp-path", "", `Local filesystem path to serve jPlayer files from (optional)`) flag.Parse() // Parse all the URIs: listen_addr, err := base.ParseListenable(*fl_listen_uri) base.PanicIf(err) jailRoot = base.CanonicalPath(jailRoot) if jplayerUrl != "" { useJPlayer = true } // Watch the html templates for changes and reload them: _, cleanup, err := web.WatchTemplates("ui", *htmlPath, "*.html", func(t *template.Template) *template.Template { return t.Funcs(map[string]interface{}{ "isLast": func(i, count int) bool { return i == count-1 }, }) }, &uiTmpl) if err != nil { log.Fatal(err) return } defer cleanup() // Start the server: base.ServeMain(listen_addr, func(l net.Listener) error { return http.Serve(l, http.HandlerFunc(processRequest)) }) }