web-crawler/main.go

51 lines
943 B
Go
Raw Normal View History

2024-08-26 10:30:14 +01:00
package main
import (
"fmt"
"os"
"strconv"
2024-08-26 10:30:14 +01:00
"codeflow.dananglin.me.uk/apollo/web-crawler/internal/crawler"
2024-08-26 10:30:14 +01:00
)
func main() {
if err := run(); err != nil {
os.Stderr.WriteString("ERROR: " + err.Error() + "\n")
2024-08-26 10:30:14 +01:00
os.Exit(1)
}
}
func run() error {
args := os.Args[1:]
if len(args) != 3 {
return fmt.Errorf("unexpected number of arguments received: want 3, got %d", len(args))
}
baseURL := args[0]
maxConcurrency, err := strconv.Atoi(args[1])
if err != nil {
return fmt.Errorf("unable to convert the max concurrency (%s) to an integer: %w", args[1], err)
}
maxPages, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("unable to convert the max pages (%s) to an integer: %w", args[2], err)
}
c, err := crawler.NewCrawler(baseURL, maxConcurrency, maxPages)
if err != nil {
return fmt.Errorf("unable to create the crawler: %w", err)
}
go c.Crawl(baseURL)
c.Wait()
c.PrintReport()
2024-08-26 10:30:14 +01:00
return nil
}