Example #1
0
// Get the content of a specified page and check if it exists
func getPage(identifier string, r *http.Request) (bool, Pages) {
	// TODO: Compile page list to make a check without lookup in the datastore
	c := appengine.NewContext(r)

	// Check if we have the item in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getPage-"+identifier)
	if cacheStatus == true {
		var pages Pages
		pPages := bytes.NewBuffer(cachedItem)
		decPages := gob.NewDecoder(pPages)
		decPages.Decode(&pages)
		return true, pages
	}

	// We don't have it in the cache, do a lookup and store it to the cache
	var page Pages
	key := datastore.NewKey(c, "Pages", identifier, 0, nil)
	if err := datastore.Get(c, key, &page); err != nil {
		return false, Pages{}
	}

	// Found it!
	cachedPage := Pages{Identifier: key.StringID(), URL: page.URL, Title: page.Title, ContentString: template.HTML(page.Content)}

	// Add it to the cache
	mPages := new(bytes.Buffer) //initialize a *bytes.Buffer
	encPages := gob.NewEncoder(mPages)
	encPages.Encode(cachedPage)
	cache.AddCache(r, "getPage-"+identifier, mPages.Bytes())

	return true, cachedPage
}
Example #2
0
func getPost(w http.ResponseWriter, r *http.Request, Seq int) Post {
	cachedItem, cacheStatus := cache.GetCache(r, "post-"+strconv.Itoa(Seq))
	if cacheStatus == true {
		var post Post
		buffPost := bytes.NewBuffer(cachedItem)
		decPost := gob.NewDecoder(buffPost)
		decPost.Decode(&post)
		return post
	}

	c := appengine.NewContext(r)
	q := datastore.NewQuery("Post").Filter("Sequence=", Seq)
	var p []Post
	q.GetAll(c, &p)

	if p != nil {
		post := p[0]
		post.ContentString = string(post.Content)
		// Add Cache
		mPost := new(bytes.Buffer)
		encPost := gob.NewEncoder(mPost)
		encPost.Encode(post)
		cache.AddCache(r, "post-"+strconv.Itoa(Seq), mPost.Bytes())

		return post
	}
	var nilPost Post
	return nilPost
}
Example #3
0
// This functions get all pages
func getAllPages(w http.ResponseWriter, r *http.Request) []Pages {
	// Check if we have the item in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getAllPages")
	if cacheStatus == true {
		var pages []Pages
		pPages := bytes.NewBuffer(cachedItem)
		decPages := gob.NewDecoder(pPages)
		decPages.Decode(&pages)
		return pages
	}

	// We don't have it in the cache, make a query and store it to the cache
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Pages")
	var pages []Pages
	keys, err := q.GetAll(c, &pages)
	if err != nil {
		errorHandler.Error(w, r, err.Error())
		return []Pages{}
	}
	models := make([]Pages, len(pages))
	for i := 0; i < len(pages); i++ {
		models[i].Identifier = keys[i].StringID()
		models[i].URL = pages[i].URL
		models[i].Title = pages[i].Title
	}

	// Add it to the cache
	mModels := new(bytes.Buffer) //initialize a *bytes.Buffer
	encModels := gob.NewEncoder(mModels)
	encModels.Encode(models)
	cache.AddCache(r, "getAllPages", mModels.Bytes())

	return models
}
Example #4
0
func getCount(w http.ResponseWriter, r *http.Request) int64 {
	cachedItem, cacheStatus := cache.GetCache(r, "Counter")
	if cacheStatus == true {
		var counter Counter
		buffCount := bytes.NewBuffer(cachedItem)
		decCount := gob.NewDecoder(buffCount)
		decCount.Decode(&counter)
		return counter.Count
	}

	c := appengine.NewContext(r)
	var counter Counter
	key := datastore.NewKey(c, "Counter", "", 1, nil)
	datastore.Get(c, key, &counter)

	if counter.Count != 0 {
		// Add Cache
		mCount := new(bytes.Buffer)
		encCount := gob.NewEncoder(mCount)
		encCount.Encode(counter)
		cache.AddCache(r, "Counter", mCount.Bytes())

		return counter.Count
	}

	return 0
}
Example #5
0
//Get the menu
// TODO: It's ugly to have it here, maybe it should on another place...
func getMenu(r *http.Request) []Navigation {
	var navigation []Navigation

	// Check if we have the item in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getMenu")
	if cacheStatus == true {
		pNavigation := bytes.NewBuffer(cachedItem)
		decNavigation := gob.NewDecoder(pNavigation)
		decNavigation.Decode(&navigation)
	} else {
		// Get it and cache
		c := appengine.NewContext(r)
		q := datastore.NewQuery("Menu").Order("Order")
		var navigation []Navigation
		q.GetAll(c, &navigation)

		mModels := new(bytes.Buffer) //initialize a *bytes.Buffer
		encModels := gob.NewEncoder(mModels)
		encModels.Encode(navigation)
		cache.AddCache(r, "getMenu", mModels.Bytes())
	}

	for key := range navigation {
		if navigation[key].NavLink == r.URL.Path {
			navigation[key].Active = true
		}
	}

	return navigation
}
Example #6
0
func acceptSuggestion(w http.ResponseWriter, r *http.Request) {
	if csrf.ValidateToken(r, r.FormValue("CSRFToken")) {
		trimPath := strings.Trim(r.URL.Path, "/admin/post/suggestion/accept")

		postID, _ := strconv.Atoi(trimPath)
		postID64 := int64(postID)

		c := appengine.NewContext(r)

		keyS := datastore.NewKey(c, "PostSuggestion", "", postID64, nil)

		var suggestion PostSuggestion
		datastore.Get(c, keyS, &suggestion)

		var post Post
		keyP := datastore.NewIncompleteKey(c, "Post", nil)
		var counter Counter
		keyC := datastore.NewKey(c, "Counter", "", 1, nil)
		datastore.Get(c, keyC, &counter)
		counter.Count = counter.Count + 1

		// Add Cache Counter
		mCount := new(bytes.Buffer)
		encCount := gob.NewEncoder(mCount)
		encCount.Encode(counter)
		cache.AddCache(r, "Counter", mCount.Bytes())

		post.Content = suggestion.Content
		post.Sequence = counter.Count
		datastore.Put(c, keyP, &post)
		datastore.Put(c, keyC, &counter)
		datastore.Delete(c, keyS)
		http.Redirect(w, r, "/admin/post/suggestion", http.StatusFound)
	}
}
Example #7
0
/*
 * Returns the content of the robots.txt
 */
func getRobot(r *http.Request) []byte {
	// Check if we have the robot in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getRobot")
	if cacheStatus == true {
		return cachedItem
	}

	// Let's query
	c := appengine.NewContext(r)
	key := datastore.NewKey(c, "Robots", "Robots.txt", 0, nil)
	var robots Robots
	datastore.Get(c, key, &robots)

	cache.AddCache(r, "getRobot", robots.Content)

	return robots.Content
}
Example #8
0
func newPost(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	u := user.Current(c)
	if u != nil {
		if csrf.ValidateToken(r, r.FormValue("CSRFToken")) {
			if r.Method == "POST" {
				var post Post
				keyP := datastore.NewIncompleteKey(c, "Post", nil)
				var counter Counter
				keyC := datastore.NewKey(c, "Counter", "", 1, nil)
				datastore.Get(c, keyC, &counter)
				counter.Count = counter.Count + 1

				// Add Cache Counter
				mCount := new(bytes.Buffer)
				encCount := gob.NewEncoder(mCount)
				encCount.Encode(counter)
				cache.AddCache(r, "Counter", mCount.Bytes())

				c := appengine.NewContext(r)
				post.Content = []byte(r.FormValue("Content"))
				post.Sequence = counter.Count
				datastore.Put(c, keyP, &post)
				datastore.Put(c, keyC, &counter)
			}
		}
	}

	type PassedData struct {
		CSRFToken string
	}

	passedData := PassedData{
		CSRFToken: csrf.GetToken(r),
	}

	passedTemplate := new(bytes.Buffer)
	template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil)
	template.Must(template.ParseFiles("templates/admin/post/new.html")).Execute(passedTemplate, passedData)
	render.Render(w, r, passedTemplate)
}