-
-
Save hungthai1401/2903333136e33e125a7c48b47612f79d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
. | |
├── books | |
│ ├── handlers.go | |
│ └── models.go | |
├── config | |
│ └── db.go | |
└── main.go |
This file contains hidden or 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 config | |
import ( | |
"database/sql" | |
_ "github.com/lib/pq" | |
) | |
type Env struct { | |
DB *sql.DB | |
} | |
func NewDB(dataSourceName string) (*sql.DB, error) { | |
db, err := sql.Open("postgres", dataSourceName) | |
if err != nil { | |
return nil, err | |
} | |
err = db.Ping() | |
if err != nil { | |
db.Close() | |
return nil, err | |
} | |
return db, nil | |
} |
This file contains hidden or 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 ( | |
"bookstore/config" | |
"fmt" | |
"net/http" | |
) | |
func BooksIndex(env *config.Env) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "GET" { | |
http.Error(w, http.StatusText(405), 405) | |
return | |
} | |
bks, err := AllBooks(env.DB) | |
if 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) | |
} | |
}) | |
} |
This file contains hidden or 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 ( | |
"bookstore/books" | |
"bookstore/config" | |
"log" | |
"net/http" | |
) | |
func main() { | |
db, err := config.NewDB("postgres://user:pass@localhost/bookstore") | |
if err != nil { | |
log.Panic(err) | |
} | |
defer db.Close() | |
env := &config.Env{DB: db} | |
http.Handle("/books", books.BooksIndex(env)) | |
http.ListenAndServe(":3000", nil) | |
} |
This file contains hidden or 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 ( | |
"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() | |
bks := make([]*Book, 0) | |
for rows.Next() { | |
bk := new(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