Add regex matching function to util
This commit is contained in:
14
main.go
14
main.go
@@ -1,12 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
main2 "gemini-grc/db"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"gemini-grc/common"
|
||||||
"gemini-grc/config"
|
"gemini-grc/config"
|
||||||
|
"gemini-grc/db"
|
||||||
"gemini-grc/gemini"
|
"gemini-grc/gemini"
|
||||||
"gemini-grc/logging"
|
"gemini-grc/logging"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@@ -30,7 +31,7 @@ func runApp() error {
|
|||||||
signals := make(chan os.Signal, 1)
|
signals := make(chan os.Signal, 1)
|
||||||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
db := main2.ConnectToDB()
|
_db := db.ConnectToDB()
|
||||||
|
|
||||||
defer func(db *sqlx.DB) {
|
defer func(db *sqlx.DB) {
|
||||||
err := db.Close()
|
err := db.Close()
|
||||||
@@ -38,17 +39,20 @@ func runApp() error {
|
|||||||
// TODO properly log & hangle error
|
// TODO properly log & hangle error
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}(db)
|
}(_db)
|
||||||
|
|
||||||
gemini.LoadBlacklist()
|
gemini.LoadBlacklist()
|
||||||
|
|
||||||
|
common.StatusChan = make(chan common.WorkerStatus, config.CONFIG.NumOfWorkers)
|
||||||
|
|
||||||
// If there's an argument, visit this
|
// If there's an argument, visit this
|
||||||
// URL only and don't spawn other workers
|
// URL only and don't spawn other workers
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
url := os.Args[1]
|
url := os.Args[1]
|
||||||
go gemini.RunWorkerWithTx(0, db, &url)
|
gemini.CrawlOneURL(_db, &url)
|
||||||
|
return nil
|
||||||
} else {
|
} else {
|
||||||
go gemini.SpawnWorkers(config.CONFIG.NumOfWorkers, db)
|
go gemini.SpawnWorkers(config.CONFIG.NumOfWorkers, _db)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-signals
|
<-signals
|
||||||
|
|||||||
10
util/util.go
10
util/util.go
@@ -5,11 +5,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"regexp"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PrintStackAndPanic(err error) {
|
func PrintStackAndPanic(err error) {
|
||||||
fmt.Printf("Error %s Stack trace:\n%s", err, debug.Stack())
|
fmt.Printf("PANIC Error %s Stack trace:\n%s", err, debug.Stack())
|
||||||
panic("PANIC")
|
panic("PANIC")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,3 +35,10 @@ func PrettyJson(data string) string {
|
|||||||
marshalled, _ := json.MarshalIndent(data, "", " ")
|
marshalled, _ := json.MarshalIndent(data, "", " ")
|
||||||
return fmt.Sprintf("%s\n", marshalled)
|
return fmt.Sprintf("%s\n", marshalled)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLinesMatchingRegex returns all lines that match given regex
|
||||||
|
func GetLinesMatchingRegex(input string, pattern string) []string {
|
||||||
|
re := regexp.MustCompile(pattern)
|
||||||
|
matches := re.FindAllString(input, -1)
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user