Skip to content

Commit f346bb3

Browse files
committed
Implement kernel.Request structure
1 parent ded876e commit f346bb3

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to the "Go-Web Framework" will be documented in this file.
44

55
## [Unreleased]
66

7+
## [v0.9.0-beta] - 2021-09-19
8+
### Changed
9+
- Included validation payload in kernel.Request structure.
10+
- Now you can access to a decoded payload by calling the kernel.Request object (in controller)
11+
712
## [v0.8.2-beta] - 2021-09-03
813
### Added
914
- New global logger handler

kernel/router.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kernel
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"github.com/RobyFerro/dig"
67
"github.com/RobyFerro/go-web-framework/register"
@@ -14,6 +15,8 @@ import (
1415

1516
var SingletonIOC *dig.Container
1617

18+
type Request map[string]interface{}
19+
1720
// WebRouter parses routing structures and set every route.
1821
// Return a Gorilla Mux router instance with all routes indicated in router.yml file.
1922
func WebRouter(routes []register.HTTPRouter) *mux.Router {
@@ -53,7 +56,7 @@ func HandleSingleRoute(routes []register.Route, router *mux.Router) {
5356
return
5457
}
5558

56-
executeControllerDirective(directive, writer, request)
59+
executeControllerDirective(directive, writer, request, validation)
5760
}).Methods(route.Method)
5861

5962
subRouter.Use(parseMiddleware(route.Middleware)...)
@@ -67,7 +70,7 @@ func HandleSingleRoute(routes []register.Route, router *mux.Router) {
6770
return
6871
}
6972

70-
executeControllerDirective(directive, writer, request)
73+
executeControllerDirective(directive, writer, request, validation)
7174
}).Methods(route.Method)
7275
}
7376
}
@@ -92,7 +95,7 @@ func HandleGroups(groups []register.Group, router *mux.Router) {
9295
return
9396
}
9497

95-
executeControllerDirective(directive, writer, request)
98+
executeControllerDirective(directive, writer, request, validation)
9699
}).Methods(route.Method)
97100

98101
nestedRouter.Use(parseMiddleware(route.Middleware)...)
@@ -106,7 +109,7 @@ func HandleGroups(groups []register.Group, router *mux.Router) {
106109
return
107110
}
108111

109-
executeControllerDirective(directive, writer, request)
112+
executeControllerDirective(directive, writer, request, validation)
110113
}).Methods(route.Method)
111114
}
112115
}
@@ -142,8 +145,18 @@ func GetControllerInterface(directive []string, w http.ResponseWriter, r *http.R
142145
// Example: MainController@main
143146
// executes the main method from MainController
144147
// It build the CUSTOM SERVICE CONTAINER and invoke the selected directive inside them.
145-
func executeControllerDirective(d []string, w http.ResponseWriter, r *http.Request) {
148+
func executeControllerDirective(d []string, w http.ResponseWriter, r *http.Request, validation interface{}) {
146149
container := BuildCustomContainer()
150+
payload := structToMap(validation)
151+
152+
err := container.Provide(func() Request {
153+
return payload
154+
})
155+
156+
if err != nil {
157+
log.Fatal(err)
158+
}
159+
147160
cc := GetControllerInterface(d, w, r)
148161
method := reflect.ValueOf(cc).MethodByName(d[1])
149162

@@ -152,6 +165,14 @@ func executeControllerDirective(d []string, w http.ResponseWriter, r *http.Reque
152165
}
153166
}
154167

168+
func structToMap(s interface{}) map[string]interface{} {
169+
m := make(map[string]interface{})
170+
j, _ := json.Marshal(s)
171+
_ = json.Unmarshal(j, &m)
172+
173+
return m
174+
}
175+
155176
func validateRequest(data interface{}, r *http.Request) error {
156177
if data != nil {
157178
if err := tool.DecodeJsonRequest(r, &data); err != nil {

0 commit comments

Comments
 (0)