Skip to content

Commit

Permalink
feat: add profiles to web server
Browse files Browse the repository at this point in the history
  • Loading branch information
lekotros committed Dec 2, 2022
1 parent a4f2550 commit 95b4310
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
26 changes: 14 additions & 12 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import (
"github.com/concreteit/greenlight/internal"
)

type ValidationProfile struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Version string `json:"version"`
Scripts []ProfileScript `json:"scripts"`
type Profile struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Version string `json:"version"`
Scripts []Script `json:"scripts"`
}

type ProfileScript struct {
Name string `json:"name"`
Config internal.M `json:"config"`
type Script struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Version string `json:"string"`
Config internal.M `json:"config"`
}

func OpenProfile(path string) (*ValidationProfile, error) {
buf, err := os.ReadFile(path)
func OpenProfile(path string) (*Profile, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}

v := &ValidationProfile{}
return v, json.Unmarshal(buf, v)
v := &Profile{}
return v, json.NewDecoder(f).Decode(v)
}
28 changes: 21 additions & 7 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ func startServer(cmd *cobra.Command, args []string) {
return c.JSON(http.StatusOK, session)
})

e.POST("/api/sessions/:sid/profile", func(c echo.Context) error {
profile := &Profile{}
if err := c.Bind(profile); err != nil {
return err
}

session := sessions.Get(c.Param("sid"))
if session == nil {
return fmt.Errorf("session not found")
} else if session.Status != "created" {
return fmt.Errorf("session already processed")
}

session.Profile = profile

return c.JSON(http.StatusOK, session)
})

e.POST("/api/sessions/:sid/upload", func(c echo.Context) error {
session := sessions.Get(c.Param("sid"))
if session == nil {
Expand Down Expand Up @@ -155,12 +173,7 @@ func startServer(cmd *cobra.Command, args []string) {
})

e.GET("/api/sessions/:sid/validate", func(c echo.Context) error {
query := ValidationQuery{}
if err := c.Bind(&query); err != nil {
return err
}

session := sessions.Get(query.SessionID)
session := sessions.Get(c.Param("sid"))
if session == nil {
return fmt.Errorf("session not found")
} else if session.Status != "created" {
Expand All @@ -169,7 +182,7 @@ func startServer(cmd *cobra.Command, args []string) {

session.Status = "running"

v, err := session.NewValidation(query.Schema, query.Rules)
v, err := session.NewValidation()
if err != nil {
session.Stopped = time.Now()
session.Status = "failure"
Expand Down Expand Up @@ -301,6 +314,7 @@ func startServer(cmd *cobra.Command, args []string) {
wsProxy := websocketproxy.NewProxy(u)
wsProxy.Upgrader = &websocket.Upgrader{}
wsProxy.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }

e.GET("/ws", func(c echo.Context) error {
wsProxy.ServeHTTP(c.Response(), c.Request())
return nil
Expand Down
60 changes: 23 additions & 37 deletions cmd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -44,15 +43,17 @@ func (s *SessionMap) New() (*Session, error) {
}

type Session struct {
ID string `json:"id"`
Created time.Time `json:"created"`
Stopped time.Time `json:"stopped"`
ID string `json:"id"`
Created time.Time `json:"created"`
Stopped time.Time `json:"stopped"`
Status string `json:"status"`
Profile *Profile `json:"profile"`
Results []greenlight.ValidationResult

fileContext *FileContext `json:"-"`
Status string `json:"status"`
Results []greenlight.ValidationResult
}

func (s *Session) NewValidation(schema string, rules []string) (*greenlight.Validation, error) {
func (s *Session) NewValidation() (*greenlight.Validation, error) {
validation, err := greenlight.NewValidation()
if err != nil {
return nil, err
Expand All @@ -72,21 +73,20 @@ func (s *Session) NewValidation(schema string, rules []string) (*greenlight.Vali

file.File.Seek(0, 0)

rvs := []*greenlight.RuleValidation{{
Name: "xsd",
Valid: false,
ErrorCount: 0,
Errors: []greenlight.TaskError{},
}}

if rules != nil {
for _, rule := range rules {
rvs = append(rvs, &greenlight.RuleValidation{
Name: rule,
Valid: false,
ErrorCount: 0,
Errors: []greenlight.TaskError{},
})
rvs := []*greenlight.RuleValidation{}

for _, script := range s.Profile.Scripts {
for name, s := range scripts {
if name == script.Name {
validation.AddScript(s, script.Config)

rvs = append(rvs, &greenlight.RuleValidation{
Name: script.Name,
Valid: false,
ErrorCount: 0,
Errors: []greenlight.TaskError{},
})
}
}
}

Expand All @@ -97,21 +97,6 @@ func (s *Session) NewValidation(schema string, rules []string) (*greenlight.Vali
})
}

validation.AddScript(scripts["xsd"], map[string]interface{}{
"schema": schema,
})

if rules != nil {
for _, r := range rules {
script := scripts[r]
if script == nil {
return nil, fmt.Errorf("script '%v' not found", r)
}

validation.AddScript(script, nil)
}
}

return validation, nil
}

Expand All @@ -122,6 +107,7 @@ func (s Session) MarshalJSON() ([]byte, error) {
"files": s.fileContext.Find("xml"),
"status": s.Status,
"results": s.Results,
"profile": s.Profile,
}

if s.Status != "created" && s.Status != "running" {
Expand Down
2 changes: 0 additions & 2 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,9 @@ func validate(cmd *cobra.Command, args []string) {
if silent {
for _, r := range res {
if !r.Valid {
fmt.Println(false)
return
}
}
fmt.Println(true)
return
}

Expand Down

0 comments on commit 95b4310

Please sign in to comment.