-
Notifications
You must be signed in to change notification settings - Fork 690
/
restful-google-custom-method.go
42 lines (32 loc) · 1.21 KB
/
restful-google-custom-method.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"io"
"log"
"net/http"
. "github.com/emicklei/go-restful/v3"
)
// 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"))
}