commit 9f0435057e5ffde0e43445d8e10eedd15088fc4e Author: PhatPhuckDave Date: Sun Oct 27 17:08:58 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1269488 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data diff --git a/db.go b/db.go new file mode 100644 index 0000000..436c7d2 --- /dev/null +++ b/db.go @@ -0,0 +1,84 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + "os" + "time" + + "github.com/mattn/go-sqlite3" +) + +type DB struct { + Ready bool + path string + readConn *sql.DB + writeConn *sql.DB +} + +func (db *DB) Open() error { + if db.path == "" { + return fmt.Errorf("database path not set") + } + + file, err := os.Open(db.path) + if err != nil { + if os.IsNotExist(err) { + log.Printf("Database file does not exist at %s, creating", db.path) + file, err := os.Create(db.path) + if err != nil { + return fmt.Errorf("failed to create database file: %v", err) + } + log.Printf("Database created at %s", db.path) + file.Close() + } else { + return fmt.Errorf("failed to open database file: %v", err) + } + } + file.Close() + + writeConn, err := sql.Open("sqlite3", db.path+"?_journal=WAL&_synchronous=NORMAL") + if err != nil { + Error.Printf("%++v", err) + return err + } + writeConn.SetMaxOpenConns(1) + writeConn.SetConnMaxIdleTime(30 * time.Second) + writeConn.SetConnMaxLifetime(30 * time.Second) + db.writeConn = writeConn + + readConn, err := sql.Open("sqlite3", db.path+"?mode=ro&_journal=WAL&_synchronous=NORMAL&_mode=ro") + if err != nil { + Error.Printf("%++v", err) + return err + } + readConn.SetMaxOpenConns(4) + readConn.SetConnMaxIdleTime(30 * time.Second) + readConn.SetConnMaxLifetime(30 * time.Second) + db.readConn = readConn + + db.Ready = true + return nil +} + +func (db *DB) Init(ddl string) error { + if !db.Ready { + return fmt.Errorf("database not ready") + } + return nil +} + +func (db *DB) Close() error { + err := db.writeConn.Close() + if err != nil { + return err + } + + err = db.readConn.Close() + if err != nil { + return err + } + + return nil +} \ No newline at end of file diff --git a/ddl.sql b/ddl.sql new file mode 100644 index 0000000..a6010b6 --- /dev/null +++ b/ddl.sql @@ -0,0 +1,17 @@ +create table guild ( + id integer primary key, + name text +); + +create table stinky ( + id integer primary key, + name text, + guild integer references guild(id) +); + +create table note ( + id integer primary key, + content text, + timestamp string, + stinky integer references stinky(id) +); \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..982346c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module stinkinator + +go 1.23.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..47251bb --- /dev/null +++ b/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "io" + "log" + "os" +) + +var Error *log.Logger +var Warning *log.Logger +func init() { + log.SetFlags(log.Lmicroseconds | log.Lshortfile) + logFile, err := os.Create("main.log") + if err != nil { + log.Printf("Error creating log file: %v", err) + os.Exit(1) + } + logger := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(logger) + + Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout), + fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"), + log.Lmicroseconds|log.Lshortfile) + Warning = log.New(io.MultiWriter(logFile, os.Stdout), + fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"), + log.Lmicroseconds|log.Lshortfile) +} + +func main() { + log.Println("Hello, World!") + Warning.Println("Hello, World!") + Error.Println("Hello, World!") +} \ No newline at end of file