MCPcopy Index your code
hub / github.com/SMNETSTUDIO/You2API / Handler

Function Handler

api/main.go:110–209  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

108var originalModel string
109
110func Handler(w http.ResponseWriter, r *http.Request) {
111 if r.URL.Path != "/v1/chat/completions" {
112 w.Header().Set("Content-Type", "application/json")
113 json.NewEncoder(w).Encode(map[string]string{
114 "status": "You2Api Service Running...",
115 "message": "MoLoveSze...",
116 })
117 return
118 }
119
120 w.Header().Set("Access-Control-Allow-Origin", "*")
121 w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
122 w.Header().Set("Access-Control-Allow-Headers", "*")
123
124 if r.Method == "OPTIONS" {
125 w.WriteHeader(http.StatusOK)
126 return
127 }
128
129 authHeader := r.Header.Get("Authorization")
130 if !strings.HasPrefix(authHeader, "Bearer ") {
131 http.Error(w, "Missing or invalid authorization header", http.StatusUnauthorized)
132 return
133 }
134 dsToken := strings.TrimPrefix(authHeader, "Bearer ")
135
136 var openAIReq OpenAIRequest
137 if err := json.NewDecoder(r.Body).Decode(&openAIReq); err != nil {
138 http.Error(w, "Invalid request body", http.StatusBadRequest)
139 return
140 }
141
142 originalModel = openAIReq.Model
143 lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1].Content
144 var chatHistory []map[string]interface{}
145 for _, msg := range openAIReq.Messages {
146 chatMsg := map[string]interface{}{
147 "question": msg.Content,
148 "answer": "",
149 }
150 if msg.Role == "assistant" {
151 chatMsg["question"] = ""
152 chatMsg["answer"] = msg.Content
153 }
154 chatHistory = append(chatHistory, chatMsg)
155 }
156
157 chatHistoryJSON, _ := json.Marshal(chatHistory)
158
159 youReq, _ := http.NewRequest("GET", "https://you.com/api/streamingSearch", nil)
160
161 q := youReq.URL.Query()
162 q.Add("q", lastMessage)
163 q.Add("page", "1")
164 q.Add("count", "10")
165 q.Add("safeSearch", "Moderate")
166 q.Add("mkt", "zh-HK")
167 q.Add("enable_worklow_generation_ux", "true")

Callers

nothing calls this directly

Calls 4

mapModelNameFunction · 0.85
getCookiesFunction · 0.85
handleStreamingResponseFunction · 0.85

Tested by

no test coverage detected