-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.go
75 lines (62 loc) · 1.62 KB
/
add.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
75
package altcrawlhqserver
import (
"context"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/internetarchive/gocrawlhq"
"go.mongodb.org/mongo-driver/mongo/options"
)
func addHandler(c *gin.Context) {
project := c.Param("project")
if !isAuthorized(c) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
// identifier := c.GetHeader("X-Identifier")
addPayload := gocrawlhq.AddPayload{}
err := c.BindJSON(&addPayload)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Printf("Discovered: %v\n", addPayload)
collection := mongoDatabase.Collection(project)
opts := options.InsertMany().SetOrdered(false)
urls := make([]interface{}, len(addPayload.URLs))
for i, url := range addPayload.URLs {
urls[i] = url
}
result, err := collection.InsertMany(context.TODO(), urls, opts)
if addPayload.BypassSeencheck {
}
c.JSON(http.StatusCreated, gin.H{"message": "Added"})
}
// func inspectSeencheck(collection *mongo.Collection, URLs []gocrawlhq.URL, URLType string) ([]gocrawlhq.URL, error) {
// values := []string{}
// for _, URL := range URLs {
// values = append(values, URL.Value)
// }
// result, err := collection.Find(
// context.TODO(),
// bson.M{
// "type": URLType,
// "value": bson.M{
// "$in": values,
// },
// },
// )
// if err != nil {
// panic(err)
// }
// defer result.Close(context.Background())
// URLs = []gocrawlhq.URL{}
// for result.Next(context.Background()) {
// var URL gocrawlhq.URL
// err := result.Decode(&URL)
// if err != nil {
// panic(err)
// }
// URLs = append(URLs, URL)
// }
// }