示例#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)
	}
}
示例#2
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)
		}
	}
}
示例#3
0
//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
}
示例#4
0
//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
}