2
0
Files
logikonline 015318aa0a
Some checks failed
Build and Release / Build Binaries (arm64, darwin, macos) (push) Has been cancelled
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been cancelled
Build and Release / Build Binary (linux/arm64) (push) Has been cancelled
Build and Release / Lint (push) Has been cancelled
Build and Release / Unit Tests (push) Has been cancelled
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Has been cancelled
Build and Release / Build Binaries (amd64, darwin, macos) (push) Has been cancelled
Build and Release / Create Release (push) Has been cancelled
Build and Release / Integration Tests (PostgreSQL) (push) Has been cancelled
feat(plugins): add PluginRouter interface for route registration
Introduce PluginRouter interface to standardize plugin route registration and replace the previous 'any' type approach. Add WebRouterAdapter to wrap web.Router and handle path prefixes correctly. This provides a cleaner, type-safe API for plugins to register routes without needing to know about the underlying router implementation.
2026-01-21 10:35:56 -05:00

82 lines
2.3 KiB
Go

// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: MIT
package plugins
import (
"net/http"
)
// PluginRouter is the interface plugins use to register routes.
// It abstracts away the underlying router implementation and ensures
// routes are registered with the correct path prefixes.
type PluginRouter interface {
// Get registers a GET route
Get(pattern string, handler http.HandlerFunc)
// Post registers a POST route
Post(pattern string, handler http.HandlerFunc)
// Put registers a PUT route
Put(pattern string, handler http.HandlerFunc)
// Delete registers a DELETE route
Delete(pattern string, handler http.HandlerFunc)
// Group creates a sub-router with a path prefix
Group(pattern string, fn func(PluginRouter))
}
// WebRouterAdapter wraps a web.Router to implement PluginRouter.
// This adapter ensures routes are registered with the correct path prefixes.
type WebRouterAdapter struct {
router WebRouter
pathPrefix string
}
// WebRouter is the interface that web.Router implements
type WebRouter interface {
Get(pattern string, h ...any)
Post(pattern string, h ...any)
Put(pattern string, h ...any)
Delete(pattern string, h ...any)
Group(pattern string, fn func(), middlewares ...any)
}
// NewWebRouterAdapter creates a new adapter for the given web.Router
func NewWebRouterAdapter(router WebRouter, pathPrefix string) *WebRouterAdapter {
return &WebRouterAdapter{
router: router,
pathPrefix: pathPrefix,
}
}
// Get implements PluginRouter.Get
func (a *WebRouterAdapter) Get(pattern string, handler http.HandlerFunc) {
a.router.Get(a.pathPrefix+pattern, handler)
}
// Post implements PluginRouter.Post
func (a *WebRouterAdapter) Post(pattern string, handler http.HandlerFunc) {
a.router.Post(a.pathPrefix+pattern, handler)
}
// Put implements PluginRouter.Put
func (a *WebRouterAdapter) Put(pattern string, handler http.HandlerFunc) {
a.router.Put(a.pathPrefix+pattern, handler)
}
// Delete implements PluginRouter.Delete
func (a *WebRouterAdapter) Delete(pattern string, handler http.HandlerFunc) {
a.router.Delete(a.pathPrefix+pattern, handler)
}
// Group implements PluginRouter.Group
func (a *WebRouterAdapter) Group(pattern string, fn func(PluginRouter)) {
subAdapter := &WebRouterAdapter{
router: a.router,
pathPrefix: a.pathPrefix + pattern,
}
fn(subAdapter)
}