func LongURL(short string, tde *kv.TiedotEngine) (*Shortened, error) { // ignore the ID for now, we don't really need it out := new(Shortened) _, err := tde.Query("short.url").Equals(kv.Path{"Short"}, base62.DecodeString(short)).OneInto(out) log.Infof("Read one into %+v error:%v", out, err) if err != nil { return nil, err } return out, nil }
func incrCount(tde *kv.TiedotEngine) int64 { mu.Lock() defer mu.Unlock() counter := new(Counter) id, err := tde.Query(counterCollection).Has(kv.Path{"Count"}).OneInto(counter) if err == kv.ErrNotFound { log.Warning("Counter not found, saving new one.") _, err = tde.Insert(counterCollection, Counter{Count: 1}) if err != nil { log.Errorf("Error saving new counter err=%s", err.Error()) } r, err := tde.All(counterCollection) log.V(3).Infof("total of %d results=%v, err=%v", len(r), r, err) return 1 } if err != nil { log.Error("Failure getting counter err:" + err.Error()) panic(err) } counter.Count++ err = tde.Update(counterCollection, id, counter) if err != nil { log.Error("Failure updating counter err:" + err.Error()) panic(err) } return counter.Count }
func remove(w http.ResponseWriter, r *http.Request, tde *kv.TiedotEngine, params martini.Params) (int, []byte) { short := params["short"] _, err := tde.Query(urlCollection).Equals(kv.Path{"Short"}, base62.DecodeString(short)).Delete() if err != nil { log.Error("Failure deleting URL /" + short + " err:" + err.Error()) return 500, M{ "message": "Could not delete URL /" + short, "error": err.Error(), }.JSON() } log.V(1).Info("Deleted URL /" + short) return 200, M{ "deleted": M{"short": short}, }.JSON() }
func incrHits(tde *kv.TiedotEngine, key string) uint64 { mu.Lock() defer mu.Unlock() short := new(Shortened) id, err := tde.Query(urlCollection).Equals( kv.Path{"Short"}, base62.DecodeString(key), ).OneInto(short) if err == kv.ErrNotFound { log.Warningf("Short URL %s not found", key) return 0 } else if err != nil { log.Errorf("Failure getting shortened URL key=%s err:%v", key, err) return 0 } short.HitCount++ err = tde.Update(urlCollection, id, short) if err != nil { log.Error("Failure updating hitcount key=%s err=%s", key, err.Error()) return 0 } return short.HitCount }
func saveShortened(s Shortened, tde *kv.TiedotEngine) error { _, err := tde.Insert(urlCollection, s) return err }