Skip to content

Commit

Permalink
Merge pull request #66 from yoyofx/dev
Browse files Browse the repository at this point in the history
add action filters and MVC route template. v1.5.0.pre-release
  • Loading branch information
yoyofx authored Jul 23, 2020
2 parents 497a4be + 21b704c commit b56f69d
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 93 deletions.
6 changes: 6 additions & 0 deletions Examples/SimpleWeb/contollers/usercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func (controller UserController) Register(ctx *Context.HttpContext, request *Reg
return ActionResult.Json{Data: result}
}

func (controller UserController) GetUserName(ctx *Context.HttpContext, request *RegisterRequest) ActionResult.IActionResult {
result := Mvc.ApiResult{Success: true, Message: "ok", Data: request}

return ActionResult.Json{Data: result}
}

func (controller UserController) GetInfo() Mvc.ApiResult {

return controller.OK(controller.userAction.Login("zhang"))
Expand Down
18 changes: 18 additions & 0 deletions Examples/SimpleWeb/contollers/userfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package contollers

import (
"fmt"
"github.com/yoyofx/yoyogo/WebFramework/Mvc"
)

type TestActionFilter struct {
}

func (f *TestActionFilter) OnActionExecuting(context Mvc.ActionFilterContext) bool {
fmt.Println("TestActionFilter OnActionExecuting")
return false
}

func (f *TestActionFilter) OnActionExecuted(context Mvc.ActionFilterContext) {
fmt.Println("TestActionFilter OnActionExecuted")
}
2 changes: 1 addition & 1 deletion Examples/SimpleWeb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func CreateCustomBuilder() *Abstractions.HostBuilder {
return YoyoGo.NewWebHostBuilder().
SetEnvironment(Context.Prod).
UseFastHttp().
//UseServer(YoyoGo.DefaultHttps(":8080", "./Certificate/server.pem", "./Certificate/server.key")).
Configure(func(app *YoyoGo.WebApplicationBuilder) {
app.UseStatic("Static")
app.UseEndpoints(registerEndpointRouterConfig)
app.UseMvc(func(builder *Mvc.ControllerBuilder) {
builder.AddController(contollers.NewUserController)
builder.AddFilter("/v1/user/info", &contollers.TestActionFilter{})
})
}).
ConfigureServices(func(serviceCollection *DependencyInjection.ServiceCollection) {
Expand Down
43 changes: 43 additions & 0 deletions Test/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Test

import (
"fmt"
"github.com/magiconair/properties/assert"
"github.com/yoyofx/yoyogo/WebFramework/Mvc"
"testing"
)

type TestActionFilter struct {
}

func (f *TestActionFilter) OnActionExecuting(context Mvc.ActionFilterContext) bool {
fmt.Println("TestActionFilter OnActionExecuted")
return true
}

func (f *TestActionFilter) OnActionExecuted(context Mvc.ActionFilterContext) {
fmt.Println("TestActionFilter OnActionExecuted")
}

func Test_Filter(t *testing.T) {

chain := Mvc.NewActionFilterChain("u*/get*", &TestActionFilter{})

assert.Equal(t, chain.MatchPath("user/getuser"), true)

assert.Equal(t, chain.MatchPath("user/get/1"), true)

assert.Equal(t, chain.MatchPath("/user/get/1"), false)

assert.Equal(t, chain.MatchPath("v1/user/get/1"), false)

filter := chain.MatchFilter("user/get/1")
assert.Equal(t, filter != nil, true)
c := Mvc.ActionFilterContext{}
assert.Equal(t, filter.OnActionExecuting(c), true)
filter.OnActionExecuted(c)

chain1 := Mvc.NewActionFilterChain("v1/user/info", &TestActionFilter{})
assert.Equal(t, chain1.MatchPath("v1/user/info"), true)

}
29 changes: 29 additions & 0 deletions Test/mvc_routetemplate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Test

import (
"github.com/magiconair/properties/assert"
"github.com/yoyofx/yoyogo/WebFramework/Mvc"
"strings"
"testing"
)

func Test_MvcTemplate(t *testing.T) {
url := "v1/usercontroller/register"
template := Mvc.NewRouteTemplate("v1/{controller}/{action}")
assert.Equal(t, template.GetControllerIndex(), 1)
assert.Equal(t, template.GetActionIndex(), 2)

assert.Equal(t, template.Match(strings.Split(url, "/")), true)
assert.Equal(t, template.ControllerName, "usercontroller")
assert.Equal(t, template.ActionName, "register")

template1 := Mvc.NewRouteTemplate("api/v1/{controller}/{action}")
assert.Equal(t, template1.GetControllerIndex(), 2)
assert.Equal(t, template1.GetActionIndex(), 3)

assert.Equal(t, template1.Match(strings.Split(url, "/")), false)
assert.Equal(t, template1.Match(strings.Split("api/"+url, "/")), true)
assert.Equal(t, template1.ControllerName, "usercontroller")
assert.Equal(t, template1.ActionName, "register")

}
180 changes: 140 additions & 40 deletions Test/yoyogo_tests.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
{
"id": "389f20be-d2f8-b845-d7a9-1f59f70a7880",
"id": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"name": "yoyogo tests",
"description": "",
"order": [
"431a63da-2e8d-e946-59d5-5fb96cef7559",
"1ab1ad70-4f1c-df87-84f2-c7514eff51d4",
"d7053b04-46c3-28b2-6a8c-35c341733084",
"b342b42b-32dc-0695-8e42-d10055f1c2f1"
"227012c2-8e71-eb7a-9880-23084f5e9d00",
"e1f59685-61a4-b46b-6223-ba44793d518e",
"35fe21aa-e471-312a-a17e-178a254d4193"
],
"folders": [
{
"name": "Mvc",
"description": "",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"order": [
"c846e43f-889c-842f-5418-e939755675b1",
"5cca23a4-3731-e656-3eff-06e0f41ed4ed",
"69884fda-1355-e242-1153-6066510a30da",
"f7e7b06a-9e95-43df-7a05-ec28fe943548"
],
"owner": "5959537",
"folders_order": [],
"id": "9cd2afdb-c74f-c541-11b6-b93fd3d6e398"
}
],
"folders_order": [
"9cd2afdb-c74f-c541-11b6-b93fd3d6e398"
],
"folders": [],
"folders_order": [],
"timestamp": 1576554457088,
"owner": "5959537",
"public": false,
"requests": [
{
"id": "1ab1ad70-4f1c-df87-84f2-c7514eff51d4",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/v1/api/info",
"queryParams": [],
"pathVariables": {},
"pathVariableData": [],
"preRequestScript": null,
"method": "GET",
"collectionId": "389f20be-d2f8-b845-d7a9-1f59f70a7880",
"data": null,
"dataMode": "params",
"name": "get /v1/api/info (group)",
"description": "group by /v1/api/info",
"descriptionFormat": "html",
"time": 1576554471503,
"version": 2,
"responses": [],
"tests": null,
"currentHelper": "normal",
"helperAttributes": {}
},
{
"id": "431a63da-2e8d-e946-59d5-5fb96cef7559",
"id": "227012c2-8e71-eb7a-9880-23084f5e9d00",
"headers": "Content-Type: application/json\n",
"headerData": [
{
Expand All @@ -62,7 +55,7 @@
"pathVariableData": [],
"preRequestScript": null,
"method": "POST",
"collectionId": "389f20be-d2f8-b845-d7a9-1f59f70a7880",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"data": [],
"dataMode": "raw",
"name": "post /info/:id (params binding)",
Expand All @@ -74,10 +67,11 @@
"tests": null,
"currentHelper": "normal",
"helperAttributes": {},
"rawModeData": "{\n\t\"username\":\"zl\"\n}"
"rawModeData": "{\n\t\"username\":\"zl\"\n}",
"collection_id": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b"
},
{
"id": "b342b42b-32dc-0695-8e42-d10055f1c2f1",
"id": "35fe21aa-e471-312a-a17e-178a254d4193",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/info",
Expand All @@ -86,7 +80,7 @@
"pathVariableData": [],
"preRequestScript": null,
"method": "GET",
"collectionId": "389f20be-d2f8-b845-d7a9-1f59f70a7880",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"data": null,
"dataMode": "params",
"name": "get /info",
Expand All @@ -97,13 +91,59 @@
"responses": [],
"tests": null,
"currentHelper": "normal",
"helperAttributes": {}
"helperAttributes": {},
"collection_id": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b"
},
{
"id": "5cca23a4-3731-e656-3eff-06e0f41ed4ed",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/v1/user/GetInfo",
"folder": "9cd2afdb-c74f-c541-11b6-b93fd3d6e398",
"queryParams": [],
"preRequestScript": null,
"pathVariables": {},
"pathVariableData": [],
"method": "GET",
"data": null,
"dataMode": "params",
"version": 2,
"tests": null,
"currentHelper": "normal",
"helperAttributes": {},
"time": 1595401401008,
"name": "http://localhost:8080/user/GetInfo",
"description": "",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"responses": []
},
{
"id": "69884fda-1355-e242-1153-6066510a30da",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/v1/user/info",
"queryParams": [],
"preRequestScript": null,
"pathVariables": {},
"pathVariableData": [],
"method": "GET",
"data": null,
"dataMode": "params",
"tests": null,
"currentHelper": "normal",
"helperAttributes": {},
"time": 1595401446978,
"name": "http://localhost:8080/v1/user/info",
"description": "",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"responses": []
},
{
"id": "d7053b04-46c3-28b2-6a8c-35c341733084",
"id": "c846e43f-889c-842f-5418-e939755675b1",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/usercontroller/Register?username=max&password=123",
"url": "http://localhost:8080/v1/usercontroller/Register?username=max&password=123",
"folder": "9cd2afdb-c74f-c541-11b6-b93fd3d6e398",
"queryParams": [
{
"key": "username",
Expand All @@ -130,10 +170,70 @@
"tests": null,
"currentHelper": "normal",
"helperAttributes": {},
"time": 1576554478861,
"time": 1595401343825,
"name": "get c:user/a:register (MVC)",
"description": "",
"collectionId": "389f20be-d2f8-b845-d7a9-1f59f70a7880",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"responses": []
},
{
"id": "e1f59685-61a4-b46b-6223-ba44793d518e",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/v1/api/info",
"queryParams": [],
"pathVariables": {},
"pathVariableData": [],
"preRequestScript": null,
"method": "GET",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"data": null,
"dataMode": "params",
"name": "get /v1/api/info (group)",
"description": "group by /v1/api/info",
"descriptionFormat": "html",
"time": 1576554471503,
"version": 2,
"responses": [],
"tests": null,
"currentHelper": "normal",
"helperAttributes": {},
"collection_id": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b"
},
{
"id": "f7e7b06a-9e95-43df-7a05-ec28fe943548",
"headers": "",
"headerData": [],
"url": "http://localhost:8080/v1/user/username?username=max&password=123",
"queryParams": [
{
"key": "username",
"value": "max",
"equals": true,
"description": "",
"enabled": true
},
{
"key": "password",
"value": "123",
"equals": true,
"description": "",
"enabled": true
}
],
"preRequestScript": null,
"pathVariables": {},
"pathVariableData": [],
"method": "GET",
"data": null,
"dataMode": "params",
"tests": null,
"currentHelper": "normal",
"helperAttributes": {},
"time": 1595401513140,
"name": "http://localhost:8080/v1/user/username?username=max&password=123",
"description": "",
"collectionId": "5487971b-3ddd-4a84-d6b6-b23d3591ce8b",
"responses": []
}
]
Expand Down
2 changes: 1 addition & 1 deletion Version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package YoyoGo

const (
//Application Version, such as v1.x.x pre-release
Version = "v1.4.9.8.release"
Version = "v1.5.0.pre-release"
//Application logo
Logo = "IF8gICAgIF8gICAgICAgICAgICAgICAgICAgIF9fXyAgICAgICAgICAKKCApICAgKCApICAgICAgICAgICAgICAgICAgKCAgX2BcICAgICAgICAKYFxgXF8vJy8nXyAgICBfICAgXyAgICBfICAgfCAoIChfKSAgIF8gICAKICBgXCAvJy8nX2BcICggKSAoICkgLydfYFwgfCB8X19fICAvJ19gXCAKICAgfCB8KCAoXykgKXwgKF8pIHwoIChfKSApfCAoXywgKSggKF8pICkKICAgKF8pYFxfX18vJ2BcX18sIHxgXF9fXy8nKF9fX18vJ2BcX19fLycKICAgICAgICAgICAgICggKV98IHwgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgIGBcX19fLycgICAgICAgICAgICBMaWdodCBhbmQgZmFzdC4gIA=="
)
2 changes: 1 addition & 1 deletion WebFramework/Middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func voidMiddleware() middleware {
return newMiddleware(
HandlerFunc(func(ctx *Context.HttpContext, next func(ctx *Context.HttpContext)) {
if ctx.Response.Status() == 0 {
ctx.Response.WriteHeader(400)
ctx.Response.WriteHeader(404)
}
}),
&middleware{},
Expand Down
Loading

0 comments on commit b56f69d

Please sign in to comment.