aboutsummaryrefslogtreecommitdiff
path: root/core.go
blob: 29dcfcb4eb73818e94edd672f84ea0fc52a9e339 (plain)
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
package sleepy

type Resource interface {
    Get() string
    // Post
    // Put 
    // Delete
}

type Route struct {
    resource Resource
    path string
}

func (route *Route) pathMatch(path string) bool {
    return route.path == path
}

type Api struct {
    routes []Route
}

func (api *Api) matchResource(path string) Resource {
    for _, route := range api.routes {
        if route.pathMatch(path) {
            return route.resource
        }
    }
    return nil
}

func (api *Api) HandleRequest(path string) string {
    resource := api.matchResource(path)
    return resource.Get()
}


func (api *Api) AddResource(resource Resource, path string) {
    api.routes = append(api.routes, Route{resource, path})
}
bgstack15