Skip to content

Commit

Permalink
Merge pull request #5 from go-preform/fix/web
Browse files Browse the repository at this point in the history
fix web router bugs and error return
  • Loading branch information
dkishere authored Jun 19, 2024
2 parents 6c6c163 + 3792390 commit d6158aa
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 33 deletions.
1 change: 1 addition & 0 deletions examples/containers/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fasthttp/router v1.5.1 h1:uViy8UYYhm5npJSKEZ4b/ozM//NGzVCfJbh6VJ0VKr8=
github.com/fasthttp/router v1.5.1/go.mod h1:WrmsLo3mrerZP2VEXRV1E8nL8ymJFYCDTr4HmnB8+Zs=
github.com/go-preform/kitchen v0.1.4-r1/go.mod h1:y6G24U/4e0gMiNteBjHyal2Jl5082+tkBEd4QCbhAV4=
github.com/go-zeromq/goczmq/v4 v4.2.2 h1:HAJN+i+3NW55ijMJJhk7oWxHKXgAuSBkoFfvr8bYj4U=
github.com/go-zeromq/goczmq/v4 v4.2.2/go.mod h1:Sm/lxrfxP/Oxqs0tnHD6WAhwkWrx+S+1MRrKzcxoaYE=
github.com/go-zeromq/zmq4 v0.17.0 h1:r12/XdqPeRbuaF4C3QZJeWCt7a5vpJbslDH1rTXF+Kc=
Expand Down
3 changes: 1 addition & 2 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ func (d ZeroLogTraceableCookware[D]) StartTrace(ctx IContext[D], id string, inpu
if d.Logger.GetLevel() > zerolog.DebugLevel {
return ctx, &zeroLogTraceSpan[D]{d.Logger}
}
*d.Logger = d.Logger.With().Str("dishId", id).Logger()
logger := d.Logger.With().Str("action", ctx.Dish().FullName()).Logger()
logger := d.Logger.With().Str("dishId", id).Str("action", ctx.Dish().FullName()).Logger()
logger.Debug().Interface("input", input).Msg("call")
return ctx.GetCtx(), &zeroLogTraceSpan[D]{logger: &logger}
}
Expand Down
10 changes: 7 additions & 3 deletions web/openApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func nodeToSwagger(w kitchen.IInstance, prefix []string, options ...SwaggerOptio
}
if len(queryParams) != 0 {
var (
params = make([]map[string]any, len(queryParams))
params = make([]map[string]any, 0, len(queryParams))
)
for _, p := range queryParams {
params = append(params, map[string]any{
Expand Down Expand Up @@ -399,7 +399,7 @@ func nodeToSwagger(w kitchen.IInstance, prefix []string, options ...SwaggerOptio
body["description"] = tags.Get("desc")
body["operationId"] = tags.Get("operationId")
body["summary"] = tags.Get("summary")
body["tags"] = tags.Get("tags")
body["tags"] = strings.Split(tags.Get("tags"), ",")

m := map[string]any{
strings.ToLower(method): body,
Expand All @@ -423,7 +423,11 @@ func nodeToSwagger(w kitchen.IInstance, prefix []string, options ...SwaggerOptio
},
})
}
body["parameters"] = parameters
if _, ok = body["parameters"]; ok {
body["parameters"] = append(body["parameters"].([]map[string]any), parameters...)
} else {
body["parameters"] = parameters
}
/*if url == "" {
url = "/" + strings.Join(append(prefix, urlParams...), "/")
} else {
Expand Down
22 changes: 16 additions & 6 deletions web/routerHelper/echoHelper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@ func (m *wrapper) AddMenuToRouter(instance kitchen.IInstance, prefix ...string)
prefix[0] = prefix[0][1:]
}
}
for _, node := range instance.Nodes() {
if action, ok = any(node).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
if action, ok = any(instance).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
for _, method := range strings.Split(method, ",") {
m.router.Add(method, strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler)
} else if group, ok = any(node).(kitchen.ISet); ok {
m.AddMenuToRouter(group, append(prefix, strcase.ToSnake(group.Name()))...)
}
} else {
for _, node := range instance.Nodes() {
if action, ok = any(node).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
for _, method := range strings.Split(method, ",") {
m.router.Add(method, strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler)
}
} else if group, ok = any(node).(kitchen.ISet); ok {
m.AddMenuToRouter(group, append(prefix, strcase.ToSnake(group.Name()))...)
}
}
}
return m
Expand Down
22 changes: 16 additions & 6 deletions web/routerHelper/fasthttpHelper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,23 @@ func (m *wrapper) AddMenuToRouter(instance kitchen.IInstance, prefix ...string)
prefix[0] = prefix[0][1:]
}
}
for _, node := range instance.Nodes() {
if action, ok = any(node).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
if action, ok = any(instance).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
for _, method := range strings.Split(method, ",") {
m.router.Handle(method, strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler)
} else if group, ok = any(node).(kitchen.ISet); ok {
m.AddMenuToRouter(group, append(prefix, strcase.ToSnake(group.Name()))...)
}
} else {
for _, node := range instance.Nodes() {
if action, ok = any(node).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
for _, method := range strings.Split(method, ",") {
m.router.Handle(method, strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler)
}
} else if group, ok = any(node).(kitchen.ISet); ok {
m.AddMenuToRouter(group, append(prefix, strcase.ToSnake(group.Name()))...)
}
}
}
return m
Expand Down
30 changes: 17 additions & 13 deletions web/routerHelper/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,38 @@ func (w *WebWriter) Write(data []byte) (int, error) {
}

func WebReturn(a kitchen.IDish, w http.ResponseWriter, outputAny any, err error) {
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
fmt.Println(err)
return
}
var data []byte
if outputAny != nil {
switch outputAny := outputAny.(type) {
case string:
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte(outputAny))
data = []byte(outputAny)
default:
var marshalErr error
if msg, ok := outputAny.(proto.Message); ok {
//todo wrap
w.Header().Set("Content-Type", "application/octet-stream")
data, _ := proto.Marshal(msg)
_, _ = w.Write(data)
data, _ = proto.Marshal(msg)
} else {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(outputAny)
if err != nil {
data, marshalErr = json.Marshal(outputAny)
if marshalErr != nil {
fmt.Println("marshalling error:", err)
}
_, _ = w.Write(data)
}
}
}
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
if data != nil {
_, _ = w.Write(data)
} else {
_, _ = w.Write([]byte(err.Error()))
}
return
}
_, _ = w.Write(data)
}

func ParseRequestToInput(input any, bundle kitchen.IWebBundle, webUrlParamMap []int, isWebInput bool) (processedInput any, raw []byte, err error) {
Expand Down
7 changes: 5 additions & 2 deletions web/routerHelper/muxHelper/wrapper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package muxHelper

import (
"fmt"
"github.com/go-preform/kitchen"
"github.com/go-preform/kitchen/web/routerHelper"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -44,13 +45,15 @@ func (m *wrapper) AddMenuToRouter(instance kitchen.IInstance, prefix ...string)
if action, ok = any(instance).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
m.router.HandleFunc(strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler).Methods(method)
fmt.Println(method, strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"))
m.router.HandleFunc(strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler).Methods(strings.Split(method, ",")...)
} else {
for _, node := range instance.Nodes() {
if action, ok = any(node).(kitchen.IDish); ok {
method, urlParts, handler = m.serveHttp(action)
urlParts = append(prefix, urlParts...)
m.router.HandleFunc(strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler).Methods(method)
fmt.Println(method, strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"))
m.router.HandleFunc(strings.ReplaceAll("/"+strings.Join(urlParts, "/"), "//", "/"), handler).Methods(strings.Split(method, ",")...)
} else if group, ok = any(node).(kitchen.ISet); ok {
m.AddMenuToRouter(group, append(prefix, strcase.ToSnake(group.Name()))...)
}
Expand Down
3 changes: 2 additions & 1 deletion web/routerHelper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func DishUrlAndMethod(dish kitchen.IDish, urlParamWrapper func(string) string) (
}
}
if tags.Get("urlParams") != "" {
urlParams = nil
toAdd := strings.Split(tags.Get("urlParams"), ",")
if tags.Get("urlParamDescs") != "" {
descs := strings.Split(tags.Get("urlParamDescs"), ",")
Expand All @@ -77,7 +78,7 @@ func DishUrlAndMethod(dish kitchen.IDish, urlParamWrapper func(string) string) (
case "GET", "HEAD", "OPTIONS", "PATCH", "TRACE", "CONNECT", "POST", "UPDATE", "DELETE", "PUT":
method = dish.Name()
default:
if tags.Get("path") != "" {
if _, ok := tags.Lookup("path"); ok {
url = tags.Get("path")
} else {
url = strcase.ToSnake(dish.Name())
Expand Down

0 comments on commit d6158aa

Please sign in to comment.