Created
October 27, 2020 07:52
-
-
Save alexedwards/d42ae90aac9dfa75046ebf8a036b080b to your computer and use it in GitHub Desktop.
Dependency injection via closure (handlers in multiple packages)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── go.mod | |
├── handlers | |
│ ├── books | |
│ │ └── books.go | |
│ └── env.go | |
├── main.go | |
└── models | |
└── models.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package books | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"bookstore.alexedwards.net/handlers" | |
"bookstore.alexedwards.net/models" | |
) | |
func Index(env *handlers.Env) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
bks, err := models.AllBooks(env.DB) | |
if err != nil { | |
log.Println(err) | |
http.Error(w, http.StatusText(500), 500) | |
return | |
} | |
for _, bk := range bks { | |
fmt.Fprintf(w, "%s, %s, %s, £%.2f\n", bk.Isbn, bk.Title, bk.Author, bk.Price) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handlers | |
import ( | |
"database/sql" | |
) | |
type Env struct { | |
DB *sql.DB | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module bookstore.alexedwards.net | |
go 1.15 | |
require github.com/lib/pq v1.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"log" | |
"net/http" | |
"bookstore.alexedwards.net/handlers" | |
"bookstore.alexedwards.net/handlers/books" | |
_ "github.com/lib/pq" | |
) | |
func main() { | |
db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore") | |
if err != nil { | |
log.Fatal(err) | |
} | |
env := &handlers.Env{DB: db} | |
http.Handle("/books", books.Index(env)) | |
http.ListenAndServe(":3000", nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import ( | |
"database/sql" | |
) | |
type Book struct { | |
Isbn string | |
Title string | |
Author string | |
Price float32 | |
} | |
func AllBooks(db *sql.DB) ([]Book, error) { | |
rows, err := db.Query("SELECT * FROM books") | |
if err != nil { | |
return nil, err | |
} | |
defer rows.Close() | |
var bks []Book | |
for rows.Next() { | |
var bk Book | |
err := rows.Scan(&bk.Isbn, &bk.Title, &bk.Author, &bk.Price) | |
if err != nil { | |
return nil, err | |
} | |
bks = append(bks, bk) | |
} | |
if err = rows.Err(); err != nil { | |
return nil, err | |
} | |
return bks, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment