diff --git a/application/application.go b/application/application.go index ce956d5..39b2f55 100644 --- a/application/application.go +++ b/application/application.go @@ -153,6 +153,7 @@ func New(cfg *conf.Conf) (*Application, error) { r.Get("/", a.TaskHandler(false)) r.Get("/volumes/*", a.VolumesHandler(6, false)) r.Get("/logs", a.TaskLogsHandler(false)) + r.Get("/logz", a.TaskLogzHandler(false)) }) r.Group(func(r chi.Router) { r.Use(a.RefererMiddleware) @@ -166,6 +167,7 @@ func New(cfg *conf.Conf) (*Application, error) { r.Get("/", a.TaskHandler(true)) r.Get("/volumes/*", a.VolumesHandler(6, true)) r.Get("/logs", a.TaskLogsHandler(true)) + r.Get("/logz", a.TaskLogzHandler(true)) }) r.Group(func(r chi.Router) { r.Use(a.RefererMiddleware) diff --git a/application/logs.go b/application/logs.go new file mode 100644 index 0000000..5940659 --- /dev/null +++ b/application/logs.go @@ -0,0 +1,45 @@ +package application + +import ( + "bytes" + "context" + "fmt" + "html/template" + "net/http" + + "github.com/docker/docker/pkg/stdcopy" + "github.com/factorysh/microdensity/html" + "github.com/factorysh/microdensity/task" +) + +func (a *Application) renderLogsPageForTask(ctx context.Context, t *task.Task, w http.ResponseWriter) error { + + reader, err := t.Logs(ctx, false) + if err != nil { + return err + } + + var buffer bytes.Buffer + var dropped bytes.Buffer + _, err = stdcopy.StdCopy(&buffer, &dropped, reader) + if err != nil { + return err + } + + data, err := NewTaskPage(t, template.HTML(fmt.Sprintf("
%s", buffer.String())), a.GitlabURL, "Task Logs", "terminal") + if err != nil { + return err + } + + p := html.Page{ + Domain: a.Domain, + Detail: fmt.Sprintf("%s / %s - logs", t.Service, t.Commit), + Partial: html.Partial{ + Template: taskTemplate, + Data: data, + }, + } + + w.WriteHeader(http.StatusOK) + return p.Render(w) +} diff --git a/application/result.go b/application/result.go deleted file mode 100644 index ae291f6..0000000 --- a/application/result.go +++ /dev/null @@ -1,38 +0,0 @@ -package application - -import ( - "html/template" - "net/url" - - "github.com/factorysh/microdensity/task" -) - -// Result of a task for html rendering -type Result struct { - GitlabDomain string - Domain string - Project string - Commit string - Service string - ID string - CreatedAt string - Inner template.HTML -} - -// NewResultFromTask inits a result for a task -func NewResultFromTask(t *task.Task, inner template.HTML, gitlabDomain string) (*Result, error) { - prettyPath, err := url.PathUnescape(t.Project) - if err != nil { - return nil, err - } - - return &Result{ - Project: prettyPath, - GitlabDomain: gitlabDomain, - Commit: t.Commit, - Service: t.Service, - ID: t.Id.String(), - CreatedAt: t.Creation.Format("2006-01-02 15:04:05"), - Inner: inner, - }, nil -} diff --git a/application/task.go b/application/task.go index abf5d3e..8f3cecd 100644 --- a/application/task.go +++ b/application/task.go @@ -231,3 +231,39 @@ func (a *Application) TaskLogsHandler(latest bool) func(http.ResponseWriter, *ht } } + +// TaskLogzHandler get a logs for a task +func (a *Application) TaskLogzHandler(latest bool) func(http.ResponseWriter, *http.Request) { + + return func(w http.ResponseWriter, r *http.Request) { + l := a.logger.With( + zap.String("url", r.URL.String()), + zap.String("service", chi.URLParam(r, "serviceID")), + zap.String("project", chi.URLParam(r, "project")), + zap.String("branch", chi.URLParam(r, "branch")), + zap.String("commit", chi.URLParam(r, "commit")), + ) + + t, err := a.storage.GetByCommit( + chi.URLParam(r, "serviceID"), + chi.URLParam(r, "project"), + chi.URLParam(r, "branch"), + chi.URLParam(r, "commit"), + latest, + ) + if err != nil { + l.Warn("Task get error", zap.Error(err)) + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(http.StatusText(http.StatusNotFound))) + return + } + + err = a.renderLogsPageForTask(r.Context(), t, w) + if err != nil { + l.Warn("when rendering a logs page", zap.Error(err)) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return + } + } +} diff --git a/application/task_page.go b/application/task_page.go new file mode 100644 index 0000000..4c3ebd3 --- /dev/null +++ b/application/task_page.go @@ -0,0 +1,42 @@ +package application + +import ( + "html/template" + "net/url" + + "github.com/factorysh/microdensity/task" +) + +// TaskPage present a page and an associated inner content +type TaskPage struct { + GitlabDomain string + Domain string + Project string + Commit string + Service string + ID string + CreatedAt string + InnerDivClasses string + InnerTitle string + Inner template.HTML +} + +// NewTaskPage inits a result for a task +func NewTaskPage(t *task.Task, inner template.HTML, gitlabDomain, InnerTitle, InnerDivClasses string) (*TaskPage, error) { + prettyPath, err := url.PathUnescape(t.Project) + if err != nil { + return nil, err + } + + return &TaskPage{ + Project: prettyPath, + GitlabDomain: gitlabDomain, + Commit: t.Commit, + Service: t.Service, + ID: t.Id.String(), + CreatedAt: t.Creation.Format("2006-01-02 15:04:05"), + InnerTitle: InnerTitle, + InnerDivClasses: InnerDivClasses, + Inner: inner, + }, nil +} diff --git a/application/templates/result.html b/application/templates/task.html similarity index 87% rename from application/templates/result.html rename to application/templates/task.html index 2ba9793..e9991bb 100644 --- a/application/templates/result.html +++ b/application/templates/task.html @@ -8,8 +8,8 @@