license-server/storage/db.go

35 lines
831 B
Go

package storage
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type Database struct {
db *sql.DB
}
func InitDB() Database {
db, _ := sql.Open("sqlite3", "./license.db")
db.Exec(`CREATE TABLE IF NOT EXISTS activations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
machine_id TEXT UNIQUE,
license TEXT,
activated INTEGER
)`)
return Database{db}
}
func (d Database) HasActivated(machineID string) bool {
row := d.db.QueryRow("SELECT activated FROM activations WHERE machine_id = ?", machineID)
var activated int
err := row.Scan(&activated)
return err == nil && activated == 1
}
func (d Database) SaveActivation(machineID string, licenseText string) {
d.db.Exec("INSERT OR REPLACE INTO activations(machine_id, license, activated) VALUES (?, ?, 1)",
machineID, licenseText)
}