diff --git a/cmd/web/main.go b/cmd/web/main.go index fe10a1c..bb974aa 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -4,6 +4,7 @@ import ( "encoding/gob" "fmt" "go-udemy-web-1/internal/config" + "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/handlers" "go-udemy-web-1/internal/helpers" "go-udemy-web-1/internal/models" @@ -27,10 +28,11 @@ var ( // main is the main application function func main() { - err := run() + db, err := run() if err != nil { log.Fatal(err) } + defer db.SQL.Close() fmt.Printf("Starting application on port %s\n", portNumber) @@ -43,7 +45,7 @@ func main() { log.Fatal(err) } -func run() error { +func run() (*driver.DB, error) { // what am I going to put in the session gob.Register(models.Reservation{}) @@ -58,9 +60,19 @@ func run() error { app.Session = session + // connect to database + log.Println("Connecting to database...") + dsn := fmt.Sprintf("host=localhost port=5432 dbname=test_connect user=%s password=%s", os.Getenv("PGUSER"), os.Getenv("PGPWD")) + db, err := driver.ConnectSQL(dsn) + if err != nil { + log.Fatal("Cannot connect to database! Dying...") + } + log.Println("Connected to database") + tc, err := render.CreateTemplateCache() if err != nil { log.Fatalf("cannot create template cache: %s", err) + return nil, err } app.TemplateCahce = tc app.UseCache = false @@ -71,10 +83,10 @@ func run() error { errorLog = log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile) app.ErrorLog = errorLog - repo := handlers.NewRepo(&app) + repo := handlers.NewRepo(&app, db) handlers.NewHandlers(repo) helpers.NewHelpers(&app) render.NewTemplates(&app) - return nil + return db, nil } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 7f86bc7..b034f0e 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -4,10 +4,13 @@ import ( "encoding/json" "fmt" "go-udemy-web-1/internal/config" + "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/forms" "go-udemy-web-1/internal/helpers" "go-udemy-web-1/internal/models" "go-udemy-web-1/internal/render" + "go-udemy-web-1/internal/repository" + "go-udemy-web-1/internal/repository/dbrepo" "net/http" ) @@ -17,12 +20,14 @@ var Repo *Repository // Repository is the repository type type Repository struct { App *config.AppConfig + DB repository.DatabaseRepo } // NewRepo creates a new repository -func NewRepo(a *config.AppConfig) *Repository { +func NewRepo(a *config.AppConfig, db *driver.DB) *Repository { return &Repository{ App: a, + DB: dbrepo.NewPostgresRepo(db.SQL, a), } } diff --git a/internal/repository/dbrepo/dbrepo.go b/internal/repository/dbrepo/dbrepo.go new file mode 100644 index 0000000..6564ec0 --- /dev/null +++ b/internal/repository/dbrepo/dbrepo.go @@ -0,0 +1,19 @@ +package dbrepo + +import ( + "database/sql" + "go-udemy-web-1/internal/config" + "go-udemy-web-1/internal/repository" +) + +type postgresDBRepo struct { + App *config.AppConfig + DB *sql.DB +} + +func NewPostgresRepo(conn *sql.DB, a *config.AppConfig) repository.DatabaseRepo { + return &postgresDBRepo{ + App: a, + DB: conn, + } +} diff --git a/internal/repository/dbrepo/postgres.go b/internal/repository/dbrepo/postgres.go new file mode 100644 index 0000000..a080109 --- /dev/null +++ b/internal/repository/dbrepo/postgres.go @@ -0,0 +1,5 @@ +package dbrepo + +func (m *postgresDBRepo) AllUsers() bool { + return true +} diff --git a/internal/repository/repository.go b/internal/repository/repository.go new file mode 100644 index 0000000..b8404e3 --- /dev/null +++ b/internal/repository/repository.go @@ -0,0 +1,5 @@ +package repository + +type DatabaseRepo interface { + AllUsers() bool +}