Compile regex and enum constraints at route registration so malformed and unknown IDs share a 404, and named rollback errors isolate one plugin's history. Co-authored-by: Cursor <cursoragent@cursor.com>
142 lines
3.7 KiB
Go
142 lines
3.7 KiB
Go
package greeter
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.golem15.com/golem15/summercms/backpack"
|
|
"git.golem15.com/golem15/summercms/bonfire"
|
|
"git.golem15.com/golem15/summercms/festival"
|
|
"git.golem15.com/golem15/summercms/pact"
|
|
"git.golem15.com/golem15/summercms/party"
|
|
"git.golem15.com/golem15/summercms/surf"
|
|
"git.golem15.com/golem15/summercms/towel"
|
|
)
|
|
|
|
var (
|
|
_ festival.Collectable = (*HelloEvent)(nil)
|
|
_ festival.Handleable = (*HelloEvent)(nil)
|
|
_ pact.HasCommands = (*Plugin)(nil)
|
|
_ pact.HasRoutes = (*Plugin)(nil)
|
|
)
|
|
|
|
// Plugin is the golem15.greeter plugin. It requires golem15.hello.
|
|
type Plugin struct {
|
|
app *backpack.App
|
|
extra string
|
|
}
|
|
|
|
func (p *Plugin) ID() string { return "golem15.greeter" }
|
|
func (p *Plugin) Requires() []string { return []string{"golem15.hello"} }
|
|
|
|
func (p *Plugin) Register(*backpack.App) error { return nil }
|
|
|
|
func (p *Plugin) Boot(app *backpack.App) error {
|
|
p.app = app
|
|
p.extra = ""
|
|
if app != nil && app.HasPlugin("golem15.optional") {
|
|
if msg, ok := app.Lookup[pact.OptionalMessage](); ok {
|
|
p.extra = msg.Message()
|
|
}
|
|
}
|
|
if app != nil && app.Events != nil {
|
|
app.Events.Listen[*HelloEvent]("golem15.greeter", func(ctx context.Context, e *HelloEvent) error {
|
|
if e.data == nil {
|
|
e.data = map[string]any{}
|
|
}
|
|
e.data["source"] = "greeter"
|
|
if actor, ok := towel.Actor(ctx); ok {
|
|
e.data["actor"] = actor
|
|
}
|
|
e.handled = true
|
|
return nil
|
|
})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Plugin) Routes(r pact.Router) error {
|
|
r.Get("/items/{id}", func(w http.ResponseWriter, req *http.Request) {
|
|
id, ok := surf.IntParam(req, "id")
|
|
if !ok || id != 1 {
|
|
http.NotFound(w, req)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprintf(w, `{"id":%d}`, id)
|
|
})
|
|
r.Where("id", `[0-9]+`)
|
|
r.Get("/kinds/{kind}", func(w http.ResponseWriter, req *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprintf(w, `{"kind":%q}`, req.PathValue("kind"))
|
|
})
|
|
r.WhereIn("kind", "widget", "gadget")
|
|
return nil
|
|
}
|
|
|
|
func (p *Plugin) Commands() []bonfire.Command {
|
|
return []bonfire.Command{{
|
|
Name: "greeter:hello",
|
|
Description: "Print the configured application name",
|
|
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
|
|
name := ""
|
|
posts := 0
|
|
debug := false
|
|
if p.app != nil && p.app.Config != nil {
|
|
name = p.app.Config.String("app.name")
|
|
posts = p.app.Config.Int("golem15.hello.posts_per_page")
|
|
debug = p.app.Config.Bool("app.debug")
|
|
}
|
|
events := "ok"
|
|
collected := ""
|
|
handled := false
|
|
if p.app != nil && p.app.Events != nil {
|
|
if err := p.app.Events.Fire(ctx, &HelloEvent{}); err != nil {
|
|
events = "err"
|
|
}
|
|
payload, err := p.app.Events.Collect(ctx, &HelloEvent{})
|
|
if err != nil {
|
|
events = "err"
|
|
}
|
|
if src, ok := payload["source"].(string); ok {
|
|
collected = src
|
|
}
|
|
ok, err := p.app.Events.UntilHandled(ctx, &HelloEvent{})
|
|
if err != nil {
|
|
events = "err"
|
|
}
|
|
handled = ok
|
|
}
|
|
out.Table([]string{"plugin", "status"}, [][]string{{"golem15.greeter", "active"}})
|
|
if _, err := out.Confirm("show greeting?", true); err != nil {
|
|
return err
|
|
}
|
|
out.Printf("name=%s posts_per_page=%d debug=%t extra=%s events=%s collected=%s handled=%t\n", name, posts, debug, p.extra, events, collected, handled)
|
|
return nil
|
|
},
|
|
}}
|
|
}
|
|
|
|
// HelloEvent is a typed greeter event used to demonstrate Fire, Collect
|
|
// and UntilHandled on the app bus.
|
|
type HelloEvent struct {
|
|
data map[string]any
|
|
handled bool
|
|
}
|
|
|
|
func (e *HelloEvent) Collected() map[string]any {
|
|
if e == nil {
|
|
return nil
|
|
}
|
|
return e.data
|
|
}
|
|
|
|
func (e *HelloEvent) IsHandled() bool {
|
|
return e != nil && e.handled
|
|
}
|
|
|
|
func init() {
|
|
party.Register(&Plugin{})
|
|
}
|