New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support google custome verb #413
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
32a2a29
feat: support google custome verb
ShiningRush 87bf942
fix: avoid regex check
ShiningRush 8a766ee
complete: custom method doc and example
ShiningRush 730608b
fix: custome method example
ShiningRush 553e8ca
fix: both check route and token
ShiningRush 7fe3ec9
fix: remove go mod file
ShiningRush File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,4 @@ examples/restful-html-template | |
|
||
s.html | ||
restful-path-tail | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package restful | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
customVerbReg = regexp.MustCompile(":([A-Za-z]+)$") | ||
) | ||
|
||
func hasCustomVerb(routeToken string) bool { | ||
return customVerbReg.MatchString(routeToken) | ||
} | ||
|
||
func isMatchCustomVerb(routeToken string, pathToken string) bool { | ||
rs := customVerbReg.FindStringSubmatch(routeToken) | ||
if len(rs) < 2 { | ||
return false | ||
} | ||
|
||
customVerb := rs[1] | ||
specificVerbReg := regexp.MustCompile(fmt.Sprintf(":%s$", customVerb)) | ||
return specificVerbReg.MatchString(pathToken) | ||
} | ||
|
||
func removeCustomVerb(str string) string { | ||
return customVerbReg.ReplaceAllString(str, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package restful | ||
|
||
import "testing" | ||
|
||
func TestHasCustomVerb(t *testing.T) { | ||
testCase := []struct { | ||
path string | ||
has bool | ||
}{ | ||
{"/{userId}:init", true}, | ||
{"/{userId:init}", false}, | ||
{"/users/{id:init}:init", true}, | ||
{"/users/{id}", false}, | ||
} | ||
|
||
for _, v := range testCase { | ||
rs := hasCustomVerb(v.path) | ||
if rs != v.has { | ||
t.Errorf("path: %v should has no custom verb", v.path) | ||
} | ||
} | ||
} | ||
|
||
func TestRemoveCustomVerb(t *testing.T) { | ||
testCase := []struct { | ||
path string | ||
expectedPath string | ||
}{ | ||
{"/{userId}:init", "/{userId}"}, | ||
{"/{userId:init}", "/{userId:init}"}, | ||
{"/users/{id:init}:init", "/users/{id:init}"}, | ||
{"/users/{id}", "/users/{id}"}, | ||
{"/init/users/{id:init}:init", "/init/users/{id:init}"}, | ||
} | ||
|
||
for _, v := range testCase { | ||
rs := removeCustomVerb(v.path) | ||
if rs != v.expectedPath { | ||
t.Errorf("expected value: %v, actual: %v", v.expectedPath, rs) | ||
} | ||
} | ||
} | ||
func TestMatchCustomVerb(t *testing.T) { | ||
testCase := []struct { | ||
routeToken string | ||
pathToken string | ||
expected bool | ||
}{ | ||
{"{userId:regex}:init", "creator-123456789gWe:init", true}, | ||
{"{userId:regex}:init", "creator-123456789gWe", false}, | ||
{"{userId:regex}", "creator-123456789gWe:init", false}, | ||
{"users:init", "users:init", true}, | ||
{"users:init", "tokens:init", true}, | ||
} | ||
|
||
for idx, v := range testCase { | ||
rs := isMatchCustomVerb(v.routeToken, v.pathToken) | ||
if rs != v.expected { | ||
t.Errorf("expected value: %v, actual: %v, index: [%v]", v.expected, rs, idx) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
. "github.com/emicklei/go-restful" | ||
"io" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
// This example shows how to create a Route with google custom method | ||
// Requires the use of a CurlyRouter and path should end with the custom method | ||
// | ||
// GET http://localhost:8080/resource:validate | ||
// POST http://localhost:8080/resource/some-resource-id:init | ||
// POST http://localhost:8080/resource/some-resource-id:recycle | ||
|
||
func main() { | ||
DefaultContainer.Router(CurlyRouter{}) | ||
ws := new(WebService) | ||
|
||
ws.Route(ws.GET("/resource:validate").To(validateHandler)) | ||
ws.Route(ws.POST("/resource/{resourceId}:init").To(initHandler)) | ||
ws.Route(ws.POST("/resource/{resourceId}:recycle").To(recycleHandler)) | ||
|
||
Add(ws) | ||
|
||
println("[go-restful] serve path tails from http://localhost:8080/basepath") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func validateHandler(req *Request, resp *Response) { | ||
io.WriteString(resp, "validate resource completed") | ||
} | ||
|
||
func initHandler(req *Request, resp *Response) { | ||
io.WriteString(resp, "init resource completed, resourceId: "+req.PathParameter("resourceId")) | ||
} | ||
|
||
func recycleHandler(req *Request, resp *Response) { | ||
io.WriteString(resp, "recycle resource completed, resourceId: "+req.PathParameter("resourceId")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,11 +49,15 @@ type Route struct { | |
|
||
//Overrides the container.contentEncodingEnabled | ||
contentEncodingEnabled *bool | ||
|
||
// indicate route path has custom verb | ||
hasCustomVerb bool | ||
} | ||
|
||
// Initialize for Route | ||
func (r *Route) postBuild() { | ||
r.pathParts = tokenizePath(r.Path) | ||
r.hasCustomVerb = hasCustomVerb(r.Path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
} | ||
|
||
// Create Request and Response from their http versions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will fail: "/init/users/{id:init}:init" , "/init/users/{id:init}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i added it to case, and run success, is something wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh, i did not execute this test. i should have said "will this fail?". I came up with this test because I saw "ReplaceAllString" in removeCustomVerb. And this case "/:init/users:init/" is probably not a valid path, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emmm, it likes "/users/{id:init}:init", so i think the case is not necessary.
and replace regex is ":customVerb$", it will just replace last word.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, good!