Exemplo n.º 1
0
func History(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	page, err := cached.Get(HISTORYS_MEMCACHE_KEY,
		1*time.Hour, func() ([]byte, error) {
			currentTime := trailk.Time().Now()
			data := getMostRecentHistory(currentTime, trailk, c, keyTool)
			buf, err := template.Render("history", data)

			if err != nil {
				return []byte(""), err
			}

			return buf.Bytes(), nil
		})

	if err != nil {
		controller.ServerError(http.StatusInternalServerError, w, err)

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}
Exemplo n.º 2
0
Arquivo: index.go Projeto: npk/trailk
func Index(w http.ResponseWriter, r *http.Request) {
	// Check if we need to display 404 here
	if r.URL.Path != "/" {
		NotFoundError(w, r)

		return
	}

	c := appengine.NewContext(r)
	trailk := context.NewContext()

	keyTool := key.NewTool(c)
	s := summary.NewSummaries(trailk, c, keyTool)
	cached := memcache.NewMemcache(c)

	page, err := cached.Get(INDEX_MEMCACHE_KEY, 24*time.Hour, func() ([]byte, error) {
		currentTime := trailk.Time().Now()

		sumData := &summaryData{
			Report30Days:  getSummaryReports("DEFAULT_30_DAYS", "Source", currentTime, s),
			Report24Hours: getSummaryReports("DEFAULT_24_HOURS", "Source", currentTime, s),

			Dists:    s.Select("Source").Distribution().All(),
			DistsRef: s.Select("Reference").Distribution().All(),
		}

		buf, tplErr := template.Render("landing", sumData)

		if tplErr != nil {
			return []byte(""), tplErr
		}

		return buf.Bytes(), nil
	})

	if err != nil {
		switch err {
		default:
			controller.ServerError(http.StatusInternalServerError, w, err)
		}

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)

	w.Write(page)
}
Exemplo n.º 3
0
func HistoryAPI(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	endTimestampStr := r.URL.Query().Get("end")

	if len(endTimestampStr) < 1 {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("End time must be specified"))

		return
	}

	endTSInt, errTS := strconv.ParseInt(endTimestampStr, 10, 64)

	if errTS != nil {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("End time must be an unix timestamp"))

		return
	}

	endTimestamp := time.Unix(endTSInt, 0)

	page, err := cached.Get(HISTORY_MEMCACHE_KEY+":"+endTimestampStr,
		1*time.Hour, func() ([]byte, error) {
			data := getMostRecentHistory(endTimestamp, trailk, c, keyTool)

			json, err := json.Marshal(data)

			if err != nil {
				return []byte(""), err
			}

			return json, nil
		})

	if err != nil {
		controller.ServerError(http.StatusInternalServerError, w, err)

		return
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}
Exemplo n.º 4
0
Arquivo: list.go Projeto: npk/trailk
func List(w http.ResponseWriter, r *http.Request) {
	var page []byte
	var err error

	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	if !user.IsAdmin(c) {
		page, err = cached.Get(SOURCES_MEMCACHE_KEY, 1*time.Hour, func() ([]byte, error) {
			data := getList(trailk, c, keyTool)
			buf, err := template.Render("sources", data)

			if err != nil {
				return []byte(""), err
			}

			return buf.Bytes(), nil
		})
	} else {
		data := getList(trailk, c, keyTool)

		data.CanAddNewSource = true

		buf, err := template.Render("sources", data)

		if err == nil {
			page = buf.Bytes()
		}
	}

	if err != nil {
		controller.ServerError(http.StatusInternalServerError, w, err)

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}
Exemplo n.º 5
0
Arquivo: item.go Projeto: npk/trailk
func Item(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)
	cached := memcache.NewMemcache(c)

	sourceIDStr := r.URL.Query().Get("id")
	sourceTypeStr := r.URL.Query().Get("type")

	if len(sourceIDStr) < 1 {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("Source ID must be inputted"))

		return
	}

	if len(sourceTypeStr) < 1 {
		sourceTypeStr = "Source"
	}

	sourceID, strcErr := strconv.ParseInt(sourceIDStr, 10, 64)

	if strcErr != nil {
		controller.ServerError(http.StatusBadRequest, w,
			errors.New("Source ID must be numeric"))

		return
	}

	page, err := cached.Get(SOURCE_MEMCACHE_KEY+sourceTypeStr+":"+sourceIDStr, 1*time.Hour, func() ([]byte, error) {
		data, err := getItem(sourceID, sourceTypeStr, trailk, c, keyTool)

		if err != nil {
			return []byte(""), err
		}

		buf, tplErr := template.Render("source", data)

		if tplErr != nil {
			return []byte(""), tplErr
		}

		return buf.Bytes(), nil
	})

	if err != nil {
		switch err {
		case datastore.ErrNoSuchEntity:
			controller.ServerError(http.StatusNotFound, w, err)

		case ErrUnknownSourceType:
			controller.ServerError(http.StatusNotFound, w, err)

		default:
			controller.ServerError(http.StatusInternalServerError, w, err)
		}

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(page)
}