mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-23 10:15:22 -04:00
b6906f7eb5
* feat: Generate HTTP server routes from template - Added a new file `http_server.gen.go` to automatically generate HTTP server routes based on defined dispatchers. - Refactored existing route definitions in `http_server.go` to utilize the generated routes, improving maintainability. - Introduced a code generation function in `generator.go` to create the HTTP server file, enhancing the plugin development workflow. - Updated the template for HTTP server generation to streamline route creation for various controllers. * fix: Update OAuth paths in PluginDispatchers for consistency - Changed the path for authorization URL from `/oauth/authorization_url` to `/oauth/get_authorization_url`. - Updated the path for credentials from `/oauth/credentials` to `/oauth/get_credentials` to align with naming conventions.
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
package generator
|
|
|
|
// controllerTemplate is the template for generating controller files
|
|
const controllerTemplate = `// Code generated by controller generator. DO NOT EDIT.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/service"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/requests"
|
|
)
|
|
|
|
{{range .Dispatchers}}
|
|
func {{.Name}}(config *app.Config) gin.HandlerFunc {
|
|
type request = plugin_entities.InvokePluginRequest[{{.RequestTypeString}}]
|
|
|
|
return func(c *gin.Context) {
|
|
BindPluginDispatchRequest(
|
|
c,
|
|
func(itr request) {
|
|
service.{{.Name}}(&itr, c, config.PluginMaxExecutionTimeout)
|
|
},
|
|
)
|
|
}
|
|
}
|
|
{{end}}
|
|
`
|
|
|
|
// serviceTemplate is the template for generating service files
|
|
const serviceTemplate = `// Code generated by controller generator. DO NOT EDIT.
|
|
|
|
package service
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/requests"
|
|
)
|
|
|
|
{{range .Dispatchers}}
|
|
func {{.Name}}(
|
|
r *plugin_entities.InvokePluginRequest[{{.RequestTypeString}}],
|
|
ctx *gin.Context,
|
|
max_timeout_seconds int,
|
|
) {
|
|
baseSSEWithSession(
|
|
func(session *session_manager.Session) (*stream.Stream[{{.ResponseTypeString}}], error) {
|
|
return plugin_daemon.{{.Name}}(session, &r.Data)
|
|
},
|
|
{{.AccessTypeString}},
|
|
{{.AccessActionString}},
|
|
r,
|
|
ctx,
|
|
max_timeout_seconds,
|
|
)
|
|
}
|
|
{{end}}
|
|
`
|
|
|
|
// pluginDaemonTemplate is the template for generating plugin daemon files
|
|
const pluginDaemonTemplate = `// Code generated by controller generator. DO NOT EDIT.
|
|
|
|
package plugin_daemon
|
|
|
|
import (
|
|
"github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/requests"
|
|
)
|
|
|
|
{{range .Dispatchers}}
|
|
func {{.Name}}(
|
|
session *session_manager.Session,
|
|
request *{{.RequestTypeString}},
|
|
) (
|
|
*stream.Stream[{{.ResponseTypeString}}], error,
|
|
) {
|
|
return GenericInvokePlugin[{{.RequestTypeString}}, {{.ResponseTypeString}}](
|
|
session,
|
|
request,
|
|
{{.BufferSize}},
|
|
)
|
|
}
|
|
{{end}}
|
|
`
|
|
|
|
const httpServerTemplate = `// Code generated by controller generator. DO NOT EDIT.
|
|
package server
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/server/controllers"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
|
|
)
|
|
|
|
func (app *App) setupGeneratedRoutes(group *gin.RouterGroup, config *app.Config) {
|
|
{{- range .Dispatchers}}
|
|
group.POST("{{.Path}}", controllers.{{.Name}}(config))
|
|
{{- end}}
|
|
}
|
|
`
|