forked from datatogether/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncrawlable_handlers.go
74 lines (68 loc) · 1.69 KB
/
uncrawlable_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"github.com/archivers-space/archive"
"net/http"
)
func UncrawlableHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
GetUncrawlableHandler(w, r)
case "PUT":
SaveUncrawlablesHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func UncrawlablesHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
ListUncrawlablesHandler(w, r)
case "PUT", "POST":
SaveUncrawlablesHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func GetUncrawlableHandler(w http.ResponseWriter, r *http.Request) {
res := &archive.Uncrawlable{}
args := &UncrawlablesGetArgs{
Id: r.URL.Path[len("/v0/uncrawlables/"):],
Url: r.FormValue("url"),
}
err := new(Uncrawlables).Get(args, res)
if err != nil {
writeErrResponse(w, http.StatusInternalServerError, err)
return
}
writeResponse(w, res)
}
func ListUncrawlablesHandler(w http.ResponseWriter, r *http.Request) {
p := PageFromRequest(r)
res := make([]*archive.Uncrawlable, p.Size)
args := &UncrawlablesListArgs{
Page: p,
OrderBy: "created",
}
err := new(Uncrawlables).List(args, &res)
if err != nil {
writeErrResponse(w, http.StatusInternalServerError, err)
return
}
writePageResponse(w, res, r, p)
}
func SaveUncrawlablesHandler(w http.ResponseWriter, r *http.Request) {
un := &archive.Uncrawlable{}
if err := json.NewDecoder(r.Body).Decode(un); err != nil {
writeErrResponse(w, http.StatusInternalServerError, err)
return
}
res := &archive.Uncrawlable{}
if err := new(Uncrawlables).Save(un, res); err != nil {
writeErrResponse(w, http.StatusInternalServerError, err)
return
}
writeResponse(w, res)
}