feat: add resource provider override

This commit is contained in:
M09Ic committed 2026-05-08 10:12:55 -07:00
1 parent d887259fcb
commit b19a8341e9
3 files changed
+57 -1

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
package pkg
import "sync"
// ResourceProvider supplies template/config bytes by logical name.
type ResourceProvider func(string) []byte
var resourceProvider struct {
sync.RWMutex
fn ResourceProvider
}
// SetResourceProvider installs an external template/config provider.
func SetResourceProvider(provider ResourceProvider) {
resourceProvider.Lock()
defer resourceProvider.Unlock()
resourceProvider.fn = provider
}
// ResetResourceProvider removes the external template/config provider.
func ResetResourceProvider() {
SetResourceProvider(nil)
}
// LoadEmbeddedConfig loads the standalone embedded config without consulting
// an installed external provider.
func LoadEmbeddedConfig(typ string) []byte {
return loadEmbeddedConfig(typ)
}
// LoadConfig loads config bytes from the external provider first, then from
// the embedded templates kept for standalone spray compatibility.
func LoadConfig(typ string) []byte {
resourceProvider.RLock()
provider := resourceProvider.fn
resourceProvider.RUnlock()
if provider != nil {
if data := provider(typ); len(data) > 0 {
return data
}
}
return loadEmbeddedConfig(typ)
}
+4 -1
View File
@@ -1,3 +1,6 @@
//go:build !emptytemplates
// +build !emptytemplates
package pkg
import (
@@ -6,7 +9,7 @@ import (
var RandomDir = "/g8kZMwp4oeKsL2in"
func LoadConfig(typ string) []byte {
func loadEmbeddedConfig(typ string) []byte {
if typ == "spray_rule" {
return encode.MustDeflateDeCompress(encode.Base64Decode("hFPNbqs8EN3zFEfJsMQYU5rwsfpa9RXuKrLktKQlTUsUkvvTxX32K48xcQJRJQRzxsdnPMOxOR3f1n/2puv+w2yOtenq1WH1Sal971wI/p56wN/nHvD3sQdkXBABlNlwju6tObRwBWxGp9ACOsX88cdTomQmk3x5nwdLvKo76C0G0l0i5eLOkSJAV7eYSiZZWSy83IhH6RSzcm+rnEKnfrdhAYYUgxRo0/d1hgK0BXV+ADEoB609jScx5ELuFfEWR4Je0AfBcA36ExReXnFwb5+gwaxclqOjmZCRlzkPP0gtikyeS4lgvwLVVxDOKBeZW0wbWMnxJomL+oqP1GQGTQaJJouHdZXkqsiVXXcG25qfJgKe24+PllN2hMYH4b/5y8Gc31VwBBE0KXy+4t4vBVKvKjAIeNP6eEimZ/PYeANte9dxkKsuYxHAGlqdybOo/n0cLmlvB8nn2IM+Qa+BX/4f5VcrBqOsbfHktSS7amLnwBgMOsEo2Hu5VfyGM1knBj15M5egJQdL14vV/Jb7wM+VajVVJwc99P82WJlFm2ZXr827na8zCt8Uevf/fBqClM+0oB3oxcOdLdL6wsJfdBHs+AI1oL2HR5Y8ePgK+mI7OG+JfhQd6BdoP4v+AQAA//8="))
} else if typ == "spray_common" {
+10
View File
@@ -0,0 +1,10 @@
//go:build emptytemplates
// +build emptytemplates
package pkg
var RandomDir = ""
func loadEmbeddedConfig(string) []byte {
return nil
}