Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Sep 9, 2024
1 parent 50b7c6d commit 63151ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/server/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (app *App) EndpointHandler(ctx *gin.Context, hook_id string, path string) {
if !app.cluster.IsPluginNoCurrentNode(
plugin_entities.PluginUniqueIdentifier(plugin_installation.PluginUniqueIdentifier),
) {
app.redirectPluginInvokeByPluginID(ctx, plugin_entities.PluginUniqueIdentifier(
app.redirectPluginInvokeByPluginIdentifier(ctx, plugin_entities.PluginUniqueIdentifier(
plugin_installation.PluginUniqueIdentifier,
))
} else {
Expand Down
3 changes: 3 additions & 0 deletions internal/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@ func (app *App) pluginGroup(group *gin.RouterGroup, config *app.Config) {
group.Use(CheckingKey(config.PluginInnerApiKey))

group.GET("/asset/:id", controllers.GetAsset)
group.POST("/install", controllers.InstallPlugin)
group.POST("/uninstall", controllers.UninstallPlugin)
group.GET("/list", controllers.ListPlugins)
}
24 changes: 15 additions & 9 deletions internal/server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"github.com/gin-gonic/gin"
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
)

const (
X_PLUGIN_IDENTIFIER = "X-Plugin-Identifier"
X_API_KEY = "X-Api-Key"
)

func CheckingKey(key string) gin.HandlerFunc {
return func(c *gin.Context) {
// get header X-Api-Key
if c.GetHeader("X-Api-Key") != key {
if c.GetHeader(X_API_KEY) != key {
c.JSON(401, gin.H{"error": "Unauthorized"})
c.Abort()
return
Expand Down Expand Up @@ -49,28 +53,30 @@ func (app *App) RedirectPluginInvoke() gin.HandlerFunc {
reader: bytes.NewReader(raw),
}

identity, err := parser.UnmarshalJsonBytes[plugin_entities.BasePluginIdentifier](raw)

if err != nil {
identity := plugin_entities.PluginUniqueIdentifier(ctx.Request.Header.Get(X_PLUGIN_IDENTIFIER))
if identity == "" {
ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid request"})
return
}

// check if plugin in current node
if !app.cluster.IsPluginNoCurrentNode(
identity.PluginUniqueIdentifier,
identity,
) {
app.redirectPluginInvokeByPluginID(ctx, identity.PluginUniqueIdentifier)
app.redirectPluginInvokeByPluginIdentifier(ctx, identity)
ctx.Abort()
} else {
ctx.Next()
}
}
}

func (app *App) redirectPluginInvokeByPluginID(ctx *gin.Context, plugin_id plugin_entities.PluginUniqueIdentifier) {
func (app *App) redirectPluginInvokeByPluginIdentifier(
ctx *gin.Context,
plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
) {
// try find the correct node
nodes, err := app.cluster.FetchPluginAvailableNodesById(plugin_id.String())
nodes, err := app.cluster.FetchPluginAvailableNodesById(plugin_unique_identifier.String())
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{"error": "Internal server error"})
log.Error("fetch plugin available nodes failed: %s", err.Error())
Expand Down

0 comments on commit 63151ef

Please sign in to comment.