Skip to content

Commit

Permalink
fix: use double pointer to implement setDefaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Jan 7, 2025
1 parent 87d57ae commit c26f5f9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion internal/core/plugin_manager/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion internal/server/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,7 +31,7 @@ func TestEndpointParams(t *testing.T) {
}
cancel := appPointer.server(&app.Config{
ServerPort: port,
PluginEndpointEnabled: true,
PluginEndpointEnabled: parser.ToPtr(true),
})
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions internal/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions internal/service/plugin_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions internal/types/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand Down
12 changes: 6 additions & 6 deletions internal/types/app/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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
}
}

0 comments on commit c26f5f9

Please sign in to comment.