feat: rename gateway spec to proxy

This commit is contained in:
2023-03-21 13:28:41 +01:00
parent fa36d55163
commit fbcd3ca806
21 changed files with 216 additions and 183 deletions

View File

@ -0,0 +1,17 @@
package proxy
import (
_ "embed"
"forge.cadoles.com/Cadoles/emissary/internal/spec"
"github.com/pkg/errors"
)
//go:embed schema.json
var schema []byte
func init() {
if err := spec.Register(NameProxy, schema); err != nil {
panic(errors.WithStack(err))
}
}

View File

@ -0,0 +1,29 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://proxy.emissary.cadoles.com/spec.json",
"title": "ProxySpec",
"description": "Emissary 'Proxy' specification",
"type": "object",
"properties": {
"proxies": {
"type": "object",
"patternProperties": {
".*": {
"type": "object",
"properties": {
"address": {
"type": "string"
},
"target": {
"type": "string"
}
},
"required": ["address", "target"],
"additionalProperties": false
}
}
}
},
"required": ["proxies"],
"additionalProperties": false
}

View File

@ -0,0 +1,40 @@
package proxy
import "forge.cadoles.com/Cadoles/emissary/internal/spec"
const NameProxy spec.Name = "proxy.emissary.cadoles.com"
type ID string
type Spec struct {
Revision int `json:"revision"`
Proxies map[ID]ProxyEntry `json:"proxies"`
}
type ProxyEntry struct {
Address string `json:"address"`
Target string `json:"target"`
}
func (s *Spec) SpecName() spec.Name {
return NameProxy
}
func (s *Spec) SpecRevision() int {
return s.Revision
}
func (s *Spec) SpecData() map[string]any {
return map[string]any{
"proxies": s.Proxies,
}
}
func NewSpec() *Spec {
return &Spec{
Revision: -1,
Proxies: make(map[ID]ProxyEntry),
}
}
var _ spec.Spec = &Spec{}

View File

@ -0,0 +1,13 @@
{
"name": "proxy.emissary.cadoles.com",
"data": {
"proxies": {
"cadoles.com": {
"address": ":3003",
"target": "https://www.cadoles.com",
"foo": "bar"
}
}
},
"revision": 0
}

View File

@ -0,0 +1,11 @@
{
"name": "proxy.emissary.cadoles.com",
"data": {
"proxies": {
"cadoles.com": {
"address": ":3003"
}
}
},
"revision": 0
}

View File

@ -0,0 +1,12 @@
{
"name": "proxy.emissary.cadoles.com",
"data": {
"proxies": {
"cadoles.com": {
"address": ":3003",
"target": "https://www.cadoles.com"
}
}
},
"revision": 0
}

View File

@ -0,0 +1,75 @@
package proxy
import (
"context"
"encoding/json"
"io/ioutil"
"testing"
"forge.cadoles.com/Cadoles/emissary/internal/spec"
"github.com/pkg/errors"
)
type validatorTestCase struct {
Name string
Source string
ShouldFail bool
}
var validatorTestCases = []validatorTestCase{
{
Name: "SpecOK",
Source: "testdata/spec-ok.json",
ShouldFail: false,
},
{
Name: "SpecMissingProp",
Source: "testdata/spec-missing-prop.json",
ShouldFail: true,
},
{
Name: "SpecAdditionalProp",
Source: "testdata/spec-additional-prop.json",
ShouldFail: true,
},
}
func TestValidator(t *testing.T) {
t.Parallel()
validator := spec.NewValidator()
if err := validator.Register(NameProxy, schema); err != nil {
t.Fatalf("+%v", errors.WithStack(err))
}
for _, tc := range validatorTestCases {
func(tc validatorTestCase) {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
rawSpec, err := ioutil.ReadFile(tc.Source)
if err != nil {
t.Fatalf("+%v", errors.WithStack(err))
}
var spec spec.RawSpec
if err := json.Unmarshal(rawSpec, &spec); err != nil {
t.Fatalf("+%v", errors.WithStack(err))
}
ctx := context.Background()
err = validator.Validate(ctx, &spec)
if !tc.ShouldFail && err != nil {
t.Errorf("+%v", errors.WithStack(err))
}
if tc.ShouldFail && err == nil {
t.Error("validation should have failed")
}
})
}(tc)
}
}