This repository was archived by the owner on Sep 26, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhost.go
More file actions
118 lines (107 loc) · 3.17 KB
/
Copy pathhost.go
File metadata and controls
118 lines (107 loc) · 3.17 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: Apache-2.0
package pluginkit
import (
"context"
"errors"
"fmt"
"net/http"
)
// Host starts and stops a fixed set of plugins.
type Host struct {
plugins []Plugin
}
// NewHost returns a [Host] managing plugins. It panics when two
// plugins share an ID.
func NewHost(plugins ...Plugin) *Host {
seen := make(map[string]struct{}, len(plugins))
for _, p := range plugins {
if _, ok := seen[p.ID()]; ok {
panic(fmt.Sprintf("pluginkit: duplicate id %q", p.ID()))
}
seen[p.ID()] = struct{}{}
}
return &Host{plugins: plugins}
}
// Start migrates every [Migrator] plugin, then starts every plugin in registration
// order, stopping the already-started ones in reverse order when a start fails.
func (h *Host) Start(ctx context.Context) error {
for _, p := range h.plugins {
migrator, ok := p.(Migrator)
if !ok {
continue
}
if err := safeCall(ctx, p.ID(), "migrate", migrator.Migrate); err != nil {
return err
}
}
for i, p := range h.plugins {
if err := safeCall(ctx, p.ID(), "start", p.Start); err != nil {
return errors.Join(err, h.stopDownFrom(ctx, i-1))
}
}
return nil
}
// Seed asks every [Seeder] plugin to fill its schema in registration order,
// stopping at the first failure.
func (h *Host) Seed(ctx context.Context) error {
for _, p := range h.plugins {
seeder, ok := p.(Seeder)
if !ok {
continue
}
if err := safeCall(ctx, p.ID(), "seed", seeder.Seed); err != nil {
return err
}
}
return nil
}
// Routes returns the HTTP handler of every [RouteProvider] plugin,
// keyed by plugin ID.
func (h *Host) Routes() map[string]http.Handler {
routes := make(map[string]http.Handler)
for _, p := range h.plugins {
if provider, ok := p.(RouteProvider); ok {
routes[p.ID()] = provider.Routes()
}
}
return routes
}
// PublicPaths returns the session-exempt paths of every
// [PublicPathProvider] plugin, keyed by plugin ID.
func (h *Host) PublicPaths() map[string][]string {
paths := make(map[string][]string)
for _, p := range h.plugins {
if provider, ok := p.(PublicPathProvider); ok {
paths[p.ID()] = provider.PublicPaths()
}
}
return paths
}
// Stop stops every plugin in reverse registration order, continuing
// past failures and returning them joined.
func (h *Host) Stop(ctx context.Context) error {
return h.stopDownFrom(ctx, len(h.plugins)-1)
}
// stopDownFrom stops plugins from index down to zero in reverse order, collecting and joining any errors.
func (h *Host) stopDownFrom(ctx context.Context, index int) error {
var errs []error
for i := index; i >= 0; i-- {
if err := safeCall(ctx, h.plugins[i].ID(), "stop", h.plugins[i].Stop); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// safeCall runs fn, wrapping any returned error and converting any panic into an error tagged with the
// plugin id and operation.
func safeCall(ctx context.Context, id, operation string, fn func(context.Context) error) (err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("pluginkit: %s %s panicked: %v", id, operation, recovered)
}
}()
if err := fn(ctx); err != nil {
return fmt.Errorf("pluginkit: %s %s: %w", id, operation, err)
}
return nil
}