Skip to content

Commit

Permalink
sqlite database setup
Browse files Browse the repository at this point in the history
  • Loading branch information
gweebg committed Jan 28, 2024
1 parent b1e3622 commit 7ae27e8
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 7 deletions.
61 changes: 54 additions & 7 deletions cmd/main.go
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)
}
20 changes: 20 additions & 0 deletions internal/database/database.go
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
}
57 changes: 57 additions & 0 deletions internal/database/models.go
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

}

0 comments on commit 7ae27e8

Please sign in to comment.