Ejemplo 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)
}
Ejemplo n.º 2
0
Archivo: list.go Proyecto: 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)
}
Ejemplo n.º 3
0
Archivo: index.go Proyecto: 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)
}
Ejemplo n.º 4
0
func ServerError(errorCode int, w http.ResponseWriter, err error) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(errorCode)

	buf, tErr := template.Render("error", &errorInfo{
		ErrorCode: errorCode,
		Error:     err,
	})

	if tErr != nil {
		fmt.Fprintf(w, "%s\n", tErr)

		return
	}

	w.Write(buf.Bytes())
}
Ejemplo n.º 5
0
Archivo: add.go Proyecto: npk/trailk
func Add(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	trailk := context.NewContext()
	keyTool := key.NewTool(c)

	if !user.IsAdmin(c) {
		controller.ServerError(http.StatusForbidden, w, errors.New(
			"Only administrator can access this page"))

		return
	}

	data := &sourceAddData{
		Name: r.FormValue("Name"),
		URL:  r.FormValue("URL"),
		Type: r.FormValue("Type"),
	}

	if strings.ToUpper(r.Method) == "POST" {
		fetcher := urlfetch.NewURLFetcher(c)

		sourceKey, err := addSource(trailk, c, keyTool, fetcher, data)

		if err != nil {
			data.Error = err
		} else {
			http.Redirect(w, r, fmt.Sprintf(
				"/source?id=%d&type=%s", sourceKey.IntID(), data.Type), 301)

			return
		}
	}

	buf, tplErr := template.Render("sources-add", data)

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

		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(buf.Bytes())
}
Ejemplo n.º 6
0
Archivo: item.go Proyecto: 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)
}