Selaa lähdekoodia

fix: use double pointer to implement setDefaultValue

Yeuoly 6 kuukautta sitten
vanhempi
commit
c26f5f9359

+ 1 - 1
internal/core/plugin_manager/watcher.go

@@ -31,7 +31,7 @@ func (p *PluginManager) initRemotePluginServer(config *app.Config) {
 
 func (p *PluginManager) startRemoteWatcher(config *app.Config) {
 	// launch TCP debugging server if enabled
-	if config.PluginRemoteInstallingEnabled {
+	if config.PluginRemoteInstallingEnabled != nil && *config.PluginRemoteInstallingEnabled {
 		p.initRemotePluginServer(config)
 		go func() {
 			err := p.remotePluginServer.Launch()

+ 2 - 1
internal/server/endpoint_test.go

@@ -8,6 +8,7 @@ import (
 	"github.com/gin-gonic/gin"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/network"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
 )
 
 func TestEndpointParams(t *testing.T) {
@@ -30,7 +31,7 @@ func TestEndpointParams(t *testing.T) {
 	}
 	cancel := appPointer.server(&app.Config{
 		ServerPort:            port,
-		PluginEndpointEnabled: true,
+		PluginEndpointEnabled: parser.ToPtr(true),
 	})
 	defer cancel()
 

+ 2 - 2
internal/server/http_server.go

@@ -98,13 +98,13 @@ func (app *App) pluginDispatchGroup(group *gin.RouterGroup, config *app.Config)
 }
 
 func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config) {
-	if config.PluginRemoteInstallingEnabled {
+	if config.PluginRemoteInstallingEnabled != nil && *config.PluginRemoteInstallingEnabled {
 		group.POST("/key", CheckingKey(config.ServerKey), controllers.GetRemoteDebuggingKey)
 	}
 }
 
 func (app *App) endpointGroup(group *gin.RouterGroup, config *app.Config) {
-	if config.PluginEndpointEnabled {
+	if config.PluginEndpointEnabled != nil && *config.PluginEndpointEnabled {
 		group.HEAD("/:hook_id/*path", app.Endpoint())
 		group.POST("/:hook_id/*path", app.Endpoint())
 		group.GET("/:hook_id/*path", app.Endpoint())

+ 2 - 2
internal/service/plugin_decoder.go

@@ -50,7 +50,7 @@ func UploadPluginPkg(
 		return exception.BadRequestError(errors.Join(err, errors.New("failed to save package"))).ToResponse()
 	}
 
-	if config.ForceVerifyingSignature || verify_signature {
+	if config.ForceVerifyingSignature != nil && *config.ForceVerifyingSignature || verify_signature {
 		if !declaration.Verified {
 			return exception.BadRequestError(errors.Join(err, errors.New(
 				"plugin verification has been enabled, and the plugin you want to install has a bad signature",
@@ -138,7 +138,7 @@ func UploadPluginBundle(
 						return exception.InternalServerError(errors.Join(errors.New("failed to save package"), err)).ToResponse()
 					}
 
-					if config.ForceVerifyingSignature || verify_signature {
+					if config.ForceVerifyingSignature != nil && *config.ForceVerifyingSignature || verify_signature {
 						if !declaration.Verified {
 							return exception.BadRequestError(errors.Join(errors.New(
 								"plugin verification has been enabled, and the plugin you want to install has a bad signature",

+ 4 - 4
internal/types/app/config.go

@@ -26,12 +26,12 @@ type Config struct {
 	// plugin remote installing
 	PluginRemoteInstallingHost                string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
 	PluginRemoteInstallingPort                uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`
-	PluginRemoteInstallingEnabled             bool   `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
+	PluginRemoteInstallingEnabled             *bool  `envconfig:"PLUGIN_REMOTE_INSTALLING_ENABLED"`
 	PluginRemoteInstallingMaxConn             int    `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_CONN"`
 	PluginRemoteInstallingMaxSingleTenantConn int    `envconfig:"PLUGIN_REMOTE_INSTALLING_MAX_SINGLE_TENANT_CONN"`
 	PluginRemoteInstallServerEventLoopNums    int    `envconfig:"PLUGIN_REMOTE_INSTALL_SERVER_EVENT_LOOP_NUMS"`
 
-	PluginEndpointEnabled bool `envconfig:"PLUGIN_ENDPOINT_ENABLED"`
+	PluginEndpointEnabled *bool `envconfig:"PLUGIN_ENDPOINT_ENABLED"`
 
 	PluginWorkingPath      string `envconfig:"PLUGIN_WORKING_PATH"` // where the plugin finally running
 	PluginMediaCacheSize   uint16 `envconfig:"PLUGIN_MEDIA_CACHE_SIZE"`
@@ -65,7 +65,7 @@ type Config struct {
 	PersistenceStorageMaxSize int64  `envconfig:"PERSISTENCE_STORAGE_MAX_SIZE"`
 
 	// force verifying signature for all plugins, not allowing install plugin not signed
-	ForceVerifyingSignature bool `envconfig:"FORCE_VERIFYING_SIGNATURE"`
+	ForceVerifyingSignature *bool `envconfig:"FORCE_VERIFYING_SIGNATURE"`
 
 	// lifetime state management
 	LifetimeCollectionHeartbeatInterval int `envconfig:"LIFETIME_COLLECTION_HEARTBEAT_INTERVAL"  validate:"required"`
@@ -102,7 +102,7 @@ func (c *Config) Validate() error {
 		return err
 	}
 
-	if c.PluginRemoteInstallingEnabled {
+	if c.PluginRemoteInstallingEnabled != nil && *c.PluginRemoteInstallingEnabled {
 		if c.PluginRemoteInstallingHost == "" {
 			return fmt.Errorf("plugin remote installing host is empty")
 		}

+ 6 - 6
internal/types/app/default.go

@@ -18,8 +18,8 @@ func (config *Config) SetDefault() {
 	setDefaultString(&config.PluginStorageType, "local")
 	setDefaultInt(&config.PluginMediaCacheSize, 1024)
 	setDefaultInt(&config.PluginRemoteInstallingMaxSingleTenantConn, 5)
-	setDefaultBool(&config.PluginRemoteInstallingEnabled, true)
-	setDefaultBool(&config.PluginEndpointEnabled, true)
+	setDefaultBoolPtr(&config.PluginRemoteInstallingEnabled, true)
+	setDefaultBoolPtr(&config.PluginEndpointEnabled, true)
 	setDefaultString(&config.DBSslMode, "disable")
 	setDefaultString(&config.PluginStorageLocalRoot, "storage")
 	setDefaultString(&config.PluginInstalledPath, "plugin")
@@ -28,7 +28,7 @@ func (config *Config) SetDefault() {
 	setDefaultInt(&config.PersistenceStorageMaxSize, 100*1024*1024)
 	setDefaultString(&config.PluginPackageCachePath, "plugin_packages")
 	setDefaultString(&config.PythonInterpreterPath, "/usr/bin/python3")
-	setDefaultBool(&config.ForceVerifyingSignature, true)
+	setDefaultBoolPtr(&config.ForceVerifyingSignature, true)
 }
 
 func setDefaultInt[T constraints.Integer](value *T, defaultValue T) {
@@ -43,8 +43,8 @@ func setDefaultString(value *string, defaultValue string) {
 	}
 }
 
-func setDefaultBool(value *bool, defaultValue bool) {
-	if !*value {
-		*value = defaultValue
+func setDefaultBoolPtr(value **bool, defaultValue bool) {
+	if *value == nil {
+		*value = &defaultValue
 	}
 }