//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 }
//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) } } }
//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) } } }
//SetupDirectories makes necessary dirs. func SetupDirectories() { for _, j := range []string{cfg.RunDir, cfg.LogDir} { if !util.IsDir(j) { err := os.MkdirAll(j, 0755) if err != nil { log.Fatal(err) } } } }
//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 }