From 7152205c80f059d3649e1830fb4dfc46d1fc158f Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 12 Feb 2025 22:05:22 +0100 Subject: template: the goapp docker package should now (in theory) build It's quite a weird way to pull out the packge name from the attribute set of defined packages, yet it kind of works. I can't test it, as docker doesn't want to run Mach-O binaries, but kicking this into hydra should result in some nice builds. --- nix/templates/goapp/frontend/db.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 nix/templates/goapp/frontend/db.go (limited to 'nix/templates/goapp/frontend/db.go') diff --git a/nix/templates/goapp/frontend/db.go b/nix/templates/goapp/frontend/db.go new file mode 100644 index 0000000..fd3605a --- /dev/null +++ b/nix/templates/goapp/frontend/db.go @@ -0,0 +1,37 @@ +package main + +import ( + "database/sql" + "log" + + _ "github.com/mattn/go-sqlite3" +) + +const create string = ` +CREATE TABLE IF NOT EXISTS users ( + id INTEGER NOT NULL PRIMARY KEY, + created_at DATETIME NOT NULL, + name TEXT, + passwordHash TEXT +); +` + +type State struct { + db *sql.DB // the database storing the "business data" + sessions *SqliteStore // the database storing sessions +} + +func NewState() (*State, error) { + db, err := sql.Open("sqlite3", databasePath) + if err != nil { + log.Println("Error opening the db: ", err) + return nil, err + } + if _, err := db.Exec(create); err != nil { + log.Println("Error creating the tables: ", err) + return nil, err + } + return &State{ + db: db, + }, nil +} -- cgit 1.4.1