Skip to content

Commit

Permalink
feat: addded views/ endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta committed Jun 8, 2022
1 parent 867099d commit 25c16de
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
92 changes: 92 additions & 0 deletions internal/admin/handlers/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package handlers

import (
"fmt"
"net/http"
"path/filepath"
"regexp"

"github.com/yohamta/dagu/internal/controller"
"github.com/yohamta/dagu/internal/filters"
"github.com/yohamta/dagu/internal/views"
)

type viewResponse struct {
Title string
Charset string
DAGs []*controller.DAG
Errors []string
HasError bool
}

type ViewHandlerConfig struct {
DAGsDir string
}

func HandleGetView(hc *ViewHandlerConfig) http.HandlerFunc {
renderFunc := useTemplate("index.gohtml", "index")

return func(w http.ResponseWriter, r *http.Request) {
p, err := getViewParameter(r)
if err != nil {
encodeError(w, err)
return
}

view, err := views.GetView(p)
if err != nil {
encodeError(w, err)
return
}

dir := filepath.Join(hc.DAGsDir, "")
dags, errs, err := controller.GetDAGs(dir)
if err != nil {
encodeError(w, err)
return
}

filteredDags := []*controller.DAG{}
filter := &filters.ContainTags{
Tags: view.ContainTags,
}
for _, d := range dags {
if filter.Matches(d.Config) {
filteredDags = append(filteredDags, d)
}
}

hasErr := false
for _, j := range dags {
if j.Error != nil {
hasErr = true
break
}
}
if len(errs) > 0 {
hasErr = true
}

data := &viewResponse{
Title: "View",
DAGs: filteredDags,
Errors: errs,
HasError: hasErr,
}

if isJsonRequest(r) {
renderJson(w, data)
} else {
renderFunc(w, data)
}
}
}

func getViewParameter(r *http.Request) (string, error) {
re := regexp.MustCompile(`/views/([^/\?]+)/?$`)
m := re.FindStringSubmatch(r.URL.Path)
if len(m) < 2 {
return "", fmt.Errorf("invalid URL")
}
return m[1], nil
}
29 changes: 29 additions & 0 deletions internal/admin/handlers/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package handlers

import (
"net/http"

"github.com/yohamta/dagu/internal/models"
"github.com/yohamta/dagu/internal/views"
)

type viewListResponse struct {
Title string
Charset string
Views []*models.View
}

func HandleGetViewList() http.HandlerFunc {
renderFunc := useTemplate("index.gohtml", "index")
return func(w http.ResponseWriter, r *http.Request) {
data := &viewListResponse{
Title: "DAGList",
Views: views.GetViews(),
}
if r.Header.Get("Accept") == "application/json" {
renderJson(w, data)
} else {
renderFunc(w, data)
}
}
}
6 changes: 6 additions & 0 deletions internal/admin/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func defaultRoutes(cfg *Config) []*route {
DAGsDir: cfg.DAGs,
},
)},
{http.MethodGet, `^/view/?$`, handlers.HandleGetViewList()},
{http.MethodGet, `^/views/([^/]+)?$`, handlers.HandleGetView(
&handlers.ViewHandlerConfig{
DAGsDir: cfg.DAGs,
},
)},
{http.MethodPost, `^/dags/?$`, handlers.HandlePostListAction(
&handlers.DAGListHandlerConfig{
DAGsDir: cfg.DAGs,
Expand Down

0 comments on commit 25c16de

Please sign in to comment.