示例#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
}
示例#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
}
示例#3
0
//ExpandAssets expands files in /file in an Assets if not exist in disk.
func ExpandAssets() {
	dir, err := util.AssetDir("file")
	if err != nil {
		log.Fatal(err)
	}
	for _, fname := range dir {
		if fname == "message-ja.txt" || fname == "message-en.txt" {
			continue
		}
		fnameDisk := path.Join(cfg.FileDir, fname)
		fnameDisk = filepath.FromSlash(fnameDisk)
		if util.IsFile(fnameDisk) {
			continue
		}
		log.Println("expanding", fnameDisk)
		d := filepath.Dir(fnameDisk)
		if !util.IsDir(d) {
			err := os.MkdirAll(d, 0755)
			if err != nil {
				log.Fatal(err, d)
			}
		}
		c, err := util.Asset(path.Join("file", fname))
		if err != nil {
			log.Fatal(err)
		}
		err = ioutil.WriteFile(fnameDisk, c, 0644)
		if err != nil {
			log.Fatal(err)
		}
	}
}
示例#4
0
//newHtemplate adds funcmap to template var and parse files.
func newHtemplate(templateDir string) *Htemplate {
	t := &Htemplate{htmlTemplate.New("")}
	t.Funcs(htmlTemplate.FuncMap(funcMap))
	templateFiles := filepath.Join(templateDir, "*.txt")

	if util.IsDir(templateDir) {
		_, err := t.ParseGlob(templateFiles)
		if err != nil {
			log.Fatal(err)
		}
	}
	mat, err := filepath.Glob(templateFiles)
	if err != nil {
		log.Fatal(err)
	}
	e := make(map[string]struct{})
	for _, m := range mat {
		e[filepath.Base(m)] = struct{}{}
	}
	dir, err := util.AssetDir("gou_template")
	if err != nil {
		log.Fatal(err)
	}
	for _, a := range dir {
		if _, exist := e[path.Base(a)]; exist {
			continue
		}
		c, err := util.Asset(path.Join("gou_template", a))
		if err != nil {
			log.Fatal(err)
		}

		if _, err := t.Parse(string(c)); err != nil {
			log.Fatal(err)
		}
	}

	return t
}