Beispiel #1
0
//handleRoot return handler that handles url not defined other handlers.
//if root, print titles of threads. if not, serve files on disk.
func handleRoot() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			gateway.PrintTitle(w, r)
			return
		}
		pathOnDisk := filepath.Join(cfg.Docroot, r.URL.Path)

		if util.IsFile(pathOnDisk) {
			http.ServeFile(w, r, pathOnDisk)
			return
		}
		pathOnAsset := path.Join("www", r.URL.Path)
		if c, err := util.Asset(pathOnAsset); err == nil {
			i, err := util.AssetInfo(pathOnAsset)
			if err != nil {
				log.Fatal(err)
			}
			reader := bytes.NewReader(c)
			http.ServeContent(w, r, path.Base(r.URL.Path), i.ModTime(), reader)
			return
		}

		log.Println("not found", r.URL.Path)
		http.NotFound(w, r)
	}
}
Beispiel #2
0
//newJsCache return jsCache instnace and parse all js files under path dir.
func newJsCache(pth string) *jsCache {
	j := &jsCache{
		path:   pth,
		files:  make(map[string]*finfo),
		assets: make(map[string]*finfo),
	}
	d, err := util.AssetDir("www")
	if err != nil {
		log.Fatal(err)
	}
	for _, f := range d {
		if path.Ext(f) != ".js" {
			continue
		}
		fname := path.Join("www", f)
		c, err := util.Asset(fname)
		if err != nil {
			log.Fatal(err)
		}
		i, err := util.AssetInfo(fname)
		if err != nil {
			log.Fatal(err)
		}
		mt := i.ModTime()
		j.assets[f] = &finfo{
			mtime: &mt,
			cont:  c,
		}
	}
	j.update()
	return j
}