1
1
package application
2
2
3
3
import (
4
+ "bytes"
5
+ "context"
4
6
"encoding/json"
7
+ "fmt"
8
+ "html/template"
5
9
"net/http"
6
10
"net/url"
7
11
"os"
8
12
"time"
9
13
10
14
"github.com/docker/docker/pkg/stdcopy"
11
15
_claims "github.com/factorysh/microdensity/claims"
16
+ "github.com/factorysh/microdensity/html"
12
17
"github.com/factorysh/microdensity/task"
13
18
"github.com/go-chi/chi/v5"
14
19
"github.com/go-chi/render"
15
20
"github.com/google/uuid"
21
+ "github.com/robert-nix/ansihtml"
16
22
"go.uber.org/zap"
17
23
)
18
24
@@ -182,8 +188,8 @@ func (a *Application) TaskIDHandler(w http.ResponseWriter, r *http.Request) {
182
188
}
183
189
}
184
190
185
- // TaskLogsHandler get a logs for a task
186
- func (a * Application ) TaskLogsHandler (latest bool ) func (http.ResponseWriter , * http.Request ) {
191
+ // TaskLogzHandler get a logs for a task
192
+ func (a * Application ) TaskLogzHandler (latest bool ) func (http.ResponseWriter , * http.Request ) {
187
193
188
194
return func (w http.ResponseWriter , r * http.Request ) {
189
195
l := a .logger .With (
@@ -219,7 +225,7 @@ func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *ht
219
225
220
226
// just stdout for now
221
227
// kudos @ndeloof, @rumpl, @glours
222
- _ , err = stdcopy .StdCopy (w , nil , reader )
228
+ _ , err = stdcopy .StdCopy (w , w , reader )
223
229
if err != nil {
224
230
l .Error ("Task log stdcopy write error" , zap .Error (err ))
225
231
w .WriteHeader (http .StatusInternalServerError )
@@ -232,8 +238,8 @@ func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *ht
232
238
233
239
}
234
240
235
- // TaskLogzHandler get a logs for a task
236
- func (a * Application ) TaskLogzHandler (latest bool ) func (http.ResponseWriter , * http.Request ) {
241
+ // TaskLogsHandler get a logs for a task, used to get row logs from curl, not used for now
242
+ func (a * Application ) TaskLogsHandler (latest bool ) func (http.ResponseWriter , * http.Request ) {
237
243
238
244
return func (w http.ResponseWriter , r * http.Request ) {
239
245
l := a .logger .With (
@@ -267,3 +273,34 @@ func (a *Application) TaskLogzHandler(latest bool) func(http.ResponseWriter, *ht
267
273
}
268
274
}
269
275
}
276
+
277
+ func (a * Application ) renderLogsPageForTask (ctx context.Context , t * task.Task , w http.ResponseWriter ) error {
278
+
279
+ reader , err := t .Logs (ctx , false )
280
+ if err != nil {
281
+ return err
282
+ }
283
+
284
+ var buffer bytes.Buffer
285
+ _ , err = stdcopy .StdCopy (& buffer , & buffer , reader )
286
+ if err != nil {
287
+ return err
288
+ }
289
+
290
+ data , err := NewTaskPage (t , template .HTML (fmt .Sprintf ("<pre>%s</pre>" , ansihtml .ConvertToHTML (buffer .Bytes ()))), a .GitlabURL , "Task Logs" , "terminal" )
291
+ if err != nil {
292
+ return err
293
+ }
294
+
295
+ p := html.Page {
296
+ Domain : a .Domain ,
297
+ Detail : fmt .Sprintf ("%s / %s - logs" , t .Service , t .Commit ),
298
+ Partial : html.Partial {
299
+ Template : taskTemplate ,
300
+ Data : data ,
301
+ },
302
+ }
303
+
304
+ w .WriteHeader (http .StatusOK )
305
+ return p .Render (w )
306
+ }
0 commit comments