Files
ddns-updater/pkg/database/init.go
Quentin McGaw ed1823d244 Complete code refactoring
- `LOGGING` environment variable `json` or `human`
- `NODEID` environment variable (integer)
- Much cleaner go code
- Listener for exit of program to do cleanup
- All code is in packages except main.go
- Custom logger package added
- Connectivity checks reworked
- Healthcheck server on localhost only, so not exposed to outside world
- Updated `go.mod` and `go.sum`
2019-04-26 02:04:39 +02:00

37 lines
947 B
Go

package database
import (
"database/sql"
"strings"
)
// A sqlite database is used to store previous IPs, when re launching the program.
// DB contains the database connection pool pointer.
// It is used so that methods are declared on it, in order
// to mock the database easily, through the help of the Datastore interface
// WARNING: Use in one single go routine, it is not thread safe !
type DB struct {
*sql.DB
}
// NewDb opens or creates the database if necessary.
func NewDb(dataDir string) (*DB, error) {
dataDir = strings.TrimSuffix(dataDir, "/")
db, err := sql.Open("sqlite3", dataDir+"/updates.db")
if err != nil {
return nil, err
}
_, err = db.Exec(
`CREATE TABLE IF NOT EXISTS updates_ips (
domain TEXT NOT NULL,
host TEXT NOT NULL,
ip TEXT NOT NULL,
t_new DATETIME NOT NULL,
t_last DATETIME NOT NULL,
current INTEGER DEFAULT 1 NOT NULL,
PRIMARY KEY(domain, host, ip, t_new)
);`)
return &DB{db}, err
}