forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.go
More file actions
239 lines (209 loc) · 6.09 KB
/
Copy pathstdio.go
File metadata and controls
239 lines (209 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http/httptest"
"os"
"github.com/rs/zerolog/log"
)
// stdioRequest represents an incoming JSON request from stdin
type stdioRequest struct {
ID string `json:"id"`
Method string `json:"method"`
Params map[string]interface{} `json:"params,omitempty"`
}
// stdioResponse represents an outgoing JSON response to stdout
type stdioResponse struct {
ID string `json:"id"`
Success bool `json:"success"`
Code int `json:"code"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
// stdioServer handles stdin/stdout JSON-based API by wrapping HTTP handlers
type stdioServer struct {
server *server
stdin io.Reader
stdout io.Writer
}
// NewStdioServer creates a new stdio server instance
func NewStdioServer(s *server) *stdioServer {
return &stdioServer{
server: s,
stdin: os.Stdin,
stdout: os.Stdout,
}
}
// newStdioServerWithIO creates a stdio server with custom IO streams (for testing)
func newStdioServerWithIO(s *server, stdin io.Reader, stdout io.Writer) *stdioServer {
return &stdioServer{
server: s,
stdin: stdin,
stdout: stdout,
}
}
func (ss *stdioServer) Start() error {
log.Info().Msg("Starting stdio mode - reading JSON requests from stdin")
scanner := bufio.NewScanner(ss.stdin)
const maxCapacity = 512 * 1024 // 512KB
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) == 0 {
continue // Skip empty lines
}
ss.handleRequest(line)
}
// Scanner stopped, check why
if err := scanner.Err(); err != nil {
log.Error().Err(err).Msg("Error reading from stdin")
return err
}
log.Info().Msg("EOF reached on stdin, shutting down")
return nil
}
func (ss *stdioServer) handleRequest(requestBytes []byte) {
var req stdioRequest
if err := json.Unmarshal(requestBytes, &req); err != nil {
ss.sendError("", 400, fmt.Sprintf("invalid JSON request: %v", err))
return
}
if req.ID == "" {
ss.sendError("", 400, "missing request id")
return
}
if req.Method == "" {
ss.sendError(req.ID, 400, "missing method")
return
}
log.Info().
Str("id", req.ID).
Str("method", req.Method).
Msg("Processing stdio request")
ss.routeRequest(&req)
}
// routeRequest dispatches the request to the appropriate HTTP handler
func (ss *stdioServer) routeRequest(req *stdioRequest) {
// Map stdio method to HTTP route and method
var httpMethod, httpPath string
switch req.Method {
case "health":
httpMethod = "GET"
httpPath = "/health"
default:
ss.sendError(req.ID, 404, fmt.Sprintf("unknown method: %s", req.Method))
return
}
ss.executeHTTPHandler(req, httpMethod, httpPath)
}
// executeHTTPHandler wraps the existing HTTP handler and adapts it for stdio
func (ss *stdioServer) executeHTTPHandler(req *stdioRequest, httpMethod, httpPath string) {
// Create a mock HTTP request
var body io.Reader
if req.Params != nil && len(req.Params) > 0 {
jsonParams, err := json.Marshal(req.Params)
if err != nil {
ss.sendError(req.ID, 400, fmt.Sprintf("invalid params: %v", err))
return
}
body = bytes.NewReader(jsonParams)
}
httpReq := httptest.NewRequest(httpMethod, httpPath, body)
httpReq.Header.Set("Content-Type", "application/json")
// Set user token header (for user authentication)
if token, ok := req.Params["token"].(string); ok {
httpReq.Header.Set("token", token)
}
// Set admin token header (for admin authentication)
if adminToken, ok := req.Params["adminToken"].(string); ok {
httpReq.Header.Set("Authorization", adminToken)
}
recorder := httptest.NewRecorder()
ss.server.router.ServeHTTP(recorder, httpReq)
ss.convertHTTPResponse(req.ID, recorder)
}
// convertHTTPResponse converts an HTTP response to a stdio response
func (ss *stdioServer) convertHTTPResponse(requestID string, recorder *httptest.ResponseRecorder) {
statusCode := recorder.Code
responseBody := recorder.Body.Bytes()
var responseData interface{}
if len(responseBody) > 0 {
if err := json.Unmarshal(responseBody, &responseData); err != nil {
// If it's not JSON, just use the raw string
responseData = string(responseBody)
}
}
success := statusCode >= 200 && statusCode < 300
if respMap, ok := responseData.(map[string]interface{}); ok {
// If it's already in wuzapi format, extract the data/error
if data, hasData := respMap["data"]; hasData {
ss.sendSuccess(requestID, statusCode, data)
return
}
if errMsg, hasError := respMap["error"]; hasError {
if errStr, ok := errMsg.(string); ok {
ss.sendError(requestID, statusCode, errStr)
return
}
}
ss.sendSuccess(requestID, statusCode, respMap)
return
}
// For non-JSON or simple responses
if success {
ss.sendSuccess(requestID, statusCode, responseData)
} else {
errorMsg := "request failed"
if str, ok := responseData.(string); ok && str != "" {
errorMsg = str
}
ss.sendError(requestID, statusCode, errorMsg)
}
}
func (ss *stdioServer) sendSuccess(id string, code int, data interface{}) {
response := stdioResponse{
ID: id,
Success: true,
Code: code,
Data: data,
}
ss.writeResponse(response)
}
func (ss *stdioServer) sendError(id string, code int, errorMsg string) {
response := stdioResponse{
ID: id,
Success: false,
Code: code,
Error: errorMsg,
}
ss.writeResponse(response)
}
func (ss *stdioServer) writeResponse(response stdioResponse) {
// Marshalled response as single line
responseBytes, err := json.Marshal(response)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal response")
fallback := stdioResponse{
ID: response.ID,
Success: false,
Code: 500,
Error: "internal error: failed to marshal response",
}
responseBytes, err = json.Marshal(fallback)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal fallback response")
return
}
}
// Write to stdout with newline
fmt.Fprintf(ss.stdout, "%s\n", string(responseBytes))
log.Debug().
Str("id", response.ID).
Bool("success", response.Success).
Int("code", response.Code).
Msg("Sent stdio response")
}