feat: add Gin HTTP API with SSE streaming chat
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"eino-test/internal/rag"
|
||||
"eino-test/internal/store"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type NoteHandler struct {
|
||||
pipeline *rag.RAGPipeline
|
||||
}
|
||||
|
||||
func NewNoteHandler(pipeline *rag.RAGPipeline) *NoteHandler {
|
||||
return &NoteHandler{pipeline: pipeline}
|
||||
}
|
||||
|
||||
func (h *NoteHandler) ListNotes(c *gin.Context) {
|
||||
notes, err := h.pipeline.NoteStore().List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var resp []NoteResponse
|
||||
for _, n := range notes {
|
||||
resp = append(resp, NoteResponse{
|
||||
ID: n.ID,
|
||||
Title: n.Title,
|
||||
Tags: n.Tags,
|
||||
CreatedAt: n.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (h *NoteHandler) GetNote(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
note, err := h.pipeline.NoteStore().GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "note not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, NoteResponse{
|
||||
ID: note.ID,
|
||||
Title: note.Title,
|
||||
Content: note.Content,
|
||||
Tags: note.Tags,
|
||||
CreatedAt: note.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *NoteHandler) CreateNote(c *gin.Context) {
|
||||
var req NoteRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
note := &store.Note{
|
||||
ID: req.Title,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
Tags: req.Tags,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
if err := h.pipeline.NoteStore().Create(c.Request.Context(), note); err != nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.pipeline.IndexNote(c.Request.Context(), note); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, NoteResponse{
|
||||
ID: note.ID,
|
||||
Title: note.Title,
|
||||
Content: note.Content,
|
||||
Tags: note.Tags,
|
||||
CreatedAt: note.CreatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *NoteHandler) DeleteNote(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.pipeline.NoteStore().Delete(c.Request.Context(), id); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "note not found"})
|
||||
return
|
||||
}
|
||||
h.pipeline.RemoveNote(id)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
||||
}
|
||||
Reference in New Issue
Block a user