-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexample_test.go
More file actions
103 lines (86 loc) · 2.8 KB
/
example_test.go
File metadata and controls
103 lines (86 loc) · 2.8 KB
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
package riverui_test
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/riverqueue/river"
"github.com/riverqueue/river/riverdriver/riverpgxv5"
"github.com/riverqueue/river/rivershared/util/slogutil"
"riverqueue.com/riverui"
)
// ExampleNewHandler demonstrates how to create a River UI handler,
// embed it in an HTTP server, and make requests to its API endpoints.
func ExampleNewHandler() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a PostgreSQL connection pool. In a real application, you'd use your
// own connection string or pool config.
connConfig, err := pgxpool.ParseConfig(os.Getenv("TEST_DATABASE_URL"))
if err != nil {
panic(err)
}
dbPool, err := pgxpool.NewWithConfig(ctx, connConfig)
if err != nil {
panic(err)
}
defer dbPool.Close()
// Create a River client with a message-only logger for reproducible output.
// You can use any slog.Handler implementation in your application.
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelWarn, ReplaceAttr: slogutil.NoLevelTime}))
// Create a River client. We don't need to start the client since we're only
// using it to demonstrate the UI.
client, err := river.NewClient(riverpgxv5.New(dbPool), &river.Config{
Logger: logger,
})
if err != nil {
panic(err)
}
// Create the River UI handler. This handler implements http.Handler and can be
// mounted in an HTTP mux
handler, err := riverui.NewHandler(&riverui.HandlerOpts{
DevMode: true, // Use the live filesystem—don't use this outside tests
Endpoints: riverui.NewEndpoints(client, nil),
Logger: logger,
Prefix: "/riverui", // Mount the UI under /riverui path
})
if err != nil {
panic(err)
}
// Start the server to initialize background processes
// This does not start an HTTP server
if err := handler.Start(ctx); err != nil {
panic(err)
}
// Create an HTTP mux and mount the River UI:
mux := http.NewServeMux()
mux.Handle("/riverui/", handler)
// For this example, we use a test server to demonstrate API calls. In a
// production environment, you would use http.ListenAndServe instead.
testServer := httptest.NewServer(mux)
defer testServer.Close()
// Make a request to the jobs endpoint to demonstrate API usage:
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testServer.URL+"/riverui/api/jobs?state=available", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("Status: %s\n", resp.Status)
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("Response: %s\n", strings.TrimSpace(string(body)))
// Output:
// Status: 200 OK
// Response: {"data":[]}
}