From c113043073dea92c9c705a86064c05364aa6902e Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Mon, 3 Jun 2024 10:19:45 +0100 Subject: Rips out Postgres - but leaves migration files for now - this does not yet remove the models - uses sqlite3 driver - changes the config based on sqlite3 - tests pass but no depth in investigating the models The sqlite3 database is going to be used for storing user data and tokens only - we are not going to store the models in a database at this point. If we want to start saving datamaps, datamaplines, returns and returnlines, etc, we will need the models but as of this commit the models are still in the code. --- cmd/dbasik-api/main.go | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'cmd/dbasik-api') diff --git a/cmd/dbasik-api/main.go b/cmd/dbasik-api/main.go index b4850f8..066c2e6 100644 --- a/cmd/dbasik-api/main.go +++ b/cmd/dbasik-api/main.go @@ -32,7 +32,7 @@ import ( "time" "github.com/joho/godotenv" - _ "github.com/lib/pq" + _ "github.com/mattn/go-sqlite3" ) const version = "0.0.1" @@ -42,12 +42,7 @@ const version = "0.0.1" type config struct { port int env string - db struct { - dsn string - maxOpenConns int - maxIdleConns int - maxIdleTime string - } + db string } // This application struct holds the dependencies for our HTTP handlers, helpers and @@ -70,12 +65,7 @@ func main() { // Read the flags into the config struct. Defaults are provided if none given. flag.IntVar(&cfg.port, "port", 5000, "API server port") flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)") - flag.StringVar(&cfg.db.dsn, "db-dsn", os.Getenv("DBASIK_DB_DSN"), "PostgreSQL DSN") - - //Read connection pool settings (explained in Configuring the pool in the book) - flag.IntVar(&cfg.db.maxOpenConns, "db-max-open-conns", 25, "PostgreSQL max open connections") - flag.IntVar(&cfg.db.maxIdleConns, "db-max-idle-conns", 25, "PostgreSQL max idle connections") - flag.StringVar(&cfg.db.maxIdleTime, "db-max-idle-time", "15m", "PostgreSQL max connection idle time") + flag.StringVar(&cfg.db, "db-dsn", os.Getenv("DBASIK_DB_DSN"), "sqlite3 DSN") flag.Parse() @@ -118,19 +108,10 @@ func main() { } func openDB(cfg config) (*sql.DB, error) { - db, err := sql.Open("postgres", cfg.db.dsn) - if err != nil { - return nil, err - } - - // set up pool config - db.SetMaxOpenConns(cfg.db.maxOpenConns) - db.SetMaxIdleConns(cfg.db.maxIdleConns) - duration, err := time.ParseDuration(cfg.db.maxIdleTime) + db, err := sql.Open("sqlite3", cfg.db) if err != nil { return nil, err } - db.SetConnMaxIdleTime(duration) // create a context with a 5 second timeout // if the database hasn't connected within this time, there is a problem -- cgit v1.2.3