You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB
Go

/*
* Copyright 2025 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package task
import (
"context"
"embed"
"mime"
"path/filepath"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/cloudwego/hertz/pkg/route"
"github.com/cloudwego/eino-examples/quickstart/eino_assistant/pkg/tool/task"
)
//go:embed web
var webContent embed.FS
// BindRoutes 注册路由
func BindRoutes(r *route.RouterGroup) error {
ctx := context.Background()
taskTool, err := task.NewTaskToolImpl(ctx, &task.TaskToolConfig{
Storage: task.GetDefaultStorage(),
})
if err != nil {
return err
}
// API 处理
r.POST("/api", func(ctx context.Context, c *app.RequestContext) {
var req task.TaskRequest
if err := c.Bind(&req); err != nil {
c.JSON(consts.StatusBadRequest, map[string]string{
"status": "error",
"error": err.Error(),
})
return
}
resp, err := taskTool.Invoke(ctx, &req)
if err != nil {
c.JSON(consts.StatusInternalServerError, map[string]string{
"status": "error",
"error": err.Error(),
})
return
}
c.JSON(consts.StatusOK, resp)
})
// 静态文件服务
r.GET("/", func(ctx context.Context, c *app.RequestContext) {
content, err := webContent.ReadFile("web/index.html")
if err != nil {
c.String(consts.StatusNotFound, "File not found")
return
}
c.Header("Content-Type", "text/html")
c.Write(content)
})
r.GET("/:file", func(ctx context.Context, c *app.RequestContext) {
file := c.Param("file")
content, err := webContent.ReadFile("web/" + file)
if err != nil {
c.String(consts.StatusNotFound, "File not found")
return
}
contentType := mime.TypeByExtension(filepath.Ext(file))
if contentType == "" {
contentType = "application/octet-stream"
}
c.Header("Content-Type", contentType)
c.Write(content)
})
return nil
}