-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"github.com/gweebg/ipwatcher/internal/config" | ||
"github.com/gweebg/ipwatcher/internal/fetch" | ||
"github.com/gweebg/ipwatcher/internal/database" | ||
"github.com/gweebg/ipwatcher/internal/utils" | ||
"log" | ||
) | ||
|
||
func main() { | ||
|
||
config.Init() | ||
//if len(os.Args) > 1 { | ||
// | ||
// command := os.Args[1] | ||
// if command == "add-source" { | ||
// // ! Execute CLI for source addition. | ||
// // ! Maybe use a flag set to separate flags. | ||
// // ! source := flag.NewFlagSet("add-source", flag.ExitOnError) | ||
// } | ||
// | ||
// return | ||
//} | ||
|
||
const version string = "v6" | ||
addr, err := fetch.RequestAddress(version) | ||
configFlags := map[string]interface{}{} | ||
|
||
configFlags["version"] = flag.String( | ||
"version", | ||
"v4", | ||
"version of the IP protocol, supports 'v4' | 'v6' | 'all'", | ||
) | ||
|
||
configFlags["config"] = flag.String( | ||
"config", | ||
"config.yml", | ||
"path to the configuration file", | ||
) | ||
|
||
configFlags["exec"] = flag.String( | ||
"exec", | ||
"on_change", | ||
"run executable/script upon an event, supports 'on_change' | 'on_same' | 'always' | 'never'", | ||
) | ||
|
||
configFlags["api"] = flag.Bool( | ||
"api", | ||
false, | ||
"expose an api with information relative to the changes", | ||
) | ||
|
||
configFlags["notify"] = flag.Bool( | ||
"notify", | ||
false, | ||
"enable email notifications, must be configured in settings", | ||
) | ||
|
||
flag.Parse() | ||
|
||
config.Init(configFlags) | ||
|
||
database.ConnectDatabase() | ||
db := database.GetDatabase() | ||
|
||
err := db.AutoMigrate(&database.AddressEntry{}) | ||
utils.Check(err, "could not run AutoMigrate") | ||
|
||
utils.Check(err, "") | ||
log.Println(addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package database | ||
|
||
import ( | ||
"github.com/gweebg/ipwatcher/internal/utils" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var db *gorm.DB | ||
|
||
func ConnectDatabase() { | ||
var err error | ||
|
||
db, err = gorm.Open(sqlite.Open("watcher.db"), &gorm.Config{}) | ||
utils.Check(err, "") | ||
} | ||
|
||
func GetDatabase() *gorm.DB { | ||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package database | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
type AddressEntry struct { | ||
gorm.Model | ||
|
||
// ID of the record, auto incremented uint64 value | ||
ID uint64 `gorm:"primaryKey;autoIncrement:true"` | ||
|
||
// Address is the newly fetched address that differs from the previous registered | ||
Address string `gorm:"index" json:"address"` | ||
// PreviousAddress is the previous address, before the update | ||
PreviousAddress string `json:"previous_address"` | ||
|
||
// Source is the URL of the API that provided the updated address | ||
Source string `json:"source"` | ||
// Version specifies the version of the address this record refers to | ||
Version string `json:"version"` | ||
// CreatedAt is the UNIX time when the address update was detected | ||
CreatedAt uint64 `gorm:"autoCreateTime" json:"at"` | ||
} | ||
|
||
// Create is the function that creates a new AddressEntry record onto the database | ||
func (e AddressEntry) Create(address string) (*AddressEntry, error) { | ||
|
||
database := GetDatabase() | ||
entry := AddressEntry{Address: address} | ||
|
||
if err := database.Create(&entry).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return &entry, nil | ||
} | ||
|
||
// First returns the latest added record for a specific address version (addressVersion) | ||
func (e AddressEntry) First(addressVersion string) (*AddressEntry, error) { | ||
|
||
database := GetDatabase() | ||
|
||
var entry AddressEntry | ||
|
||
query := database. | ||
Where("version = ?", addressVersion). | ||
Order("id desc"). | ||
First(&entry) | ||
|
||
if query.Error != nil { | ||
return nil, query.Error | ||
} | ||
|
||
return &entry, nil | ||
|
||
} |