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) }
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) }
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()) }
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) }
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) }
func Login(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u == nil { loginURL, lgErr := user.LoginURL(c, "/") if lgErr != nil { controller.ServerError(http.StatusInternalServerError, w, lgErr) return } http.Redirect(w, r, loginURL, 301) return } http.Redirect(w, r, "/", 301) }
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) }
func NotFoundError(w http.ResponseWriter, r *http.Request) { controller.ServerError(http.StatusNotFound, w, errors.New("Page not found")) }