diff --git a/cmd/main.go b/cmd/main.go index 1d619dd..0beabe2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1 +1,32 @@ -package cmd +package main + +import ( + "flag" + + "github.com/lib/pq" + "tomatentum.net/outfit-voting-abi26/internal/database" +) + +var config pq.Config + +func main() { + handleflag() + + database.OpenDB(config) + defer database.CloseDB() + + for { + + } +} + +func handleflag() { + config.SSLMode = pq.SSLModePrefer + flag.StringVar(&config.Host, "dbhost", "localhost", "Postgres host") + config.Port = uint16(*flag.Uint("dbport", 5432, "Postgres port")) + flag.StringVar(&config.Database, "dbname", "postgres", "Postgres db name") + flag.StringVar(&config.User, "dbuser", "postgres", "Postgres user") + flag.StringVar(&config.Password, "dbpass", "", "Postgres password") + + flag.Parse() +} diff --git a/go.mod b/go.mod index 3b10bd6..bf050a2 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module tomatentum.net/outfit-voting-abi26 go 1.25.5 + +require github.com/lib/pq v1.12.3 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c7cd147 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ= +github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA= diff --git a/internal/database/database.go b/internal/database/database.go new file mode 100644 index 0000000..5a37928 --- /dev/null +++ b/internal/database/database.go @@ -0,0 +1,31 @@ +package database + +import ( + "database/sql" + "log" + + "github.com/lib/pq" +) + +var db *sql.DB + +func OpenDB(conf pq.Config) { + connector, err := pq.NewConnectorConfig(conf) + + if err != nil { + panic(err) + } + db = sql.OpenDB(connector) + + if err := db.Ping(); err != nil { + panic(err) + } + + log.Printf("Database opened to %s:%d, %s as %s!\n", conf.Host, conf.Port, conf.Database, conf.User) +} + +func CloseDB() { + if err := db.Close(); err != nil { + panic(err) + } +} diff --git a/internal/database/state.go b/internal/database/state.go new file mode 100644 index 0000000..e69de29