Exemplo n.º 1
0
func linkHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	pbr, _ := util.GetPubber(c)
	vars := mux.Vars(r)
	linkId := vars["link"]
	link := util.GetLink(c, linkId)
	c.Infof("LINK: %v\n", util.EmptyLink)
	page := util.Page{Pubber: *pbr, Content: *link}
	buff := renderTemplate(w, "link", page)
	if buff != nil {
		w.Write(buff)
	}
}
Exemplo n.º 2
0
func visitHandler(w http.ResponseWriter, r *http.Request) {
	var (
		referers  map[string]int
		languages map[string]int
		browsers  map[string]int
		oss       map[string]int
	)

	c := appengine.NewContext(r)
	vars := mux.Vars(r)
	linkId := vars["link"]
	if linkId == "" {
		http.Redirect(w, r, "/", http.StatusFound)
	}
	link := util.GetLink(c, linkId)
	if link.URL == "" {
		http.Redirect(w, r, "/", http.StatusFound)
	}
	// Update and publish
	header := r.Header
	lang := strings.ToLower(strings.Split(header.Get("Accept-Language"),
		",")[0])
	ref := r.Referer()

	if ref == "" {
		ref = "(not set)"
	}
	ip := getIP(r)
	ua := r.UserAgent()
	browser := browserDetect(ua)
	os := osDetect(ua)

	// Update the LinkData
	data := &link.Data
	data.Clicks++

	err := json.Unmarshal(data.Referers, &referers)
	if err != nil {
		c.Infof("JSON Err1: %v\n", err)
		referers = make(map[string]int)
	}
	refJson, err := updateLDMap(referers, ref)
	if err == nil {
		data.Referers = refJson
	}

	err = json.Unmarshal(data.Languages, &languages)
	if err != nil {
		languages = make(map[string]int)
	}
	langJson, err := updateLDMap(languages, lang)
	if err == nil {
		data.Languages = langJson
	}

	err = json.Unmarshal(data.Browsers, &browsers)
	if err != nil {
		browsers = make(map[string]int)
	}
	browJson, err := updateLDMap(browsers, browser)
	if err == nil {
		data.Browsers = browJson
	}

	err = json.Unmarshal(data.OSs, &oss)
	if err != nil {
		oss = make(map[string]int)
	}
	ossJson, err := updateLDMap(oss, os)
	if err == nil {
		data.OSs = ossJson
	}

	link.Save(c)

	// Publish the Click with PubNub
	click := util.Click{
		IP:       ip,
		Referer:  ref,
		Language: lang,
		Browser:  browser,
		OS:       os,
	}
	pm := &util.PubMessage{
		Data:  link.Data,
		Click: click,
	}
	util.Publish(c, w, r, linkId, pm)
	http.Redirect(w, r, link.URL, http.StatusFound)
}