// 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) }