//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) } }
//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 }
//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) } } }
//newTtemplate adds funcmap to template var and parse files. func newTtemplate(templateDir string) *Ttemplate { t := &Ttemplate{textTemplate.New("")} t.Funcs(textTemplate.FuncMap(funcMap)) templateFiles := filepath.Join(templateDir, "rss1.txt") if util.IsFile(templateFiles) { _, err := t.ParseFiles(templateFiles) if err != nil { log.Fatal(err) } } else { cont, err := util.Asset(path.Join("gou_template", "rss1.txt")) if err != nil { log.Fatal(err) } _, err = t.Parse(string(cont)) if err != nil { log.Fatal(err) } } return t }
//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 }
//newMessage reads from the file excpet #comment and stores them with url unescaping value. func newMessage(filedir, fname string) (Message, error) { var err error m := make(map[string]string) var dat []byte file := path.Join("file", fname) if dat, err = util.Asset(file); err != nil { log.Println(err) } file = filepath.Join(filedir, fname) if util.IsFile(fname) { dat1, err := ioutil.ReadFile(file) if err != nil { log.Println(err) } else { log.Println("loaded", file) dat = dat1 } } if dat == nil { err := errors.New("message file was not found") log.Fatal(err) return nil, err } re := regexp.MustCompile(`^\s*#`) for i, line := range strings.Split(string(dat), "\n") { line = strings.Trim(line, "\r\n") if line == "" || re.MatchString(line) { continue } buf := strings.Split(line, "<>") if len(buf) != 2 { log.Fatalf("illegal format at line %d in the message file", i) } buf[1] = html.UnescapeString(buf[1]) m[buf[0]] = buf[1] } return m, nil }