go restful

go http web

1
2
3
4
5
6
7
8
9
10
11
12
//基础 web server
func simpleHttpServer(){
//1. 往serverMux的 map[string]muxEntry 添加路由规则pattern以及封装了handler的muxEntry
//2. 匹配pattern,Handler被调用
http.HandleFunc("/hello", SayHello)
//1. 实例化Server
http.ListenAndServe(":8000",nil)
}
func SayHello(w http.ResponseWriter, req *http.Request) {
fmt.Println(req)
w.Write([]byte("Hello!!!"))
}

golang 装饰器模式

1
2
3
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}

go-restful

一个 Container 包含多个 WebService,同时container实现了http.handler接口定义的ServeHTTP方法

1
2
3
4
5
6
7
8
9
10
11
12
type Container struct {
webServicesLock sync.RWMutex
webServices []*WebService //一个Container包含多个webservice
ServeMux *http.ServeMux
isRegisteredOnRoot bool
containerFilters []FilterFunction
doNotRecover bool // default is true
recoverHandleFunc RecoverHandleFunction
serviceErrorHandleFunc ServiceErrorHandleFunction
router RouteSelector // default is a CurlyRouter (RouterJSR311 is a slower alternative)
contentEncodingEnabled bool // default is false
}
1
2
3
func (c *Container) ServeHTTP(httpwriter http.ResponseWriter, httpRequest *http.Request) {
c.ServeMux.ServeHTTP(httpwriter, httpRequest)
}

一个 webservice 包含多个Route,webservice里定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type WebService struct {
rootPath string
pathExpr *pathExpression
routes []Route //一个webservice包含多个Route
produces []string
consumes []string
pathParameters []*Parameter
filters []FilterFunction
documentation string
apiVersion string

typeNameHandleFunc TypeNameHandleFunction

dynamicRoutes bool

// protects 'routes' if dynamic routes are enabled
routesLock sync.RWMutex
}
1
2
3
4
5
6
7
8
9
10
// Path specifies the root URL template path of the WebService.
// All Routes will be relative to this path.
func (w *WebService) Path(root string) *WebService {
w.rootPath = root
if len(w.rootPath) == 0 {
w.rootPath = "/"
}
w.compilePathExpression()
return w
}
1
2
3
4
5
6
7
8
// Route creates a new Route using the RouteBuilder and add to the ordered list of Routes.
func (w *WebService) Route(builder *RouteBuilder) *WebService {
w.routesLock.Lock()
defer w.routesLock.Unlock()
builder.copyDefaults(w.produces, w.consumes)
w.routes = append(w.routes, builder.Build())
return w
}

一个 Route 包含HTTP 协议协议相关的HTTP Request 、HTTP Reponse 、方法等处理。由RouteBuilder传递过来的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Route struct {
Method string
Produces []string
Consumes []string
Path string // webservice root path + described path
Function RouteFunction//具体处理函数
Filters []FilterFunction
If []RouteSelectionConditionFunction

// cached values for dispatching
relativePath string
pathParts []string
pathExpr *pathExpression // cached compilation of relativePath as RegExp
...
}
1
2
// RouteFunction declares the signature of a function that can be bound to a Route.
type RouteFunction func(*Request, *Response)
1
2
3
4
5
6
// To bind the route to a function.
// If this route is matched with the incoming Http Request then call this function with the *Request,*Response pair. Required.
func (b *RouteBuilder) To(function RouteFunction) *RouteBuilder {
b.function = function
return b
}

1. ResourceMethod and ResourceMethodContainer

2: Apply Builder pattern to reduce clutter

3: Introduce defaults on the Resource and override per method if needed

4: Binding Routes to Functions

5: Document the API

Reference

http://ernestmicklei.com/2012/11/go-restful-api-design/


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!