Persist pages to file system
This commit is contained in:
31
network.go
31
network.go
@@ -7,35 +7,42 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func Visit(url string) (result *Result) {
|
||||
result = &Result{}
|
||||
func Visit(url string) (result *Snapshot) {
|
||||
result = &Snapshot{Timestamp: time.Now(), UID: UID()}
|
||||
|
||||
// Wrap error with additional information
|
||||
defer func() {
|
||||
if result.error != nil {
|
||||
result.error = fmt.Errorf("[%s] Error: %w", result.url, result.error)
|
||||
if result.Error != nil {
|
||||
result.Error = fmt.Errorf("[%s] Error: %w", result.Url, result.Error)
|
||||
}
|
||||
}()
|
||||
|
||||
geminiUrl, err := ParseUrl(url, "")
|
||||
if err != nil {
|
||||
result.error = err
|
||||
result.Error = err
|
||||
return result
|
||||
}
|
||||
result.url = *geminiUrl
|
||||
result.Url = *geminiUrl
|
||||
|
||||
LogInfo("[%s] Dialing", geminiUrl.String())
|
||||
LogInfo("[%s] Dialing", geminiUrl)
|
||||
|
||||
// Establish a TLS connection
|
||||
tlsConfig := &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", geminiUrl.hostname, geminiUrl.port), tlsConfig)
|
||||
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", geminiUrl.Hostname, geminiUrl.Port), tlsConfig)
|
||||
if err != nil {
|
||||
result.error = err
|
||||
result.Error = err
|
||||
return result
|
||||
}
|
||||
defer conn.Close()
|
||||
// Defer properly: Also handle possible
|
||||
// error of conn.Close()
|
||||
defer func() {
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
result.Error = fmt.Errorf("[%s] Closing connection error, ignoring: %w", result.Url.String(), err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Read data from the connection
|
||||
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
||||
@@ -53,7 +60,7 @@ func Visit(url string) (result *Result) {
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else {
|
||||
result.error = err
|
||||
result.Error = err
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -61,6 +68,6 @@ func Visit(url string) (result *Result) {
|
||||
LogInfo("[%s] Received %d bytes", geminiUrl.String(), len(data))
|
||||
// time.Sleep(time.Duration(time.Second * 2))
|
||||
// LogDebug("[%s] Visitor finished", geminiUrl.String())
|
||||
result.data = string(data)
|
||||
result.Data = string(data)
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user