Lint fixes.

This commit is contained in:
2024-11-01 10:10:42 +02:00
parent ef3f009709
commit a0563074ed
9 changed files with 42 additions and 49 deletions

View File

@@ -1,20 +1,29 @@
// func PopulateDB(db *sqlx.DB) { package main
// // Delete all rows in the snapshots table
// db.MustExec("TRUNCATE snapshots;")
// // Prepare the query for inserting a snapshot with uid, url, and timestamp import (
// query := `INSERT INTO snapshots(uid, url, timestamp) "gemini-grc/uid"
// VALUES ($1, $2, $3)` "time"
// // Calculate the timestamp for 2 days ago "github.com/jmoiron/sqlx"
// timestamp := time.Now().Add(-48 * time.Hour) )
// db.MustExec(query, uid.UID(), "gemini://geminiprotocol.net/", timestamp) func PopulateDB(db *sqlx.DB) {
// db.MustExec(query, uid.UID(), "gemini://warmedal.se/~antenna", timestamp) // Delete all rows in the snapshots table
// db.MustExec(query, uid.UID(), "gemini://skyjake.fi/~Cosmos/", timestamp) db.MustExec("TRUNCATE snapshots;")
// db.MustExec(query, uid.UID(), "gemini://gemini.circumlunar.space/capcom/", timestamp)
// db.MustExec(query, uid.UID(), "gemini://auragem.letz.dev/", timestamp) // Prepare the query for inserting a snapshot with uid, url, and timestamp
// db.MustExec(query, uid.UID(), "gemini://gemplex.space/", timestamp) query := `INSERT INTO snapshots(uid, url, timestamp)
// db.MustExec(query, uid.UID(), "gemini://kennedy.gemi.dev/", timestamp) VALUES ($1, $2, $3)`
// db.MustExec(query, uid.UID(), "gemini://tlgs.one/", timestamp)
// } // Calculate the timestamp for 2 days ago
timestamp := time.Now().Add(-48 * time.Hour)
db.MustExec(query, uid.UID(), "gemini://geminiprotocol.net/", timestamp)
db.MustExec(query, uid.UID(), "gemini://warmedal.se/~antenna", timestamp)
db.MustExec(query, uid.UID(), "gemini://skyjake.fi/~Cosmos/", timestamp)
db.MustExec(query, uid.UID(), "gemini://gemini.circumlunar.space/capcom/", timestamp)
db.MustExec(query, uid.UID(), "gemini://auragem.letz.dev/", timestamp)
db.MustExec(query, uid.UID(), "gemini://gemplex.space/", timestamp)
db.MustExec(query, uid.UID(), "gemini://kennedy.gemi.dev/", timestamp)
db.MustExec(query, uid.UID(), "gemini://tlgs.one/", timestamp)
}

View File

@@ -67,7 +67,7 @@ func SaveToFile(rootPath string, s *Snapshot, done chan struct{}) {
urlPath := s.URL.Path urlPath := s.URL.Path
// If path is empty, add `index.gmi` as the file to save // If path is empty, add `index.gmi` as the file to save
if urlPath == "" || urlPath == "." { if urlPath == "" || urlPath == "." {
urlPath = fmt.Sprintf("index.gmi") urlPath = "index.gmi"
} }
// If path ends with '/' then add index.gmi for the // If path ends with '/' then add index.gmi for the
// directory to be created. // directory to be created.

View File

@@ -5,32 +5,10 @@ import (
"fmt" "fmt"
"gemini-grc/logging" "gemini-grc/logging"
"net/url" "net/url"
gourl "net/url"
"regexp" "regexp"
"strconv" "strconv"
"strings"
) )
func isGeminiURL(url string) bool {
_, err := gourl.Parse(url)
if err != nil {
logging.LogWarn("[%s] Invalid URL: %v", url, err)
return false
}
return strings.HasPrefix(url, "gemini://")
}
func parseLinks(s Snapshot, queue chan string) {
for _, link := range *s.Links {
if strings.HasPrefix(link.Full, "gemini://") {
go func(link GeminiUrl) {
// fmt.Printf("LINK: %s\n", link)
queue <- link.Full
}(link)
}
}
}
func checkGeminiStatusCode(code int) error { func checkGeminiStatusCode(code int) error {
switch { switch {
case code == 20: case code == 20:

View File

@@ -62,10 +62,9 @@ func ConnectAndGetData(url string) ([]byte, error) {
} }
// Make sure we always close the connection. // Make sure we always close the connection.
defer func() { defer func() {
err := conn.Close() // No need to handle error:
if err != nil { // Connection will timeout eventually if still open somehow.
// Do nothing! Connection will timeout eventually if still open somehow. conn.Close()
}
}() }()
// Set read and write timeouts on the TCP connection. // Set read and write timeouts on the TCP connection.
@@ -141,7 +140,6 @@ func Visit(s *Snapshot) {
if pageData.Data != nil { if pageData.Data != nil {
s.Data = null.ValueFrom(pageData.Data) s.Data = null.ValueFrom(pageData.Data)
} }
return
} }
// Update given snapshot with the // Update given snapshot with the

View File

@@ -20,6 +20,7 @@ func EnsureValidUTF8(input []byte) (string, error) {
charmap.Windows1252.NewDecoder(), // Then try Windows-1252, etc charmap.Windows1252.NewDecoder(), // Then try Windows-1252, etc
// TODO: Try more encodings? // TODO: Try more encodings?
} }
// First successful conversion wins
for _, encoding := range encodings { for _, encoding := range encodings {
reader := transform.NewReader(bytes.NewReader(inputNoNull), encoding) reader := transform.NewReader(bytes.NewReader(inputNoNull), encoding)
result, err := io.ReadAll(reader) result, err := io.ReadAll(reader)

View File

@@ -57,7 +57,7 @@ func RobotMatch(s *Snapshot) bool {
logging.LogDebug("Checking robots.txt cache for %s", s.URL.String()) logging.LogDebug("Checking robots.txt cache for %s", s.URL.String())
key := fmt.Sprintf("%s:%d", s.Host, s.URL.Port) key := fmt.Sprintf("%s:%d", s.Host, s.URL.Port)
v, ok := RobotsCache.Load(key) v, ok := RobotsCache.Load(key)
if ok == false { if !ok {
// First time check, populate robot cache // First time check, populate robot cache
logging.LogDebug("No robots.txt entry, populating cache for %s", s.URL.String()) logging.LogDebug("No robots.txt entry, populating cache for %s", s.URL.String())
disallowedURLs := populateBlacklist(key) disallowedURLs := populateBlacklist(key)

View File

@@ -58,11 +58,11 @@ func runWorker(id int, db *sqlx.DB) {
logging.LogInfo("[%d] Starting %d/%d %s", id, i+1, total, s.URL) logging.LogInfo("[%d] Starting %d/%d %s", id, i+1, total, s.URL)
err = workOnSnapshot(id, tx, &s) err = workOnSnapshot(id, tx, &s)
if err != nil { if err != nil {
logging.LogError("[%d] [%s] Error %w", id, s.URL, err) logging.LogError("[%d] [%s] Unexpected Error %w", id, s.URL, err)
util.PrintStackAndPanic(err) util.PrintStackAndPanic(err)
} }
if s.Error.Valid { if s.Error.Valid {
logging.LogWarn("[%d] [%s] Error: %v", id, s.URL, fmt.Errorf(s.Error.String)) logging.LogWarn("[%d] [%s] Worker Error: %v", id, s.URL, s.Error.String)
} }
logging.LogDebug("[%d] Done %d/%d.", id, i, total) logging.LogDebug("[%d] Done %d/%d.", id, i, total)
} }

3
lint.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
golangci-lint run

View File

@@ -44,6 +44,10 @@ func runApp() error {
} }
}(db) }(db)
// if len(os.Args) > 1 {
// url := os.Args[1]
// }
// os.Exit(1)
go gemini.SpawnWorkers(config.CONFIG.NumOfWorkers, db) go gemini.SpawnWorkers(config.CONFIG.NumOfWorkers, db)
<-sigs <-sigs