mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 12:55:29 +00:00
validate config updates via cmd and api with schema
This commit is contained in:
36
api.go
36
api.go
@@ -5,6 +5,7 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -92,8 +93,41 @@ func (r *Router) handleConfigHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
http.Error(w, "Config update in progress.", http.StatusConflict)
|
http.Error(w, "Config update in progress.", http.StatusConflict)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//TODO(jwetzell): again way too much marshaling
|
||||||
|
cfgBytes, err := io.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cfgMap := make(map[string]any)
|
||||||
|
err = json.Unmarshal(cfgBytes, &cfgMap)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = schema.ApplyDefaults(&cfgMap)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = schema.ValidateConfig(cfgMap)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
validCfgBytes, err := json.Marshal(cfgMap)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var newConfig config.Config
|
var newConfig config.Config
|
||||||
err := json.NewDecoder(req.Body).Decode(&newConfig)
|
err = json.Unmarshal(validCfgBytes, &newConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -15,6 +16,7 @@ import (
|
|||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/module"
|
"github.com/jwetzell/showbridge-go/internal/module"
|
||||||
"github.com/jwetzell/showbridge-go/internal/route"
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/schema"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||||
@@ -103,7 +105,28 @@ func readConfig(configPath string) (config.Config, error) {
|
|||||||
return config.Config{}, err
|
return config.Config{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = yaml.Unmarshal(configBytes, &cfg)
|
//TODO(jwetzell): this is an annoying amount of marshaling
|
||||||
|
|
||||||
|
yamlMap := make(map[string]any)
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(configBytes, &yamlMap)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = schema.ApplyDefaults(&yamlMap)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = schema.ValidateConfig(yamlMap)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
validatedConfigBytes, err := json.Marshal(yamlMap)
|
||||||
|
|
||||||
|
err = json.Unmarshal(validatedConfigBytes, &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config.Config{}, err
|
return config.Config{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/google/jsonschema-go/jsonschema"
|
"github.com/google/jsonschema-go/jsonschema"
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var ConfigSchema = jsonschema.Schema{
|
var ConfigSchema = jsonschema.Schema{
|
||||||
@@ -24,22 +21,19 @@ var ConfigSchema = jsonschema.Schema{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateConfig(config config.Config) error {
|
func ApplyDefaults(cfg *map[string]any) error {
|
||||||
resolvedSchema, err := GetResolvedConfigSchema()
|
resolvedSchema, err := GetResolvedConfigSchema()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(config)
|
return resolvedSchema.ApplyDefaults(cfg)
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
}
|
func ValidateConfig(cfg map[string]any) error {
|
||||||
|
resolvedSchema, err := GetResolvedConfigSchema()
|
||||||
jsonMap := make(map[string]any)
|
if err != nil {
|
||||||
err = json.Unmarshal(jsonBytes, &jsonMap)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
return resolvedSchema.Validate(cfg)
|
||||||
}
|
|
||||||
|
|
||||||
return resolvedSchema.Validate(jsonMap)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetResolvedConfigSchema() (*jsonschema.Resolved, error) {
|
func GetResolvedConfigSchema() (*jsonschema.Resolved, error) {
|
||||||
configSchema := ConfigSchema
|
return ConfigSchema.Resolve(&jsonschema.ResolveOptions{
|
||||||
|
|
||||||
return configSchema.Resolve(&jsonschema.ResolveOptions{
|
|
||||||
Loader: func(uri *url.URL) (*jsonschema.Schema, error) {
|
Loader: func(uri *url.URL) (*jsonschema.Schema, error) {
|
||||||
switch uri.String() {
|
switch uri.String() {
|
||||||
case "https://showbridge.io/modules.schema.json":
|
case "https://showbridge.io/modules.schema.json":
|
||||||
@@ -23,5 +21,6 @@ func GetResolvedConfigSchema() (*jsonschema.Resolved, error) {
|
|||||||
return nil, fmt.Errorf("unknown schema reference: %s", uri.String())
|
return nil, fmt.Errorf("unknown schema reference: %s", uri.String())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
ValidateDefaults: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user