about summary refs log tree commit diff
path: root/dispatcher.go
blob: b40b556f858167be6dc7ac0e008871b2fdb07f22 (plain) (blame)
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
package mooh

type Dispatcher struct {
	Routes      []Route
	RouteAccess map[string]*Route
}

func BuildDispatcher() Dispatcher {
	router := Dispatcher{}
	router.RouteAccess = map[string]*Route{}
	return router
}

func (self *Dispatcher) GetRoute(path string) *Route {
	r := self.RouteAccess[path]

	if r != nil {
		return r
	}

	return nil
}

func (self *Dispatcher) AddRoute(path string, method string, code func(*Request) (Response, error)) {
	r := self.GetRoute(path)

	if r == nil {
		route := MakeRoute(path, method, code)
		self.Routes = append(self.Routes, route)
		self.RouteAccess[path] = &route
	} else {
		r.Executors[method] = code
	}
}

func (self *Dispatcher) Match(request Request) *Route {
	for _, r := range self.Routes {
		match, route := r.Match(request)
		if match == true {
			return route
		}
	}
	return nil
}

// func (*dispatcher) ListRoutes() {
// }

// func (*dispatcher) AddRoutes() {
// }

// func (self *dispatcher) UriFor() {
// }