Skip to content

Commit

Permalink
add examples for issue519 with path in root
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Jun 17, 2023
1 parent abc6f6e commit 047e50d
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/issue519/old39_path/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/emicklei/go-restful/examples/trimslash

go 1.14

require (
github.com/emicklei/go-restful/v3 v3.9.0
github.com/json-iterator/go v1.1.12 // indirect
)
17 changes: 17 additions & 0 deletions examples/issue519/old39_path/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
31 changes: 31 additions & 0 deletions examples/issue519/old39_path/restful-hello-world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"io"
"log"
"net/http"

restful "github.com/emicklei/go-restful/v3"
)

// This example shows the effect of the 3.9.0 behavior.
//
// curl http://localhost:8080/say/hello -> to you
// curl http://localhost:8080/say/hello/ -> to you

func main() {
ws := new(restful.WebService)
ws.Path("/say")
ws.Route(ws.GET("/hello").To(hello1))
ws.Route(ws.GET("/hello/").To(hello2))
restful.Add(ws)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func hello1(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "world")
}

func hello2(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "to you")
}
8 changes: 8 additions & 0 deletions examples/issue519/pathjoin_path/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/emicklei/go-restful/examples/trimslash

go 1.14

require (
github.com/emicklei/go-restful/v3 v3.10.2
github.com/json-iterator/go v1.1.12 // indirect
)
17 changes: 17 additions & 0 deletions examples/issue519/pathjoin_path/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE=
github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
31 changes: 31 additions & 0 deletions examples/issue519/pathjoin_path/restful-hello-world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"io"
"log"
"net/http"

restful "github.com/emicklei/go-restful/v3"
)

// This example shows the effect of the default MergePathStrategy (restful.PathJoinStrategy).
//
// curl http://localhost:8080/say/hello -> world
// curl http://localhost:8080/say/hello/ -> 404

func main() {
ws := new(restful.WebService)
ws.Path("/say")
ws.Route(ws.GET("/hello").To(hello1))
ws.Route(ws.GET("/hello/").To(hello2))
restful.Add(ws)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func hello1(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "world")
}

func hello2(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "to you")
}
8 changes: 8 additions & 0 deletions examples/issue519/trimslash_path/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/emicklei/go-restful/examples/trimslash

go 1.14

require (
github.com/emicklei/go-restful/v3 v3.10.2
github.com/json-iterator/go v1.1.12 // indirect
)
17 changes: 17 additions & 0 deletions examples/issue519/trimslash_path/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE=
github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
38 changes: 38 additions & 0 deletions examples/issue519/trimslash_path/restful-hello-world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"io"
"log"
"net/http"

restful "github.com/emicklei/go-restful/v3"
)

// This example shows the minimal code needed to get a restful.WebService working.
//
// curl http://localhost:8080/say/hello -> world
// curl http://localhost:8080/say/hello/ -> to you
// curl http://localhost:8080/say/hi -> world
// curl http://localhost:8080/say/hi/ -> 404
// curl http://localhost:8080/say/hey -> to you
// curl http://localhost:8080/say/hey/ -> 404

func main() {
restful.MergePathStrategy = restful.TrimSlashStrategy
ws := new(restful.WebService)
ws.Path("/say")
ws.Route(ws.GET("/hello").To(hello1))
ws.Route(ws.GET("/hello/").To(hello2))
ws.Route(ws.GET("/hi").To(hello1))
ws.Route(ws.GET("/hey").To(hello2))
restful.Add(ws)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func hello1(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "world")
}

func hello2(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "to you")
}

0 comments on commit 047e50d

Please sign in to comment.