diff options
| author | demo <demo@antix1> | 2026-05-07 21:57:13 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-07 21:57:13 -0400 |
| commit | 00a2f4555f81257c8043c74d9a6a0428a049339a (patch) | |
| tree | 7522dd9a1d9df81bfdedcd51881158d9a9241a35 | |
| parent | 5addbf869a466e2bd62b543c7350394bc2027be6 (diff) | |
feat: use stub client code instead
I guess I got a little carried away last commit - I'm writing a
client, not a server.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | main.go | 47 |
2 files changed, 41 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc65166 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/links
\ No newline at end of file @@ -2,25 +2,58 @@ package main import ( "flag" - "fmt" + "io" "log" "net/http" + "time" ) +type Link struct { + Href string + Text string +} + func main() { - port := flag.Int("port", 8080, "Server port") + // Logging configuration. + log.SetFlags(log.LstdFlags | log.Lshortfile) + + // CLI flag configuration. rawURL := flag.String("url", "", "Web address of target HTML") + timeoutSecs := flag.Int("timeout", 2, "Number of seconds after which to time out") if *rawURL == "" { log.Fatal("Missing -url") } - mux := http.NewServeMux() + // Configure the request. + timeout := time.Duration(*timeoutSecs) * time.Second + client := http.Client{ + Timeout: timeout, + } + + req, err := http.NewRequest(http.MethodGet, *rawURL, nil) + if err != nil { + log.Fatal(err) + } + + // Perform the request. + resp, err := client.Do(req) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() - srv := http.Server{ - Addr: fmt.Sprintf(":%d", *port), - Handler: mux, + links, err := findLinks(resp.Body) + if err != nil { + log.Fatal(err) } - log.Fatal(srv.ListenAndServe()) + _ = links +} + +// findLinks consumes the given reader, scraping it of anchor +// tags. Each anchor tag is "unmarshalled" into a [Link]. The +// resulting slice of Links is returned, along with an error. +func findLinks(_ io.Reader) ([]Link, error) { + return nil, nil } |
