Browse Source

feat: support inner key

Yeuoly 10 months ago
parent
commit
755f3f1bb5

+ 6 - 6
.env.example

@@ -1,10 +1,10 @@
-# A secretkey that is used for securely communicating with DIFY API. 
-# You can generate a strong key using `openssl rand -base64 42`.
-PLUGIN_INNER_API_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi
-PLUGIN_INNER_API_URL=http://127.0.0.1:5001
-
 SERVER_PORT=5002
-GIN_MODE=debug
+SERVER_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1
+GIN_MODE=release
+PLATFORM=aws_lambda
+
+DIFY_INNER_API_KEY=QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1
+DIFY_INNER_API_URL=http://127.0.0.1:5001
 
 PLUGIN_REMOTE_INSTALLING_HOST=127.0.0.1
 PLUGIN_REMOTE_INSTALLING_PORT=5003

+ 2 - 2
internal/core/dify_invocation/real/http_client.go

@@ -27,9 +27,9 @@ func InitDifyInvocationDaemon(base string, calling_key string) (dify_invocation.
 		},
 	}
 
-	invocation.baseurl = baseurl
+	invocation.dify_inner_api_baseurl = baseurl
 	invocation.client = client
-	invocation.PLUGIN_INNER_API_KEY = calling_key
+	invocation.dify_inner_api_key = calling_key
 
 	return invocation, nil
 }

+ 2 - 2
internal/core/dify_invocation/real/http_request.go

@@ -11,7 +11,7 @@ import (
 func Request[T any](i *RealBackwardsInvocation, method string, path string, options ...http_requests.HttpOptions) (*T, error) {
 	options = append(options,
 		http_requests.HttpHeader(map[string]string{
-			"X-Inner-Api-Key": i.PLUGIN_INNER_API_KEY,
+			"X-Inner-Api-Key": i.dify_inner_api_key,
 		}),
 		http_requests.HttpWriteTimeout(5000),
 		http_requests.HttpReadTimeout(240000),
@@ -23,7 +23,7 @@ func Request[T any](i *RealBackwardsInvocation, method string, path string, opti
 func StreamResponse[T any](i *RealBackwardsInvocation, method string, path string, options ...http_requests.HttpOptions) (*stream.Stream[T], error) {
 	options = append(
 		options, http_requests.HttpHeader(map[string]string{
-			"X-Inner-Api-Key": i.PLUGIN_INNER_API_KEY,
+			"X-Inner-Api-Key": i.dify_inner_api_key,
 		}),
 		http_requests.HttpWriteTimeout(5000),
 		http_requests.HttpReadTimeout(240000),

+ 1 - 1
internal/core/dify_invocation/real/path.go

@@ -2,5 +2,5 @@ package real
 
 func (r *RealBackwardsInvocation) difyPath(path ...string) string {
 	path = append([]string{"inner", "api"}, path...)
-	return r.baseurl.JoinPath(path...).String()
+	return r.dify_inner_api_baseurl.JoinPath(path...).String()
 }

+ 3 - 3
internal/core/dify_invocation/real/types.go

@@ -6,7 +6,7 @@ import (
 )
 
 type RealBackwardsInvocation struct {
-	PLUGIN_INNER_API_KEY string
-	baseurl              *url.URL
-	client               *http.Client
+	dify_inner_api_key     string
+	dify_inner_api_baseurl *url.URL
+	client                 *http.Client
 }

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

@@ -124,7 +124,7 @@ func (p *PluginManager) Init(configuration *app.Config) {
 	}
 
 	invocation, err := real.InitDifyInvocationDaemon(
-		configuration.PluginInnerApiURL, configuration.PluginInnerApiKey,
+		configuration.DifyInnerApiURL, configuration.DifyInnerApiKey,
 	)
 	if err != nil {
 		log.Panic("init dify invocation daemon failed: %s", err.Error())

+ 3 - 3
internal/server/http_server.go

@@ -44,7 +44,7 @@ func (app *App) server(config *app.Config) func() {
 }
 
 func (app *App) pluginInvokeGroup(group *gin.RouterGroup, config *app.Config) {
-	group.Use(CheckingKey(config.PluginInnerApiKey))
+	group.Use(CheckingKey(config.PluginServerApiKey))
 	group.Use(app.RedirectPluginInvoke())
 	group.Use(app.InitClusterID())
 
@@ -62,7 +62,7 @@ func (app *App) pluginInvokeGroup(group *gin.RouterGroup, config *app.Config) {
 
 func (app *App) remoteDebuggingGroup(group *gin.RouterGroup, config *app.Config) {
 	if config.PluginRemoteInstallingEnabled {
-		group.POST("/key", CheckingKey(config.PluginInnerApiKey), controllers.GetRemoteDebuggingKey)
+		group.POST("/key", CheckingKey(config.PluginServerApiKey), controllers.GetRemoteDebuggingKey)
 	}
 }
 
@@ -98,7 +98,7 @@ func (app *App) endpointManagementGroup(group *gin.RouterGroup) {
 }
 
 func (app *App) pluginGroup(group *gin.RouterGroup, config *app.Config) {
-	group.Use(CheckingKey(config.PluginInnerApiKey))
+	group.Use(CheckingKey(config.PluginServerApiKey))
 
 	group.GET("/asset/:id", controllers.GetAsset)
 	group.POST("/:tenant_id/install/pkg", controllers.InstallPluginFromPkg(config))

+ 5 - 2
internal/types/app/config.go

@@ -8,9 +8,12 @@ import (
 
 type Config struct {
 	ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
+	ServerKey  string `envconfig:"SERVER_KEY" validate:"required"`
 
-	PluginInnerApiKey string `envconfig:"PLUGIN_INNER_API_KEY" validate:"required"`
-	PluginInnerApiURL string `envconfig:"PLUGIN_INNER_API_URL" validate:"required"`
+	PluginServerApiKey string `envconfig:"PLUGIN_SERVER_API_KEY" validate:"required"`
+
+	DifyInnerApiURL string `envconfig:"DIFY_INNER_API_URL" validate:"required"`
+	DifyInnerApiKey string `envconfig:"DIFY_INNER_API_KEY" validate:"required"`
 
 	PluginRemoteInstallingHost             string `envconfig:"PLUGIN_REMOTE_INSTALLING_HOST"`
 	PluginRemoteInstallingPort             uint16 `envconfig:"PLUGIN_REMOTE_INSTALLING_PORT"`