diff --git a/config.go b/config.go index ed469f9..0fe4629 100644 --- a/config.go +++ b/config.go @@ -18,6 +18,15 @@ type Config struct { TelnetServer bool `yaml:"telnetserver"` FlexRadioSpot bool `yaml:"flexradiospot"` } `yaml:"general"` + Database struct { + MySQL bool `yaml:"mysql"` + SQLite bool `yaml:"sqlite"` + MySQLUser string `yaml:"mysql_db_user"` + MySQLPassword string `yaml:"mysql_db_password"` + MySQLDbName string `yaml:"mysql_db_name"` + MySQLHost string `yaml:"mysql_host"` + MySQLPort string `yaml:"mysql_port"` + } `yaml:"database"` SQLite struct { SQLitePath string `yaml:"sqlite_path"` Callsign string `yaml:"callsign"` diff --git a/config.yml b/config.yml index 0c512d3..3630689 100644 --- a/config.yml +++ b/config.yml @@ -1,9 +1,17 @@ general: delete_log_file_at_start: true log_to_file: true - log_level: INFO # INFO or DEBUG or WARN + log_level: DEBUG # INFO or DEBUG or WARN telnetserver: true # not in use for now flexradiospot: true # not in use for now +database: + mysql: true #only one of the two can be true + sqlite: false + mysql_db_user: rouggy + mysql_db_password: 89DGgg290379 + mysql_db_name: log_f4bpo + mysql_host: 10.10.10.15 + mysql_port: 3306 sqlite: sqlite_path: 'C:\Perso\Seafile\Radio\Logs\Log4OM\F4BPO.SQLite' # SQLite Db oath of Log4OM callsign: F4BPO # Log4OM Callsign used to check if you get spotted by someone diff --git a/database.go b/database.go index 2894841..2190e48 100644 --- a/database.go +++ b/database.go @@ -9,6 +9,8 @@ import ( "sync" "time" + _ "github.com/go-sql-driver/mysql" + log "github.com/sirupsen/logrus" ) @@ -37,18 +39,33 @@ type FlexDXClusterRepository struct { } func NewLog4OMContactsRepository(filePath string) *Log4OMContactsRepository { - db, err := sql.Open("sqlite3", filePath) - if err != nil { - Log.Errorf("Cannot open db", err) - } - _, err = db.Exec("PRAGMA journal_mode=WAL") - if err != nil { - panic(err) + + if Cfg.Database.MySQL { + db, err := sql.Open("mysql", Cfg.Database.MySQLUser+":"+Cfg.Database.MySQLPassword+"@tcp("+Cfg.Database.MySQLHost+":"+Cfg.Database.MySQLPort+")/"+Cfg.Database.MySQLDbName) + if err != nil { + Log.Errorf("Cannot open db", err) + } + + return &Log4OMContactsRepository{ + db: db, + Log: Log} + + } else if Cfg.Database.SQLite { + db, err := sql.Open("sqlite3", filePath) + if err != nil { + Log.Errorf("Cannot open db", err) + } + _, err = db.Exec("PRAGMA journal_mode=WAL") + if err != nil { + panic(err) + } + + return &Log4OMContactsRepository{ + db: db, + Log: Log} } - return &Log4OMContactsRepository{ - db: db, - Log: Log} + return nil } func NewFlexDXDatabase(filePath string) *FlexDXClusterRepository { @@ -95,6 +112,16 @@ func NewFlexDXDatabase(filePath string) *FlexDXClusterRepository { } } +func (r *Log4OMContactsRepository) CountEntries() int { + var contacts int + _ = r.db.QueryRow("SELECT COUNT(*) FROM log").Scan(&contacts) + if err != nil { + log.Error("could not query database", err) + } + + return contacts +} + func (r *Log4OMContactsRepository) ListByCountry(countryID string, contactsChan chan []Contact, wg *sync.WaitGroup) { defer wg.Done() rows, err := r.db.Query("SELECT callsign, band, mode, dxcc, stationcallsign, country FROM log WHERE dxcc = ?", countryID) diff --git a/go.mod b/go.mod index 1c20be6..61cbae9 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.rouggy.com/rouggy/FlexDXCluster go 1.23.1 require ( + github.com/go-sql-driver/mysql v1.9.3 github.com/mattn/go-sqlite3 v1.14.23 github.com/sirupsen/logrus v1.9.3 github.com/x-cray/logrus-prefixed-formatter v0.5.2 @@ -10,6 +11,7 @@ require ( ) require ( + filippo.io/edwards25519 v1.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect diff --git a/go.sum b/go.sum index 63ff35a..4bbb3d3 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,13 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= +github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= diff --git a/main.go b/main.go index fa882b6..7eaa146 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,7 @@ func main() { cfg := NewConfig(cfgPath) log := NewLog() - log.Info("Running FlexDXCluster version 0.5") + log.Info("Running FlexDXCluster version 0.6") log.Infof("Callsign: %s", cfg.SQLite.Callsign) DeleteDatabase("./flex.sqlite", log) @@ -61,6 +61,8 @@ func main() { // Database connection to Log4OM cRepo := NewLog4OMContactsRepository(cfg.SQLite.SQLitePath) + contacts := cRepo.CountEntries() + log.Infof("Log4OM Database Contains %v Contacts", contacts) defer cRepo.db.Close() TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port) diff --git a/spot.go b/spot.go index fef8060..089242a 100644 --- a/spot.go +++ b/spot.go @@ -27,7 +27,10 @@ type TelnetSpot struct { CallsignWorked bool } +// var spotNumber = 1 + func ProcessTelnetSpot(re *regexp.Regexp, spotRaw string, SpotChanToFlex chan TelnetSpot, SpotChanToHTTPServer chan TelnetSpot, Countries Countries) { + match := re.FindStringSubmatch(spotRaw) if len(match) != 0 { @@ -144,6 +147,8 @@ func ProcessTelnetSpot(re *regexp.Regexp, spotRaw string, SpotChanToFlex chan Te // Log.Infof("Could not decode: %s", strings.Trim(spotRaw, "\n")) } + // Log.Infof("Spots Processed: %v", spotNumber) + // spotNumber++ } func (spot *TelnetSpot) GetBand() {