在Golang iris web 框架里使用数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
func main() {
dbinfo := fmt.Sprintf("port=32768 user=%s password=%s dbname=%s sslmode=disable", "postgres", "pass123", "postgres")
db, err := sql.Open("postgres", dbinfo)
if err != nil {
fmt.Printf("err: %+v ", err)
}
defer db.Close()
iris.Get("/people", GetAll(db))
iris.Get("/people/:id", Get(db))
iris.Post("/people/:id", Post(db))
iris.Delete("/people/:id", Delete(db))
iris.Listen(":8000")
}
func GetAll(db *sql.DB) iris.HandlerFunc {
return func(ctx *iris.Context) {
p, err := posts.GetAll(db)
if err != nil {
fmt.Printf("%v", err)
ctx.Error("Failed to load people list", 503)
}
ctx.JSON(200, p)
}
}
func Get(db *sql.DB) iris.HandlerFunc {
return func(ctx *iris.Context) {
ID, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
handleError(ctx, "Failed to load people list", err)
}
p, err := posts.Get(db, ID)
if err != nil {
handleError(ctx, "Failed to load people list", err)
}
ctx.JSON(200, p)
}
}
func Post(db *sql.DB) iris.HandlerFunc {
return func(ctx *iris.Context) {
name := ctx.Param("name")
hobby := ctx.Param("hobby")
err := posts.Post(db, name, hobby)
if err != nil {
handleError(ctx, "Failed to save person", err)
return
}
ctx.Write("ok")
}
}
func Delete(db *sql.DB) iris.HandlerFunc {
return func(ctx *iris.Context) {
ID, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
handleError(ctx, "Failed to load people list", err)
}
err = posts.Delete(db, ID)
if err != nil {
handleError(ctx, "Failed to load people list", err)
}
ctx.Write("ok")
}
}
func handleError(ctx *iris.Context, message string, err error) {
fmt.Printf("%v", err)
ctx.Error(message, 503)
}
// posts/posts.go
func GetAll(db *sql.DB) ([]Person, error) {
rows, err := db.Query("SELECT * FROM people")
if err != nil {
return nil, fmt.Errorf("failed to get people list: %v", err)
}
var people []Person
for rows.Next() {
p, err := readPerson(rows)
if err != nil {
return nil, fmt.Errorf("failed to get people list: %v", err)
}
people = append(people, p)
}
return people, nil
}
func Get(db *sql.DB, ID int) (Person, error) {
rows, err := db.Query("SELECT * FROM people WHERE id = $1", ID)
rows.Next()
if err != nil {
return Person{}, fmt.Errorf("failed to get people list: %v", err)
}
return readPerson(rows)
}
func Post(db *sql.DB, name, hobby string) error {
_, err := db.Query("INSERT INTO people (name, hobby) VALUES ($1, $2)", name, hobby)
return err
}
func Delete(db *sql.DB, ID int) error {
_, err := db.Query("DELETE * FROM people WHERE id = $1", ID)
return err
}
func readPerson(rows *sql.Rows) (Person, error) {
var id int
var name string
var hobby string
err := rows.Scan(&id, &name, &hobby)
if err != nil {
return Person{}, fmt.Errorf("failed to get people list: %v", err)
}
return Person{id, name, hobby}, nil
}
type Person struct {
ID int
Name string
Hobby string
}
本文网址: https://golangnote.com/topic/151.html 转摘请注明来源