/
service_logs.go
145 lines (131 loc) · 3.59 KB
/
service_logs.go
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
package server
import (
"bufio"
"context"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/kuzznya/letsdeploy/app/apperrors"
"github.com/kuzznya/letsdeploy/app/core"
"github.com/kuzznya/letsdeploy/app/middleware"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strconv"
"time"
)
func ServiceLogStreamEndpoint(r *gin.Engine, c *core.Core, rdb *redis.Client) {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
r.GET("/api/v1/services/:id/logs", func(ctx *gin.Context) {
token := ctx.Query("token")
if token == "" {
log.Errorln("Token is not provided")
_ = ctx.AbortWithError(http.StatusUnauthorized, apperrors.Unauthorized("Token is not provided"))
return
}
username, err := rdb.Get(ctx, token).Result()
if err != nil {
log.WithError(err).Errorln("Invalid token provided")
_ = ctx.AbortWithError(http.StatusForbidden, apperrors.ForbiddenWrap(err, "Invalid token provided"))
return
}
rdb.Del(ctx, token)
idStr := ctx.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
_ = ctx.AbortWithError(http.StatusBadRequest, apperrors.BadRequest("Failed to parse service id"))
}
replicaStr := ctx.Query("replica")
replica := 0
if replicaStr != "" {
replica, err = strconv.Atoi(replicaStr)
if err != nil {
_ = ctx.AbortWithError(http.StatusBadRequest, apperrors.BadRequest("Failed to parse replica index"))
}
}
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
if err != nil {
log.WithError(err).Errorln("Failed to upgrade to WebSocket")
_ = ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
logCtx, cancel := context.WithCancel(ctx)
logs, err := c.Services.StreamServiceLogs(logCtx, id, replica, middleware.Authentication{Username: username})
if err != nil {
_ = ctx.AbortWithError(http.StatusInternalServerError, err)
}
r := bufio.NewReader(logs)
conn.SetCloseHandler(func(code int, text string) error {
log.Debugln("WebSocket connection closed")
cancel()
return nil
})
startMonitorConn(conn, cancel)
for {
line, isPrefix, err := r.ReadLine()
if err != nil {
log.WithError(err).Errorln("Failed to read service logs")
_ = conn.Close()
cancel()
return
}
if !isPrefix {
line = append(line, '\n')
}
_ = conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
err = conn.WriteMessage(websocket.TextMessage, line)
if err != nil {
log.WithError(err).Errorln("Failed to write service logs to WebSocket")
_ = conn.Close()
cancel()
return
}
}
})
}
func startMonitorConn(conn *websocket.Conn, cancel context.CancelFunc) {
go func() {
for {
err := conn.SetReadDeadline(time.Now().Add(20 * time.Second))
if err != nil {
log.WithError(err).Errorln("WebSocket ping read timed out")
_ = conn.Close()
cancel()
return
}
msgType, reader, err := conn.NextReader()
if err != nil {
log.WithError(err).Errorln("WebSocket ping read failed")
_ = conn.Close()
cancel()
return
}
if msgType != websocket.TextMessage {
log.Errorln("WebSocket ping unknown message")
_ = conn.Close()
cancel()
return
}
data, err := io.ReadAll(reader)
if err != nil {
log.WithError(err).Errorln("WebSocket ping read failed")
_ = conn.Close()
cancel()
return
}
if string(data) != "ping" {
log.Errorf("WebSocket ping unknown message: %s", string(data))
_ = conn.Close()
cancel()
return
}
log.Debugln("Ping received")
}
}()
}