-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (55 loc) · 1.18 KB
/
main.go
File metadata and controls
66 lines (55 loc) · 1.18 KB
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
package main
import (
"fmt"
"net/url"
"os"
"strconv"
"sync"
)
type config struct {
pages map[string]int
baseURL *url.URL
mu *sync.Mutex
concurrencyControl chan struct{}
wg *sync.WaitGroup
maxPages int
}
func main() {
cmdArgs := os.Args[1:]
if len(cmdArgs) < 3 {
fmt.Println("not enough arguments provided")
os.Exit(1)
} else if len(cmdArgs) > 3 {
fmt.Println("too many arguments provided")
os.Exit(1)
}
baseURL := cmdArgs[0]
maxConcurrent, err := strconv.Atoi(cmdArgs[1])
if err != nil {
fmt.Println(err)
return
}
maxPages, err := strconv.Atoi(cmdArgs[2])
if err != nil {
fmt.Println(err)
return
}
parsedURL, err := url.Parse(baseURL)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cfg := config{
pages: make(map[string]int),
baseURL: parsedURL,
mu: &sync.Mutex{},
concurrencyControl: make(chan struct{}, maxConcurrent),
wg: &sync.WaitGroup{},
maxPages: maxPages,
}
fmt.Printf("starting crawl\n%v\n", baseURL)
cfg.wg.Add(1)
go cfg.crawlPage(baseURL)
cfg.wg.Wait()
printReport(cfg.pages, baseURL)
}