Skip to content

Commit

Permalink
Refactor log import function and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
abaldeweg authored Feb 3, 2025
1 parent df4e09c commit 264b735
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions logs_import/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ import (
"github.com/spf13/viper"
)

func ImportLogs() {
entries, err := parser.ReadLogEntries()
if err != nil {
func Import() {
if err := importLogs(); err != nil {
log.Println(err)
os.Exit(1)
}

fmt.Println("\033[32mLogs successfully imported!\033[0m")
os.Exit(0)
}

func importLogs() error {
entries, err := parser.ReadLogEntries()
if err != nil {
return err
}

db, err := db.NewDBHandler()
if err != nil {
log.Println(err)
os.Exit(1)
return err
}
defer db.Close()

for _, entry := range entries {
if !isBlocked(entry.RequestPath) {
if err := db.Add(entry); err != nil {
log.Println(err)
os.Exit(1)
return err
}
}
}

fmt.Println("\033[32mLogs successfully imported!\033[0m")
os.Exit(0)
return nil
}

func isBlocked(host string) bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package importer

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion logs_import/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

fmt.Println("Blocklist:", viper.GetStringSlice("blocklist"))

go importer.ImportLogs()
go importer.Import()

for {
time.Sleep(1 * time.Second)
Expand Down

0 comments on commit 264b735

Please sign in to comment.