Exemple #1
0
//extentions reads files with suffix in root dir and return them.
//if __merged file exists return it when useMerged=true.
func (c *CGI) extension(suffix string) []string {
	var filename []string
	d, err := util.AssetDir("www")
	if err != nil {
		log.Fatal(err)
	}
	for _, fname := range d {
		if util.HasExt(fname, suffix) {
			filename = append(filename, fname)
		}
	}
	if util.IsDir(cfg.Docroot) {
		err = util.EachFiles(cfg.Docroot, func(f os.FileInfo) error {
			i := f.Name()
			if util.HasExt(i, suffix) {
				if !util.HasString(filename, i) {
					filename = append(filename, i)
				}
			}
			return nil
		})
		if err != nil {
			log.Println(err)
		}
	}
	sort.Strings(filename)
	return filename
}
//update reloads all js files if mtime is newer.
func (j *jsCache) update() {
	for k := range j.files {
		j.files[k].exist = false
	}
	if !util.IsDir(j.path) {
		return
	}
	err := util.EachFiles(j.path, func(f os.FileInfo) error {
		var err error
		name := f.Name()
		if !util.HasExt(name, ".js") {
			return nil
		}
		oldfi, exist := j.files[name]
		if !exist || f.ModTime().After(*oldfi.mtime) {
			m := f.ModTime()
			fi := finfo{mtime: &m, exist: true}
			fi.cont, err = ioutil.ReadFile(path.Join(j.path, name))
			j.files[name] = &fi
		} else {
			oldfi.exist = true
		}
		return err
	})
	if err != nil {
		log.Fatal(err)
	}

	for k := range j.files {
		if !j.files[k].exist {
			delete(j.files, k)
		}
	}
}