mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 07:43:41 +08:00 
			
		
		
		
	refactor: abusing goroutines and channel (#1561)
* refactor: abusing goroutines * fix: trim data prefix * refactor: move functions to render package * refactor: add back trim & flush --------- Co-authored-by: JustSong <quanpengsong@gmail.com>
This commit is contained in:
		
							
								
								
									
										29
									
								
								common/render/render.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								common/render/render.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
package render
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func StringData(c *gin.Context, str string) {
 | 
			
		||||
	str = strings.TrimPrefix(str, "data: ")
 | 
			
		||||
	str = strings.TrimSuffix(str, "\r")
 | 
			
		||||
	c.Render(-1, common.CustomEvent{Data: "data: " + str})
 | 
			
		||||
	c.Writer.Flush()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ObjectData(c *gin.Context, object interface{}) error {
 | 
			
		||||
	jsonData, err := json.Marshal(object)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error marshalling object: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	StringData(c, string(jsonData))
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Done(c *gin.Context) {
 | 
			
		||||
	StringData(c, "[DONE]")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user