mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 09:49:12 +00:00
update d1-blog-server example
This commit is contained in:
parent
bd02c8bd45
commit
e46e29aa7d
@ -3,7 +3,7 @@ dev:
|
|||||||
wrangler dev
|
wrangler dev
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build-tinygo:
|
build:
|
||||||
mkdir -p dist
|
mkdir -p dist
|
||||||
tinygo build -o ./dist/app.wasm -target wasm ./main.go
|
tinygo build -o ./dist/app.wasm -target wasm ./main.go
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mailru/easyjson"
|
"github.com/mailru/easyjson"
|
||||||
|
"github.com/syumai/workers/cloudflare/d1"
|
||||||
_ "github.com/syumai/workers/cloudflare/d1" // register driver
|
_ "github.com/syumai/workers/cloudflare/d1" // register driver
|
||||||
"github.com/syumai/workers/examples/d1-blog-server/app/model"
|
"github.com/syumai/workers/examples/d1-blog-server/app/model"
|
||||||
"github.com/syumai/workers/internal/jsutil"
|
"github.com/syumai/workers/internal/jsutil"
|
||||||
@ -23,10 +24,15 @@ func NewArticleHandler() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *articleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
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 {
|
if err != nil {
|
||||||
h.handleErr(w, http.StatusInternalServerError, fmt.Sprintf("failed to initialize DB: %v", err))
|
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 {
|
switch req.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
h.listArticles(w, req, db)
|
h.listArticles(w, req, db)
|
||||||
@ -62,7 +68,7 @@ func (h *articleHandler) createArticle(w http.ResponseWriter, req *http.Request,
|
|||||||
CreatedAt: uint64(now),
|
CreatedAt: uint64(now),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := db.ExecContext(req.Context(), `
|
_, err := db.Exec(`
|
||||||
INSERT INTO articles (id, title, body, created_at)
|
INSERT INTO articles (id, title, body, created_at)
|
||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
`, article.ID, article.Title, article.Body, article.CreatedAt)
|
`, 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) {
|
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
|
SELECT id, title, body, created_at FROM articles
|
||||||
ORDER BY created_at DESC;
|
ORDER BY created_at DESC;
|
||||||
`)
|
`)
|
||||||
|
@ -4,7 +4,7 @@ go 1.19
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mailru/easyjson v0.7.7
|
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 => ../../
|
replace github.com/syumai/workers => ../../
|
||||||
|
Loading…
x
Reference in New Issue
Block a user