-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
148 lines (112 loc) · 4.4 KB
/
plugin.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package server
import (
"context"
"io"
"io/fs"
"net/http"
"time"
// Packages
pg "github.com/djthorpe/go-pg"
authschema "github.com/mutablelogic/go-server/pkg/auth/schema"
pgschema "github.com/mutablelogic/go-server/pkg/pgqueue/schema"
)
///////////////////////////////////////////////////////////////////////////////
// INTERFACES
// Plugin represents a service
type Plugin interface {
// Return the unique name for the plugin
Name() string
// Return a description of the plugin
Description() string
// Create a task from a plugin
New(context.Context) (Task, error)
}
// Task represents a service task
type Task interface {
// Run a task
Run(context.Context) error
}
// Provider represents a service provider
type Provider interface {
Task
// Write out the plugin configuration to a writer
WriteConfig(io.Writer) error
// Load a plugin by name and label, and provide a resolver function
Load(string, string, PluginResolverFunc) error
// Return a task given a plugin label
Task(context.Context, string) Task
}
// Define a function for resolving plugin configuration
type PluginResolverFunc func(context.Context, string, Plugin) error
///////////////////////////////////////////////////////////////////////////////
// HTTP ROUTER
type HTTPRouter interface {
// Return the CORS origin
Origin() string
// Register a function to handle a URL path
HandleFunc(context.Context, string, http.HandlerFunc)
// Register serving of static files from a filesystem
HandleFS(context.Context, string, fs.FS)
}
///////////////////////////////////////////////////////////////////////////////
// HTTP MIDDLEWARE
type HTTPMiddleware interface {
// Return a http handler with middleware as the parent handler
HandleFunc(http.HandlerFunc) http.HandlerFunc
}
///////////////////////////////////////////////////////////////////////////////
// LOGGER
// Logger defines methods for logging messages and structured data.
// It can also act as HTTP middleware for request logging.
type Logger interface {
HTTPMiddleware
// Debug logs a debugging message.
Debug(context.Context, ...any)
// Debugf logs a formatted debugging message.
Debugf(context.Context, string, ...any)
// Print logs an informational message.
Print(context.Context, ...any)
// Printf logs a formatted informational message.
Printf(context.Context, string, ...any)
// With returns a new Logger that includes the given key-value pairs
// in its structured log output.
With(...any) Logger
}
///////////////////////////////////////////////////////////////////////////////
// PGPOOL
// PG provides access to a PostgreSQL connection pool.
type PG interface {
// Conn returns the underlying connection pool object.
Conn() pg.PoolConn
}
///////////////////////////////////////////////////////////////////////////////
// PGQUEUE
// PGCallback defines the function signature for handling tasks dequeued
// from a PostgreSQL-backed queue.
type PGCallback func(context.Context, any) error
// PGQueue defines methods for interacting with a PostgreSQL-backed task queue.
type PGQueue interface {
// RegisterTicker registers a periodic task (ticker) with a callback function.
// It returns the metadata of the registered ticker.
RegisterTicker(context.Context, pgschema.TickerMeta, PGCallback) (*pgschema.Ticker, error)
// RegisterQueue registers a task queue with a callback function.
// It returns the metadata of the registered queue.
RegisterQueue(context.Context, pgschema.QueueMeta, PGCallback) (*pgschema.Queue, error)
// CreateTask adds a new task to a specified queue with a payload and optional delay.
// It returns the metadata of the created task.
CreateTask(context.Context, string, any, time.Duration) (*pgschema.Task, error)
// UnmarshalPayload unmarshals a payload into a destination object.
UnmarshalPayload(dest any, payload any) error
}
///////////////////////////////////////////////////////////////////////////////
// AUTHENTICATION AND AUTHORIZATION
// Auth defines methods for authenticating users based on HTTP requests
// and authorizing them based on required scopes.
type Auth interface {
// Authenticate attempts to identify and return the user associated with
// an incoming HTTP request, typically by inspecting headers or tokens.
Authenticate(*http.Request) (*authschema.User, error)
// Authorize checks if a given user has the necessary permissions (scopes)
// to perform an action.
Authorize(context.Context, *authschema.User, ...string) error
}