init
Add a package level scope variable
var db *sql.DB
Initialize your database
Note: defer.db.Close()
has been removed
func init() {
var err error
db, err = sql.Open("postgres", "postgres://bond:password@localhost/bookstore?sslmode=disable")
if err != nil {
panic(err)
}
if err = db.Ping(); err != nil {
panic(err)
}
fmt.Println("You connected to your database.")
}
add routes & server
func main() {
http.HandleFunc("/", index)
http.ListenAndServe(":8080", nil)
}
add a index func
func index(w http.ResponseWriter, r *http.Request){
if r.Method != "GET" {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
return
}
rows, err := db.Query("SELECT * FROM books")
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
defer rows.Close()
bks := make([]Book, 0)
for rows.Next() {
bk := Book{}
err := rows.Scan(&bk.isbn, &bk.title, &bk.author, &bk.price) // order matters
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
bks = append(bks, bk)
}
if err = rows.Err(); err != nil {
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)
}
}
run the application and make a request
curl -i localhost:8080/books