Skip to content

Commit

Permalink
update kit docs, tweaks to kit interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Dec 7, 2023
1 parent 766472f commit 5d129ff
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 62 deletions.
15 changes: 15 additions & 0 deletions api/val_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ func (val *Val) IsArrayString() bool {
return val.Type == ArrayString
}

func (val *Val) IsEmpty() bool {
switch val.Type {
case Unknown, Nil:
return true
case String:
return val.String() == ""
case ArrayNumber:
return len(val.arrayNumVal) == 0
case ArrayString:
return len(val.arrayStrVal) == 0
default:
return false
}
}

// UnmarshalJSON implements the json.Unmarshaller interface.
func (val *Val) UnmarshalJSON(value []byte) error {
defErr := errors.New("value must be type boolean, number, string, []number, or []string; nested objects are not supported")
Expand Down
4 changes: 3 additions & 1 deletion components/broker/engine/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (brk *broker) AuthorizeComponent(ctx context.Context, comp *core.Component,
}
}

// TODO is this safe?
// TODO use platform status to check if component is platform component.
platformCompName := fmt.Sprintf("%s-%s", config.Platform, comp.Name)
if !strings.HasSuffix(svcAccName, comp.GroupKey()) && svcAccName != platformCompName {
return fmt.Errorf("service account name does not match component")
Expand All @@ -233,6 +233,8 @@ func (brk *broker) AuthorizeComponent(ctx context.Context, comp *core.Component,
return fmt.Errorf("unauthorized component: %s", review.Status.Error)
}

// TODO make sure component exists in an AppDeployment

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/kubefox/components/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
who kit.EnvVar
who kit.EnvVarDep
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/kubefox/components/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
backend kit.Dependency
backend kit.ComponentDep
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/kubefox/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/xigxog/kubefox/examples/hello-world/kubefox
go 1.21

// TODO update when kubefox is released
require github.com/xigxog/kubefox v0.2.5-alpha.0.20231206144113-48f17b47cb37
require github.com/xigxog/kubefox v0.2.5-alpha.0.20231207161131-766472f99055

require (
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/hello-world/kubefox/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xigxog/kubefox v0.2.5-alpha.0.20231206144113-48f17b47cb37 h1:jgXm3ga62ZnY8oejlI173Z7TfTBtXeOZbJQpArrXiw4=
github.com/xigxog/kubefox v0.2.5-alpha.0.20231206144113-48f17b47cb37/go.mod h1:9qjm+/xmMZUbXct3cf37+rOsEwapMtLORwZWExzZgT0=
github.com/xigxog/kubefox v0.2.5-alpha.0.20231207161131-766472f99055 h1:xZK7obOPCJcH7PrAJUE5f4SxRBka/ujvGJYtpXTCYIM=
github.com/xigxog/kubefox v0.2.5-alpha.0.20231207161131-766472f99055/go.mod h1:9qjm+/xmMZUbXct3cf37+rOsEwapMtLORwZWExzZgT0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
2 changes: 1 addition & 1 deletion kit/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Client struct {
wrapped *graphql.Client
}

func New(ktx kit.Kontext, dependency kit.Dependency) *Client {
func New(ktx kit.Kontext, dependency kit.ComponentDep) *Client {
return &Client{
ktx: ktx,
wrapped: graphql.NewClient("", &http.Client{
Expand Down
27 changes: 16 additions & 11 deletions kit/kit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const (
maxAttempts = 5
)

// TODO also support declarative routes? Example:
//
// kit.RouteBuilder().
// Header("host", "google.com").
// Query("param1", "fish").
// Handler(myHandler)

type kit struct {
spec *api.ComponentDetails

Expand Down Expand Up @@ -112,6 +119,10 @@ func (svc *kit) Log() *logkf.Logger {
return svc.log
}

func (svc *kit) L() *logkf.Logger {
return svc.log
}

func (svc *kit) Title(title string) {
svc.spec.Title = title
}
Expand All @@ -120,12 +131,6 @@ func (svc *kit) Description(description string) {
svc.spec.Title = description
}

// TODO also support declarative routes? Example:
//
// kit.RouteBuilder().
// Header("host", "google.com").
// Query("param1", "fish").
// Handler(myHandler)
func (svc *kit) Route(rule string, handler EventHandler) {
r := &route{
RouteSpec: api.RouteSpec{
Expand All @@ -143,7 +148,7 @@ func (svc *kit) Default(handler EventHandler) {
svc.spec.DefaultHandler = handler != nil
}

func (svc *kit) EnvVar(name string, opts ...env.VarOption) EnvVar {
func (svc *kit) EnvVar(name string, opts ...env.VarOption) EnvVarDep {
if name == "" {
svc.log.Fatal("environment variable name is required")
}
Expand All @@ -160,15 +165,15 @@ func (svc *kit) EnvVar(name string, opts ...env.VarOption) EnvVar {
return env.NewVar(name, schema.Type)
}

func (svc *kit) Component(name string) Dependency {
func (svc *kit) Component(name string) ComponentDep {
return svc.dependency(name, api.ComponentTypeKubeFox)
}

func (svc *kit) HTTPAdapter(name string) Dependency {
func (svc *kit) HTTPAdapter(name string) ComponentDep {
return svc.dependency(name, api.ComponentTypeHTTP)
}

func (svc *kit) dependency(name string, typ api.ComponentType) Dependency {
func (svc *kit) dependency(name string, typ api.ComponentType) ComponentDep {
c := &dependency{
typ: typ,
name: name,
Expand Down Expand Up @@ -263,7 +268,7 @@ func (svc *kit) recvReq(req *grpc.ComponentEvent) {
log.Error(err)

errEvt := core.NewErr(err, core.EventOpts{})
if err := ktx.ForwardResp(errEvt).Send(); err != nil {
if err := ktx.Resp().Forward(errEvt); err != nil {
log.Error(err)
}
}
Expand Down
47 changes: 25 additions & 22 deletions kit/kontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,31 @@ func (k *kontext) Log() *logkf.Logger {
return k.log
}

func (k *kontext) Env(v EnvVar) string {
func (k *kontext) Env(v EnvVarDep) string {
return k.EnvV(v).String()
}

func (k *kontext) EnvV(v EnvVar) *api.Val {
func (k *kontext) EnvV(v EnvVarDep) *api.Val {
val, _ := api.ValProto(k.env[v.Name()])
return val
}

func (k *kontext) EnvDef(v EnvVar, def string) string {
func (k *kontext) EnvDef(v EnvVarDep, def string) string {
if val := k.Env(v); val == "" {
return def
} else {
return val
}
}

func (k *kontext) EnvDefV(v EnvVar, def *api.Val) *api.Val {
if val := k.EnvV(v); val == nil {
func (k *kontext) EnvDefV(v EnvVarDep, def *api.Val) *api.Val {
if val := k.EnvV(v); val.IsEmpty() {
return def
} else {
return val
}
}

func (k *kontext) ForwardResp(resp core.EventReader) Resp {
return &respKontext{
Event: core.CloneToResp(resp.(*core.Event), core.EventOpts{
Parent: k.Event,
Source: k.kit.brk.Component,
Target: k.Event.Source,
}),
ktx: k,
}
}

func (k *kontext) Resp() Resp {
return &respKontext{
Event: core.NewResp(core.EventOpts{
Expand All @@ -93,7 +82,7 @@ func (k *kontext) Resp() Resp {
}
}

func (k *kontext) Req(c Dependency) Req {
func (k *kontext) Req(c ComponentDep) Req {
return &reqKontext{
Event: core.NewReq(core.EventOpts{
Type: c.EventType(),
Expand All @@ -105,7 +94,7 @@ func (k *kontext) Req(c Dependency) Req {
}
}

func (k *kontext) Forward(c Dependency) Req {
func (k *kontext) Forward(c ComponentDep) Req {
return &reqKontext{
Event: core.CloneToReq(k.Event, core.EventOpts{
Type: c.EventType(),
Expand All @@ -117,13 +106,13 @@ func (k *kontext) Forward(c Dependency) Req {
}
}

func (k *kontext) HTTP(c Dependency) *http.Client {
func (k *kontext) HTTP(c ComponentDep) *http.Client {
return &http.Client{
Transport: k.Transport(c),
}
}

func (k *kontext) Transport(c Dependency) http.RoundTripper {
func (k *kontext) Transport(c ComponentDep) http.RoundTripper {
return &EventRoundTripper{
req: &reqKontext{
Event: core.NewReq(core.EventOpts{
Expand All @@ -137,9 +126,19 @@ func (k *kontext) Transport(c Dependency) http.RoundTripper {
}
}

func (resp *respKontext) SendStr(val string) error {
func (resp *respKontext) Forward(evt core.EventReader) error {
resp.Event = core.CloneToResp(evt.(*core.Event), core.EventOpts{
Parent: resp.ktx.Event,
Source: resp.ktx.kit.brk.Component,
Target: resp.ktx.Event.Source,
})

return resp.Send()
}

func (resp *respKontext) SendStr(val ...string) error {
c := fmt.Sprintf("%s; %s", api.ContentTypePlain, api.CharSetUTF8)
return resp.SendBytes(c, []byte(val))
return resp.SendBytes(c, []byte(strings.Join(val, "")))
}

func (resp *respKontext) SendHTML(val string) error {
Expand Down Expand Up @@ -170,6 +169,10 @@ func (resp *respKontext) SendAccepts(json any, html, str string) error {
}

func (resp *respKontext) SendReader(contentType string, reader io.Reader) error {
if closer, ok := reader.(io.ReadCloser); ok {
defer closer.Close()
}

bytes, err := io.ReadAll(reader)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 5d129ff

Please sign in to comment.