Example #1
0
// Handle the home page request.
func indexHandler(w http.ResponseWriter, req *http.Request) {
	layout, err := template.ParseFile(PATH_PUBLIC + TEMPLATE_LAYOUT)
	if err != nil {
		http.Error(w, ERROR_TEMPLATE_NOT_FOUND, http.StatusNotFound)
		return
	}
	index, err := template.ParseFile(PATH_PUBLIC + TEMPLATE_INDEX)
	//artical, err := template.ParseFile(PATH_PUBLIC + TEMPLATE_ARTICAL)
	if err != nil {
		http.Error(w, ERROR_TEMPLATE_NOT_FOUND, http.StatusNotFound)
		return
	}
	mapOutput := map[string]interface{}{"Title": "炫酷的网站技术" + TITLE, "Keyword": KEYWORD, "Description": DESCRIPTION, "Base": BASE_URL, "Url": BASE_URL, "Carousel": getAddition(PREFIX_INDEX), "Script": getAddition(PREFIX_SCRIPT), "Items": leveldb.GetRandomContents(20, &Filter{})}
	content := []byte(index.RenderInLayout(layout, mapOutput))
	w.Write(content)
	go cacheFile("index", content)
}
func fecthHandler(w http.ResponseWriter, req *http.Request) {
	url := req.URL.Path
	layout, err := template.ParseFile(PATH_PUBLIC + TEMPLATE_LAYOUT)
	if err != nil {
		http.Error(w, ERROR_TEMPLATE_NOT_FOUND, http.StatusNotFound)
		return
	}
	artical, err := template.ParseFile(PATH_PUBLIC + TEMPLATE_ARTICAL)
	if err != nil {
		http.Error(w, ERROR_TEMPLATE_NOT_FOUND, http.StatusNotFound)
		return
	}
	key := bytes.Trim([]byte(url), "/")
	bs, err := leveldb.Get(key)
	if err != nil {
		http.Error(w, ERROR_TEMPLATE_NOT_FOUND, http.StatusNotFound)
		return
	}
	sps := bytes.Split(bs, []byte(DELIMITER))
	if len(sps) > 3 {
		page := struct {
			Title, Keyword, Description, Markdown, Base, Url string
		}{
			Title:       string(sps[0]) + TITLE,
			Keyword:     string(sps[1]),
			Description: string(sps[2]),
			Markdown:    convertToHTML(bytes.Replace(sps[3], []byte("{{Base}}"), []byte(BASE_URL), -1)),
			Base:        BASE_URL,
			Url:         BASE_URL + string(key) + "/",
		}
		content := []byte(artical.RenderInLayout(layout, &page))
		go cacheFile(string(key), content)
		w.Write(content)
	} else {
		http.Error(w, ERROR_TEMPLATE_NOT_FOUND, http.StatusNotFound)
		return
	}
}