Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Commit

Permalink
ability to flush the whole domain
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Feb 21, 2015
1 parent 2101ce4 commit 5cf1731
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ Note that you can't use `application/x-www-form-urlencoded` with HTTP DELETE.
So you have to put the `?url=...` into the URL.

Note also that in this example the `url` is URL encoded. The `:` becomes `%3A`.

## How to remove all your documents

You can start over and flush all the documents you have sent it by doing
a HTTP DELETE request to the url `/v1/flush`. Like this:

curl -X DELETE -H "Auth-Key: yoursecurekey" \
https://autocompeter.com/v1/flush

This will reset the counts all related to your domain. The only thing that
isn't removed is your auth key.
82 changes: 82 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func updateHandler(w http.ResponseWriter, req *http.Request) {
if group != "" {
encodedGroup := encodeString(group)
c.Append("ZADD", encoded+encodedGroup+prefix, form.Popularity, encodedURL)
c.Append("HSET", encoded+"$groups", encodedURL, encodedGroup)
pipedCommands++
} else {
c.Append("ZADD", encoded+prefix, form.Popularity, encodedURL)
}
Expand Down Expand Up @@ -498,6 +500,85 @@ func privateStatsHandler(w http.ResponseWriter, req *http.Request) {

}

func flushHandler(w http.ResponseWriter, req *http.Request) {
key := req.Header.Get("AUTH-KEY")
if key == "" {
output := map[string]string{"error": "Auth-Key header not set"}
renderer.JSON(w, http.StatusForbidden, output)
return
}

c, err := redisPool.Get()
errHndlr(err)
defer redisPool.Put(c)

domain, err := GetDomain(key, c)
if err != nil {
output := map[string]string{"error": "Auth-Key not recognized"}
renderer.JSON(w, http.StatusForbidden, output)
return
}

encoded := encodeString(domain)

all, err := c.Cmd("HGETALL", encoded+"$titles").List()
errHndlr(err)
pipedCommands := 0
var encodedURL string
for i, each := range all {
if i%2 == 0 {
encodedURL = each
} else {
encodedGroup := ""
reply := c.Cmd("HGET", encoded+"$groups", encodedURL)
if reply.Type != redis.NilReply {
encodedGroup, err = reply.Str()
errHndlr(err)
}
prefixes := getPrefixes(each)
for _, prefix := range prefixes {
if encodedGroup != "" {
c.Append("ZREM", encoded+encodedGroup+prefix, encodedURL)
} else {
c.Append("ZREM", encoded+prefix, encodedURL)
}
pipedCommands++
}
c.Append("HDEL", encoded+"$titles", encodedURL)
pipedCommands++
c.Append("HDEL", encoded+"$urls", encodedURL)
pipedCommands++
if encodedGroup != "" {
c.Append("HDEL", encoded+"$groups", encodedURL)
pipedCommands++
}
}
}

now := time.Now()
// var dt time.Time
var fetchKey string
for y := 2015; y <= now.Year(); y++ {
for m := 1; m <= 12; m++ {
fetchKey = fmt.Sprintf("$domainfetches$%v$%v", y, m)
c.Append("HDEL", fetchKey, domain)
pipedCommands++
}
}

for i := 1; i <= pipedCommands; i++ {
if err := c.GetReply().Err; err != nil {
errHndlr(err)
}
}

err = c.Cmd("HSET", "$domaindocuments", domain, 0).Err
errHndlr(err)

output := map[string]string{"message": "OK"}
renderer.JSON(w, http.StatusNoContent, output)
}

var (
redisPool *pool.Pool
procs int
Expand Down Expand Up @@ -581,6 +662,7 @@ func main() {
mux.HandleFunc("/v1", updateHandler).Methods("POST", "PUT")
mux.HandleFunc("/v1", deleteHandler).Methods("DELETE")
mux.HandleFunc("/v1/stats", privateStatsHandler).Methods("GET")
mux.HandleFunc("/v1/flush", flushHandler).Methods("DELETE")

n := negroni.Classic()

Expand Down
39 changes: 39 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,45 @@ def test_delete_row_carefully(self):
[u'/other/url', u'Another blog post about nothing']
])

def test_delete_domain(self):
self._set_domain_key('xyz123', 'peterbecom')
r = self.post('/v1', {
'url': '/plog/something',
'title': "blog something",
}, headers={'Auth-Key': 'xyz123'})
eq_(r.status_code, 201)
r = self.post('/v1', {
'url': '/plog/other',
'title': "Another blog",
'group': 'private'
}, headers={'Auth-Key': 'xyz123'})
eq_(r.status_code, 201)

r = self.get('/v1/stats', headers={'Auth-Key': 'xyz123'})
eq_(r.status_code, 200)
stats = r.json()
eq_(stats['documents'], 2)

r = self.get('/v1?q=blog&d=peterbecom')
eq_(r.status_code, 200)
eq_(len(r.json()['results']), 1)
r = self.get('/v1?q=blog&d=peterbecom&g=private')
eq_(r.status_code, 200)
eq_(len(r.json()['results']), 2)

r = self.delete('/v1/flush')
eq_(r.status_code, 403)
r = self.delete('/v1/flush', headers={'Auth-Key': 'xyz123'})
eq_(r.status_code, 204)
r = self.get('/v1?q=blo&d=peterbecom')
eq_(r.status_code, 200)
eq_(len(r.json()['results']), 0)

r = self.get('/v1/stats', headers={'Auth-Key': 'xyz123'})
eq_(r.status_code, 200)
stats = r.json()
eq_(stats['documents'], 0)

def test_search_with_groups(self):
r = self.post('/v1', {
'url': '/page/public',
Expand Down

0 comments on commit 5cf1731

Please sign in to comment.