Created
June 10, 2020 19:00
-
-
Save u7ter/bf55578d20ceaaa23e664407d5730d5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "crypto/tls" | |
| log "github.com/sirupsen/logrus" | |
| "go.mongodb.org/mongo-driver/mongo" | |
| "go.mongodb.org/mongo-driver/mongo/options" | |
| "go.mongodb.org/mongo-driver/bson" | |
| "net/http" | |
| "time" | |
| ) | |
| type Item struct { | |
| Id string `bson:"_id,omitempty"` | |
| Url string `bson:"location"` | |
| } | |
| func main() { | |
| client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://root:example@localhost/?authSource=admin")) | |
| if err != nil { | |
| log.Error(err) | |
| } | |
| ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) | |
| err = client.Connect(ctx) | |
| if err != nil { | |
| log.Error(err) | |
| } | |
| defer client.Disconnect(ctx) | |
| coll := client.Database("analytics").Collection("stats") | |
| item := Item{} | |
| notesResult := []Item{} | |
| cursor, err := coll.Find(ctx, bson.M{}) | |
| if err != nil { | |
| log.Error(err) | |
| return | |
| } | |
| // Iterate through the returned cursor. | |
| for cursor.Next(ctx) { | |
| cursor.Decode(&item) | |
| notesResult = append(notesResult, item) | |
| } | |
| queue := make(chan string) | |
| for _, el := range notesResult { | |
| go func() { | |
| queue <- "https://site.com/" + el.Id | |
| }() | |
| } | |
| for uri := range queue { | |
| enqueue(uri, queue) | |
| } | |
| } | |
| func enqueue(uri string, queue chan string) { | |
| tlsConfig := &tls.Config{ | |
| InsecureSkipVerify: true, | |
| } | |
| transport := &http.Transport{ | |
| TLSClientConfig: tlsConfig, | |
| } | |
| client := http.Client{Transport: transport} | |
| resp, err := client.Get(uri) | |
| if err != nil { | |
| return | |
| } | |
| defer resp.Body.Close() | |
| if resp.StatusCode != 200 { | |
| log.WithFields(log.Fields{ | |
| "status": resp.StatusCode, | |
| }).Error("uri: "+ uri) | |
| } else { | |
| log.WithFields(log.Fields{ | |
| "status": resp.StatusCode, | |
| }).Info("uri: "+ uri) | |
| } | |
| //links := collectlinks.All(resp.Body) | |
| // | |
| //for _, link := range links { | |
| // go func() { queue <- link }() | |
| //} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment