File: restful-google-custom-method.go

package info (click to toggle)
golang-github-emicklei-go-restful 3.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 792 kB
  • sloc: makefile: 9; sh: 6
file content (42 lines) | stat: -rw-r--r-- 1,239 bytes parent folder | download
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"))
}