-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
60 lines (45 loc) · 1.17 KB
/
main.go
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
package main
import (
"app/benchmarks"
"log"
"net/http"
"time"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/jsvm"
)
func customMiddleware(e *core.RequestEvent) error {
e.Set("total", 20)
return e.Next()
}
var dbConnect core.DBConnectFunc
func main() {
app := pocketbase.NewWithConfig(pocketbase.Config{
DefaultQueryTimeout: 120 * time.Second,
DBConnect: dbConnect,
})
jsvm.MustRegister(app, jsvm.Config{
HooksPoolSize: 50,
})
benchmarks.MustRegister(app)
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
se.Router.GET("/go", func(e *core.RequestEvent) error {
total, _ := e.Get("total").(int)
records, err := e.App.FindRecordsByFilter("posts10k", "title != ''", "-created", total, 0)
if err != nil {
return err
}
return e.JSON(http.StatusOK, records)
}).BindFunc(customMiddleware)
return se.Next()
})
app.OnRecordUpdateRequest("go").BindFunc(func(e *core.RecordRequestEvent) error {
if e.Record.GetString("title") != "" {
e.Record.Set("title", "go_update")
}
return e.Next()
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}