~evanj/cms

ref: 34b3875dddf3e9096221b155abcb25ad2e7aeecc cms/internal/s/db/db.go -rw-r--r-- 824 bytes
34b3875dEvan M Jones Feat(*): Project init. 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package db

import (
	"database/sql"
	"log"

	"git.sr.ht/~evanj/security"
	_ "github.com/mattn/go-sqlite3"
)

type DB struct {
	*sql.DB
	log *log.Logger
	sec securer
}

type securer interface {
	TokenCreate(val security.TokenMap) (string, error)
	TokenFrom(tokenString string) (security.TokenMap, error)
	HashCreate(salt, pass string) (string, error)
	HashCompare(salt, pass, hash string) error
}

func New(log *log.Logger, creds string, sec securer) (*DB, error) {
	conn, err := sql.Open("sqlite3", creds)
	if err != nil {
		return nil, err
	}

	db := &DB{
		conn,
		log,
		sec,
	}

	return db, nil
}

func (db *DB) EnsureSetup() error {
	res, err := db.Exec(`
		CREATE TABLE cms_user (
			ID INTEGER PRIMARY KEY AUTOINCREMENT,
			NAME TEXT UNIQUE NOT NULL,
			HASH TEXT NOT NULL
		);
	`)

	_ = res
	_ = err

	return nil
}