From 95b4310838ab6a77047f89818111e9310e04ec6e Mon Sep 17 00:00:00 2001 From: leko Date: Fri, 2 Dec 2022 11:08:43 +0100 Subject: [PATCH] feat: add profiles to web server --- cmd/profile.go | 26 +++++++++++---------- cmd/server.go | 28 +++++++++++++++++------ cmd/session.go | 60 +++++++++++++++++++------------------------------ cmd/validate.go | 2 -- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/cmd/profile.go b/cmd/profile.go index c70d9fd..e777e93 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -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) } diff --git a/cmd/server.go b/cmd/server.go index f1860fd..8a7aa23 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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 { @@ -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" { @@ -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" @@ -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 diff --git a/cmd/session.go b/cmd/session.go index 8887bc6..97545ba 100644 --- a/cmd/session.go +++ b/cmd/session.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "fmt" "sync" "time" @@ -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 @@ -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{}, + }) + } } } @@ -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 } @@ -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" { diff --git a/cmd/validate.go b/cmd/validate.go index bdf1ae8..da83bbe 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -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 }