update d1-blog-server example

This commit is contained in:
syumai 2023-02-26 12:26:48 +09:00
parent bd02c8bd45
commit e46e29aa7d
3 changed files with 11 additions and 5 deletions

View File

@ -3,7 +3,7 @@ dev:
wrangler dev
.PHONY: build
build-tinygo:
build:
mkdir -p dist
tinygo build -o ./dist/app.wasm -target wasm ./main.go

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/mailru/easyjson"
"github.com/syumai/workers/cloudflare/d1"
_ "github.com/syumai/workers/cloudflare/d1" // register driver
"github.com/syumai/workers/examples/d1-blog-server/app/model"
"github.com/syumai/workers/internal/jsutil"
@ -23,10 +24,15 @@ func NewArticleHandler() http.Handler {
}
func (h *articleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
db, err := sql.Open("d1", "BlogDB")
// initialize DB.
// D1 connector requires request's context to initialize DB.
c, err := d1.OpenConnector(req.Context(), "BlogDB")
if err != nil {
h.handleErr(w, http.StatusInternalServerError, fmt.Sprintf("failed to initialize DB: %v", err))
}
// use sql.OpenDB instead of sql.Open.
db := sql.OpenDB(c)
switch req.Method {
case http.MethodGet:
h.listArticles(w, req, db)
@ -62,7 +68,7 @@ func (h *articleHandler) createArticle(w http.ResponseWriter, req *http.Request,
CreatedAt: uint64(now),
}
_, err := db.ExecContext(req.Context(), `
_, err := db.Exec(`
INSERT INTO articles (id, title, body, created_at)
VALUES (?, ?, ?, ?)
`, article.ID, article.Title, article.Body, article.CreatedAt)
@ -84,7 +90,7 @@ VALUES (?, ?, ?, ?)
}
func (h *articleHandler) listArticles(w http.ResponseWriter, req *http.Request, db *sql.DB) {
rows, err := db.QueryContext(req.Context(), `
rows, err := db.Query(`
SELECT id, title, body, created_at FROM articles
ORDER BY created_at DESC;
`)

View File

@ -4,7 +4,7 @@ go 1.19
require (
github.com/mailru/easyjson v0.7.7
github.com/syumai/workers v0.0.0-00010101000000-000000000000
github.com/syumai/workers v0.9.0
)
replace github.com/syumai/workers => ../../