Skip to content

Commit

Permalink
feat: Modified get by HTTT to refer to device config
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsuki3939 committed Sep 28, 2023
1 parent 5192045 commit 9c5d638
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions internal/core/serve_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand All @@ -41,6 +42,11 @@ import (
"github.com/nttcom/kuesta/pkg/kuesta"
)

const (
ServiceKind = "services"
DeviceKind = "devices"
)

type HttpGetBody struct {
Paths []string `json:"paths"`
}
Expand Down Expand Up @@ -99,12 +105,25 @@ func HttpGet(c echo.Context, rootpath string) error {
c.Logger().Info("get called for http")
req := HttpGetBody{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("HttpGet Please provide valid credentials: %v", err))
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("HttpGet error: Bind: %v", err))
}

res := make([]interface{}, 0)
for _, v := range req.Paths {
path := filepath.Clean(filepath.Join(rootpath, v, "input.cue"))
pathElms := strings.Split(filepath.Clean(v), "/")
if len(pathElms) < 2 {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("HttpGet error: not expected path %v", v))
}
rfile := ""
switch pathElms[1] {
case ServiceKind:
rfile = "input.cue"
case DeviceKind:
rfile = "config.cue"
default:
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("HttpGet error: not expected kind %v", pathElms[1]))
}
path := filepath.Clean(filepath.Join(rootpath, v, rfile))
buf, err := os.ReadFile(path)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("HttpGet error: ReadFile: %v", err))
Expand Down

0 comments on commit 9c5d638

Please sign in to comment.