docs(pact,party,surf): compile README Go examples via src= (quick-261118-qsd)
This commit is contained in:
@@ -46,25 +46,11 @@ Use cases:
|
||||
|
||||
## Usage
|
||||
|
||||
A plugin declares its capabilities by implementing the interfaces and asserting them at compile time:
|
||||
A plugin declares its capabilities by implementing the interfaces, and asserts them at compile time with lines such as `var _ pact.HasRoutes = (*BlogPlugin)(nil)`:
|
||||
|
||||
```go
|
||||
package blog
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
)
|
||||
|
||||
var (
|
||||
_ pact.HasRoutes = (*Plugin)(nil)
|
||||
_ pact.HasPermissions = (*Plugin)(nil)
|
||||
)
|
||||
|
||||
type Plugin struct{}
|
||||
|
||||
func (p *Plugin) Routes(r pact.Router) error {
|
||||
```go src=modules/pact/example_test.go#BlogPlugin.Routes
|
||||
// Routes declares the plugin's JSON API behind the auth middleware.
|
||||
func (p *BlogPlugin) Routes(r pact.Router) error {
|
||||
r.Group("/api/blog", []string{"auth"}, func(r pact.Router) {
|
||||
r.Get("/posts", listPosts)
|
||||
r.Get("/posts/{id}", showPost)
|
||||
@@ -72,26 +58,26 @@ func (p *Plugin) Routes(r pact.Router) error {
|
||||
})
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
func (p *Plugin) Permissions() []pact.Permission {
|
||||
```go src=modules/pact/example_test.go#BlogPlugin.Permissions
|
||||
// Permissions declares the backend permission that guards the post screens.
|
||||
func (p *BlogPlugin) Permissions() []pact.Permission {
|
||||
return []pact.Permission{
|
||||
{Code: "acme.blog.access_posts", Tab: "Blog", Label: "Manage posts"},
|
||||
}
|
||||
}
|
||||
|
||||
func listPosts(w http.ResponseWriter, r *http.Request) {}
|
||||
func showPost(w http.ResponseWriter, r *http.Request) {}
|
||||
```
|
||||
|
||||
A plugin schedules one of its registered commands by implementing `pact.HasSchedule`:
|
||||
A plugin schedules its registered console commands by implementing `pact.HasSchedule`:
|
||||
|
||||
```go
|
||||
var _ pact.HasSchedule = (*Plugin)(nil)
|
||||
|
||||
func (p *Plugin) Schedule() []pact.ScheduledCommand {
|
||||
```go src=modules/pact/example_test.go#BlogPlugin.Schedule
|
||||
// Schedule runs three of the plugin's registered console commands.
|
||||
func (p *BlogPlugin) Schedule() []pact.ScheduledCommand {
|
||||
return []pact.ScheduledCommand{
|
||||
{Command: "blog:prune-drafts", Cadence: pact.Daily()},
|
||||
{Command: "blog:ping", Args: []string{"--quiet"}, Cadence: pact.Every(15 * time.Minute)},
|
||||
{Command: "blog:send-digest", Cadence: pact.DailyAt(7, 30)},
|
||||
{Command: "blog:sync-feed", Args: []string{"--quiet"}, Cadence: pact.Every(15 * time.Minute)},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2,15 +2,44 @@ package pact_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
)
|
||||
|
||||
// BlogPlugin is the acme.blog plugin; only its schedule is shown here.
|
||||
// BlogPlugin is the acme.blog plugin: routes, permissions and a schedule.
|
||||
type BlogPlugin struct{}
|
||||
|
||||
var _ pact.HasSchedule = (*BlogPlugin)(nil)
|
||||
var (
|
||||
_ pact.HasRoutes = (*BlogPlugin)(nil)
|
||||
_ pact.HasPermissions = (*BlogPlugin)(nil)
|
||||
_ pact.HasSchedule = (*BlogPlugin)(nil)
|
||||
)
|
||||
|
||||
// Routes declares the plugin's JSON API behind the auth middleware.
|
||||
func (p *BlogPlugin) Routes(r pact.Router) error {
|
||||
r.Group("/api/blog", []string{"auth"}, func(r pact.Router) {
|
||||
r.Get("/posts", listPosts)
|
||||
r.Get("/posts/{id}", showPost)
|
||||
r.Where("id", "[0-9]+")
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Permissions declares the backend permission that guards the post screens.
|
||||
func (p *BlogPlugin) Permissions() []pact.Permission {
|
||||
return []pact.Permission{
|
||||
{Code: "acme.blog.access_posts", Tab: "Blog", Label: "Manage posts"},
|
||||
}
|
||||
}
|
||||
|
||||
func listPosts(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "posts") }
|
||||
func showPost(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "post "+r.PathValue("id")) }
|
||||
|
||||
// Schedule runs three of the plugin's registered console commands.
|
||||
func (p *BlogPlugin) Schedule() []pact.ScheduledCommand {
|
||||
@@ -35,3 +64,63 @@ func ExampleHasSchedule() {
|
||||
// blog:send-digest []: daily at 07:30
|
||||
// blog:sync-feed [--quiet]: every 15m0s
|
||||
}
|
||||
|
||||
// routeRecorder is a pact.Router that records each route and constraint and
|
||||
// keeps the handlers, standing in for surf's router.
|
||||
type routeRecorder struct {
|
||||
prefix string
|
||||
middleware []string
|
||||
lines *[]string
|
||||
handlers map[string]http.HandlerFunc
|
||||
}
|
||||
|
||||
func (r routeRecorder) group(prefix string, mw []string, fn func(pact.Router)) {
|
||||
fn(routeRecorder{r.prefix + prefix, append(slices.Clone(r.middleware), mw...), r.lines, r.handlers})
|
||||
}
|
||||
func (r routeRecorder) add(method, path string, h http.HandlerFunc, mw []string) {
|
||||
route := method + " " + r.prefix + path
|
||||
*r.lines = append(*r.lines, route+" "+strings.Join(append(slices.Clone(r.middleware), mw...), ","))
|
||||
r.handlers[route] = h
|
||||
}
|
||||
func (r routeRecorder) Group(p string, mw []string, fn func(pact.Router)) { r.group(p, mw, fn) }
|
||||
func (r routeRecorder) GroupRaw(p string, mw []string, fn func(pact.Router)) { r.group(p, mw, fn) }
|
||||
func (r routeRecorder) Get(p string, h http.HandlerFunc, mw ...string) { r.add("GET", p, h, mw) }
|
||||
func (r routeRecorder) Post(p string, h http.HandlerFunc, mw ...string) { r.add("POST", p, h, mw) }
|
||||
func (r routeRecorder) Put(p string, h http.HandlerFunc, mw ...string) { r.add("PUT", p, h, mw) }
|
||||
func (r routeRecorder) Patch(p string, h http.HandlerFunc, mw ...string) { r.add("PATCH", p, h, mw) }
|
||||
func (r routeRecorder) Delete(p string, h http.HandlerFunc, mw ...string) { r.add("DELETE", p, h, mw) }
|
||||
func (r routeRecorder) Where(param, pattern string) {
|
||||
*r.lines = append(*r.lines, "where "+param+" "+pattern)
|
||||
}
|
||||
func (r routeRecorder) WhereIn(param string, values ...string) {
|
||||
*r.lines = append(*r.lines, "where "+param+" in "+strings.Join(values, ","))
|
||||
}
|
||||
|
||||
// TestDocsDeclarations checks the routes and permissions the README shows
|
||||
// and serves both handlers.
|
||||
func TestDocsDeclarations(t *testing.T) {
|
||||
p := &BlogPlugin{}
|
||||
var lines []string
|
||||
rec := routeRecorder{lines: &lines, handlers: map[string]http.HandlerFunc{}}
|
||||
if err := p.Routes(rec); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"GET /api/blog/posts auth", "GET /api/blog/posts/{id} auth", "where id [0-9]+"}
|
||||
if !slices.Equal(lines, want) {
|
||||
t.Fatalf("routes = %q, want %q", lines, want)
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
for route, h := range rec.handlers {
|
||||
mux.HandleFunc(route, h)
|
||||
}
|
||||
for path, body := range map[string]string{"/api/blog/posts": "posts", "/api/blog/posts/7": "post 7"} {
|
||||
w := httptest.NewRecorder()
|
||||
mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
|
||||
if w.Body.String() != body {
|
||||
t.Fatalf("GET %s = %q, want %q", path, w.Body, body)
|
||||
}
|
||||
}
|
||||
if perms := p.Permissions(); len(perms) != 1 || perms[0].Code != "acme.blog.access_posts" {
|
||||
t.Fatalf("Permissions() = %+v", perms)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user