Files
Laterna/cmd/internal/db/connection.go
T
Siesta 57592b4987 feat: a lot
i did a lot of work, and i also forgot to add commits regularly. I added the controller routes to create and delete controllers, improved error handling und improved the authentification process. I'm gonna add an websocket next and then I should be hopefully done with the foundations of this project. I may also add anviewControllers route which respons with every available controller
2025-07-20 18:31:38 +02:00

45 lines
846 B
Go

package db
import (
"database/sql"
"flag"
_ "github.com/mattn/go-sqlite3"
"github.com/siestaw/laterna/server/cmd/internal/logger"
)
var DB *sql.DB
func ConnectDB() {
var err error
DB, err = sql.Open("sqlite3", "./db.sql")
if err != nil {
logger.DBLogger.Printf("An Error occured while connecting to the database: %v", err)
return
}
logger.DBLogger.Printf("Successfully connected to the database!")
InitDB()
resetAdmin := flag.Bool("resetAdminToken", false, "recreate the admin token")
flag.Parse()
if *resetAdmin {
DeleteController(0)
}
}
func InitDB() {
_, err := DB.Exec(`
CREATE TABLE IF NOT EXISTS controllers (
id INTEGER PRIMARY KEY,
token_hash TEXT,
color TEXT,
updated_at DATETIME
);
`)
if err != nil {
logger.DBLogger.Fatalf("An error occured while initializing the database: %v", err)
}
}