Skip to content

Commit 4287c3c

Browse files
committed
feat: add interactive terminal support and enhance logging
1 parent ea1c416 commit 4287c3c

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

pkg/server/server.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/gin-gonic/gin"
1717
"github.com/iamlongalong/runshell/cmd/runshell/docs"
18+
"github.com/iamlongalong/runshell/pkg/log"
1819
"github.com/iamlongalong/runshell/pkg/types"
1920
swaggerFiles "github.com/swaggo/files"
2021
ginSwagger "github.com/swaggo/gin-swagger"
@@ -99,25 +100,25 @@ func NewServer(executorBuilder types.ExecutorBuilder, addr string) *Server {
99100
start := time.Now()
100101

101102
// 打印请求信息
102-
fmt.Printf("\n=== [REQUEST-%s] %v ===\n", requestID, start.Format("2006-01-02 15:04:05.000"))
103-
fmt.Printf("Path: %s\n", c.Request.URL.Path)
104-
fmt.Printf("Method: %s\n", c.Request.Method)
105-
fmt.Printf("Client IP: %s\n", c.ClientIP())
106-
fmt.Printf("Headers:\n")
103+
log.Debug("\n=== [REQUEST-%s] %v ===", requestID, start.Format("2006-01-02 15:04:05.000"))
104+
log.Debug("Path: %s", c.Request.URL.Path)
105+
log.Debug("Method: %s", c.Request.Method)
106+
log.Debug("Client IP: %s", c.ClientIP())
107+
log.Debug("Headers:")
107108
for k, v := range c.Request.Header {
108-
fmt.Printf(" %s: %v\n", k, v)
109+
log.Debug(" %s: %v", k, v)
109110
}
110-
fmt.Printf("Query Parameters:\n")
111+
log.Debug("Query Parameters:")
111112
for k, v := range c.Request.URL.Query() {
112-
fmt.Printf(" %s: %v\n", k, v)
113+
log.Debug(" %s: %v", k, v)
113114
}
114115

115116
// 如果是 JSON 请求,打印请求体
116117
if c.Request.Body != nil && c.Request.Header.Get("Content-Type") == "application/json" {
117118
bodyBytes, _ := io.ReadAll(c.Request.Body)
118-
// 重新设置 body 以供后���中间件使用
119+
// 重新设置 body 以供后续中间件使用
119120
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
120-
fmt.Printf("Request Body (JSON):\n%s\n", string(bodyBytes))
121+
log.Debug("Request Body (JSON):\n%s", string(bodyBytes))
121122
}
122123

123124
// 创建自定义的 ResponseWriter 来捕获响应
@@ -134,33 +135,33 @@ func NewServer(executorBuilder types.ExecutorBuilder, addr string) *Server {
134135
duration := time.Since(start)
135136

136137
// 打印响应信息
137-
fmt.Printf("\n=== [RESPONSE-%s] %v ===\n", requestID, time.Now().Format("2006-01-02 15:04:05.000"))
138-
fmt.Printf("Duration: %v\n", duration)
139-
fmt.Printf("Status: %d\n", c.Writer.Status())
140-
fmt.Printf("Response Headers:\n")
138+
log.Debug("\n=== [RESPONSE-%s] %v ===", requestID, time.Now().Format("2006-01-02 15:04:05.000"))
139+
log.Debug("Duration: %v", duration)
140+
log.Debug("Status: %d", c.Writer.Status())
141+
log.Debug("Response Headers:")
141142
for k, v := range c.Writer.Header() {
142-
fmt.Printf(" %s: %v\n", k, v)
143+
log.Debug(" %s: %v", k, v)
143144
}
144145

145146
// 打印响应体
146147
if blw.body.Len() > 0 {
147148
contentType := c.Writer.Header().Get("Content-Type")
148-
fmt.Printf("Response Body (%s):\n%s\n", contentType, blw.body.String())
149+
log.Debug("Response Body (%s):\n%s", contentType, blw.body.String())
149150
}
150151

151152
// 如果有错误,打印错误信息
152153
if len(c.Errors) > 0 {
153-
fmt.Printf("\nErrors:\n")
154+
log.Debug("\nErrors:")
154155
for i, err := range c.Errors {
155-
fmt.Printf(" %d. [%v] %v\n", i+1, err.Type, err.Err)
156+
log.Debug(" %d. [%v] %v", i+1, err.Type, err.Err)
156157
if err.Meta != nil {
157-
fmt.Printf(" Meta: %+v\n", err.Meta)
158+
log.Debug(" Meta: %+v", err.Meta)
158159
}
159160
}
160161
}
161162

162-
fmt.Printf("\n=== [END-%s] === Total Time: %v ===\n", requestID, duration)
163-
fmt.Println(strings.Repeat("=", 80))
163+
log.Debug("\n=== [END-%s] === Total Time: %v ===", requestID, duration)
164+
log.Debug(strings.Repeat("=", 80))
164165
})
165166

166167
s := &Server{
@@ -195,8 +196,8 @@ func (w *bodyLogWriter) WriteHeader(status int) {
195196
func (s *Server) setupRoutes() {
196197
// API 文档
197198
s.engine.GET("/swagger/*any", func(c *gin.Context) {
198-
fmt.Printf("Swagger request received: %s\n", c.Request.URL.Path)
199-
fmt.Printf("Swagger request headers: %v\n", c.Request.Header)
199+
log.Debug("Swagger request received: %s", c.Request.URL.Path)
200+
log.Debug("Swagger request headers: %v", c.Request.Header)
200201

201202
if c.Param("any") == "/doc.json" {
202203
doc := docs.SwaggerInfo.ReadDoc()
@@ -246,23 +247,23 @@ func (s *Server) Start() error {
246247
}
247248

248249
// 创建监听器
249-
fmt.Printf("Creating listener for %s\n", s.addr)
250+
log.Info("Creating listener for %s", s.addr)
250251
listener, err := net.Listen("tcp", s.addr)
251252
if err != nil {
252253
return fmt.Errorf("failed to create listener: %w", err)
253254
}
254-
fmt.Printf("Listener created successfully for %s\n", s.addr)
255+
log.Info("Listener created successfully for %s", s.addr)
255256

256257
s.listener = listener
257258
s.server = &http.Server{
258259
Handler: s.engine,
259260
}
260261

261262
// 启动服务器
262-
fmt.Printf("Starting server on %s\n", s.addr)
263+
log.Info("Starting server on %s", s.addr)
263264
go func() {
264265
if err := s.server.Serve(listener); err != nil && err != http.ErrServerClosed {
265-
fmt.Printf("Server error: %v\n", err)
266+
log.Error("Server error: %v", err)
266267
}
267268
}()
268269

@@ -321,7 +322,7 @@ func (s *Server) handleExec(c *gin.Context) {
321322
return
322323
}
323324

324-
fmt.Printf("Received exec request: %+v\n", req)
325+
log.Debug("Received exec request: %+v", req)
325326

326327
executor, err := s.executorBuilder.Build(&types.ExecuteOptions{
327328
WorkDir: req.WorkDir,
@@ -332,7 +333,7 @@ func (s *Server) handleExec(c *gin.Context) {
332333
return
333334
}
334335

335-
fmt.Printf("Created executor: %s\n", executor.Name())
336+
log.Debug("Created executor: %s", executor.Name())
336337

337338
// 准备执行选项
338339
var outputBuf bytes.Buffer
@@ -343,7 +344,7 @@ func (s *Server) handleExec(c *gin.Context) {
343344
Stderr: &outputBuf,
344345
}
345346

346-
fmt.Printf("Prepared execution options: %+v\n", opts)
347+
log.Debug("Prepared execution options: %+v", opts)
347348

348349
// 执行命令
349350
execCtx := &types.ExecuteContext{
@@ -356,16 +357,16 @@ func (s *Server) handleExec(c *gin.Context) {
356357
Executor: executor,
357358
}
358359

359-
fmt.Printf("Executing command: %s %v\n", req.Command, req.Args)
360+
log.Debug("Executing command: %s %v", req.Command, req.Args)
360361

361362
result, err := executor.Execute(execCtx)
362363
if err != nil {
363-
fmt.Printf("Command execution failed: %v\n", err)
364+
log.Error("Command execution failed: %v", err)
364365
s.handleError(c, http.StatusInternalServerError, err, fmt.Sprintf("Command execution failed: %v", err))
365366
return
366367
}
367368

368-
fmt.Printf("Command execution succeeded: %+v\n", result)
369+
log.Info("Command execution succeeded: %+v", result)
369370

370371
// 构造响应
371372
response := ExecResponse{
@@ -581,18 +582,18 @@ func (s *Server) handleError(c *gin.Context, status int, err error, msg string)
581582
_ = c.Error(err)
582583

583584
if status == http.StatusInternalServerError {
584-
fmt.Printf("Internal Server Error Details:\n")
585-
fmt.Printf("Error: %v\n", err)
586-
fmt.Printf("Path: %s\n", c.Request.URL.Path)
587-
fmt.Printf("Method: %s\n", c.Request.Method)
588-
fmt.Printf("Message: %s\n", msg)
589-
fmt.Printf("Headers: %v\n", c.Request.Header)
590-
fmt.Printf("Query: %v\n", c.Request.URL.Query())
585+
log.Error("Internal Server Error Details:")
586+
log.Error("Error: %v", err)
587+
log.Error("Path: %s", c.Request.URL.Path)
588+
log.Error("Method: %s", c.Request.Method)
589+
log.Error("Message: %s", msg)
590+
log.Error("Headers: %v", c.Request.Header)
591+
log.Error("Query: %v", c.Request.URL.Query())
591592
if c.Request.Body != nil {
592593
body, _ := c.GetRawData()
593-
fmt.Printf("Body: %s\n", string(body))
594+
log.Error("Body: %s", string(body))
594595
}
595-
fmt.Println("----------------------------------------")
596+
log.Error("----------------------------------------")
596597
}
597598

598599
if msg == "" {

0 commit comments

Comments
 (0)