-
Notifications
You must be signed in to change notification settings - Fork 4
/
router.go
55 lines (49 loc) · 2.15 KB
/
router.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
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2020 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package router supplies a router interface ane some implementations.
package router
// Router is a router manager based on the path with the optional method.
type Router interface {
// Range traverses all the registered routes.
Range(func(name, path, method string, handler interface{}))
// Path generates a url path by the path name and parameters.
//
// Return "" if there is not the route path named name.
Path(name string, params ...interface{}) string
// Add adds the route and returns the number of the parameters
// if there are the parameters in the route path.
//
// name is the name of the path, which is optional and must be unique
// if not empty.
//
// If method is empty, handler is the handler of all the methods supported
// by the implementation. Or, it is only that of the given method.
//
// For the parameter in the path, the format is determined by the implementation.
Add(name, path, method string, handler interface{}) (paramNum int, err error)
// Del deletes the given route.
//
// If method is empty, deletes all the routes associated with the path.
// Or, only delete the given method for the path.
Del(path, method string) (err error)
// Match matches the route by path and method, puts the path parameters
// into pnames and pvalues, then returns the handler and the number
// of the path paramethers.
//
// If pnames or pvalues is empty, it will ignore the path paramethers
// when finding the route handler.
//
// Return (nil, 0) if not found the route handler.
Match(path, method string, pnames, pvalues []string) (handler interface{}, pn int)
}