Skip to content

Commit

Permalink
Connect app to database
Browse files Browse the repository at this point in the history
  • Loading branch information
abneed committed Jul 3, 2022
1 parent fcf05f6 commit 39a2bf8
Show file tree
Hide file tree
Showing 25 changed files with 724 additions and 225 deletions.
Binary file added bookings
Binary file not shown.
24 changes: 18 additions & 6 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/abneed/bookings/helpers"
"github.com/abneed/bookings/internal/config"
"github.com/abneed/bookings/internal/driver"
"github.com/abneed/bookings/internal/handlers"
"github.com/abneed/bookings/internal/models"
"github.com/abneed/bookings/internal/render"
Expand All @@ -25,10 +26,11 @@ var errorLog *log.Logger

// main is the main application function
func main() {
err := run()
db, err := run()
if err != nil {
log.Fatal(err)
}
defer db.SQL.Close()

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
// _ = http.ListenAndServe(portNumber, nil)
Expand All @@ -41,9 +43,12 @@ 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{})
gob.Register(models.User{})
gob.Register(models.Room{})
gob.Register(models.Restriction{})

// change this to true when in production
app.InProduction = false
Expand All @@ -62,19 +67,26 @@ func run() error {

app.Session = session

// connect to database
log.Println("Connecting to database...")
db, err := driver.ConnectSQL("host=localhost port=5432 dbname=bookings user=Abneed password=")
if err != nil {
log.Fatal("Cannot connect to database! Dying...")
}

tc, err := render.CreateTemplateCache()
if err != nil {
log.Fatal("cannot create template cache")
return err
return nil, err
}

app.TemplateCache = tc
app.UseCache = false

repo := handlers.NewRepo(&app)
repo := handlers.NewRepo(&app, db)
handlers.NewHandlers(repo)
render.NewTemplates(&app)
render.NewRenderer(&app)
helpers.NewHelpers(&app)

return nil
return db, nil
}
2 changes: 1 addition & 1 deletion cmd/web/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "testing"

func TestRun(t *testing.T) {
err := run()
_, err := run()
if err != nil {
t.Error("failed run()")
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func routes(app *config.AppConfig) http.Handler {
mux.Get("/search-availability", handlers.Repo.Availability)
mux.Post("/search-availability", handlers.Repo.PostAvailability)
mux.Post("/search-availability-json", handlers.Repo.AvailabilityJSON)
mux.Get("/choose-room/{id}", handlers.Repo.ChooseRoom)
mux.Get("/book-room", handlers.Repo.BookRoom)

mux.Get("/contact", handlers.Repo.Contact)

Expand Down
Loading

0 comments on commit 39a2bf8

Please sign in to comment.