ducky/devices

Paddy 2015-12-14 Child:51ad0db105c8

15:c24a6c5fcd8c Go to Latest

ducky/devices/vendor/code.secondbit.org/trout.hg/route.go

Begin implementation on apiv1. Begin implementing the apiv1 package, which will define the first iteration of our API endpoints and logic. Each API package should be self-contained and able to run without depending on each other. Think of them as interfaces into manipulating the business logic defined in the devices package. The point is to have total control over backwards compatibility, as long as our business logic doesn't change. If that happens, we're in a bad place, but not as bad as it could be. This required us to pull in all our API tools; the api package, its dependencies, the scopeTypes package (so we can define scopes for our API), the trout router, etc. We also updated uuid to the latest, which now includes a license. Hooray? The new apiv1 package consists of a few things: * The devices.go file defines the types the API will use to communicate, along with some helpers to convert from API types to devices types. There's also a stub for validating the device creation requests, which I haven't implemented yet because I'm a pretty bad person. * endpoints.go just contains a helper function that builds our routes and assigns handlers to them, giving us an http.Handler in the returns that we can listen with. * handlers.go defines our HTTP handlers, which will read requests and write responses, after doing the appropriate validation and executing the appropriating business logic. Right now, we only have a handler for creating devices, and it doesn't actually do any validation. Also, we have some user-correctable errors being returned as 500s right now, which is Bad. Fortunately, they're all marked with BUG, so I can at least come back to them. * response.go defines the Response type that will be used for returning information after a request is executed. It may eventually get some helpers, but for now it's pretty basic. * scopes.go defines the Scopes that we're going to be using in the package to control access. It should probably (eventually) include a helper to register the Scopes, or we should have a collector service that pulls in all the packages, finds all their Scopes, and registers them. I haven't decided how I want to manage Scope registration just yet. We exported the getStorer function (now GetStorer) so other packages can use it. I'm not sure how I feel about this just yet. We also had to create a WithStorer helper method that embeds the Storer into a context.Context, so we can bootstrap in devicesd. We erroneously had Created in the DeviceChange struct, but there's no reason the Created property of a Device should ever change, so it was removed from the logic, from the struct, and from the tests. Our CreateMany helper was erroneously creating the un-modified Devices that were passed in, instead of the Devices that had sensible defaults filled. We created a _very minimal_ (e.g., needs some work before it's ready for production) devicesd package that will spin up a simple server, just so we could take a peek at our apiv1 endpoints as they'd actually be used. (It worked. Yay?) We should continue to expand on this with configuration, more information being logged, etc.

History
paddy@15 1 package trout
paddy@15 2
paddy@15 3 import (
paddy@15 4 "net/http"
paddy@15 5 "strconv"
paddy@15 6 "strings"
paddy@15 7 "time"
paddy@15 8 )
paddy@15 9
paddy@15 10 var (
paddy@15 11 default404Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paddy@15 12 w.WriteHeader(http.StatusNotFound)
paddy@15 13 w.Write([]byte("404 Not Found"))
paddy@15 14 return
paddy@15 15 }))
paddy@15 16 default405Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paddy@15 17 w.WriteHeader(http.StatusMethodNotAllowed)
paddy@15 18 w.Write([]byte("405 Method Not Allowed"))
paddy@15 19 return
paddy@15 20 }))
paddy@15 21 )
paddy@15 22
paddy@15 23 // RequestVars returns easy-to-access mappings of parameters to values for URL templates. Any {parameter} in
paddy@15 24 // your URL template will be available in the returned Header as a slice of strings, one for each instance of
paddy@15 25 // the {parameter}. In the case of a parameter name being used more than once in the same URL template, the
paddy@15 26 // values will be in the slice in the order they appeared in the template.
paddy@15 27 //
paddy@15 28 // Values can easily be accessed by using the .Get() method of the returned Header, though to access multiple
paddy@15 29 // values, they must be accessed through the map. All parameters use http.CanonicalHeaderKey for their formatting.
paddy@15 30 // When using .Get(), the parameter name will be transformed automatically. When utilising the Header as a map,
paddy@15 31 // the parameter name needs to have http.CanonicalHeaderKey applied manually.
paddy@15 32 func RequestVars(r *http.Request) http.Header {
paddy@15 33 res := http.Header{}
paddy@15 34 for h, v := range r.Header {
paddy@15 35 stripped := strings.TrimPrefix(h, http.CanonicalHeaderKey("Trout-Param-"))
paddy@15 36 if stripped != h {
paddy@15 37 res[stripped] = v
paddy@15 38 }
paddy@15 39 }
paddy@15 40 return res
paddy@15 41 }
paddy@15 42
paddy@15 43 // Router defines a set of Endpoints that map requests to the http.Handlers. The http.Handler assigned to
paddy@15 44 // Handle404, if set, will be called when no Endpoint matches the current request. The http.Handler assigned
paddy@15 45 // to Handle405, if set, will be called when an Endpoint matches the current request, but has no http.Handler
paddy@15 46 // set for the HTTP method that the request used. Should either of these properties be unset, a default
paddy@15 47 // http.Handler will be used.
paddy@15 48 //
paddy@15 49 // The Router type is safe for use with empty values, but makes no attempt at concurrency-safety in adding
paddy@15 50 // Endpoints or in setting properties. It should also be noted that the adding Endpoints while simultaneously
paddy@15 51 // routing requests will lead to undefined and (almost certainly) undesirable behaviour. Routers are intended
paddy@15 52 // to be initialised with a set of Endpoints, and then start serving requests. Using them outside of this use
paddy@15 53 // case is unsupported.
paddy@15 54 type Router struct {
paddy@15 55 t *trie
paddy@15 56 Handle404 http.Handler
paddy@15 57 Handle405 http.Handler
paddy@15 58 }
paddy@15 59
paddy@15 60 func (router *Router) serve404(w http.ResponseWriter, r *http.Request, t time.Time) {
paddy@15 61 h := default404Handler
paddy@15 62 if router.Handle404 != nil {
paddy@15 63 h = router.Handle404
paddy@15 64 }
paddy@15 65 r.Header.Set("Trout-Timer", strconv.FormatInt(time.Now().Sub(t).Nanoseconds(), 10))
paddy@15 66 h.ServeHTTP(w, r)
paddy@15 67 }
paddy@15 68
paddy@15 69 func (router *Router) serve405(w http.ResponseWriter, r *http.Request, t time.Time) {
paddy@15 70 h := default405Handler
paddy@15 71 if router.Handle405 != nil {
paddy@15 72 h = router.Handle405
paddy@15 73 }
paddy@15 74 r.Header.Set("Trout-Timer", strconv.FormatInt(time.Now().Sub(t).Nanoseconds(), 10))
paddy@15 75 h.ServeHTTP(w, r)
paddy@15 76 }
paddy@15 77
paddy@15 78 func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
paddy@15 79 start := time.Now()
paddy@15 80 if router.t == nil {
paddy@15 81 router.serve404(w, r, start)
paddy@15 82 return
paddy@15 83 }
paddy@15 84 pieces := strings.Split(strings.ToLower(strings.Trim(r.URL.Path, "/")), "/")
paddy@15 85 router.t.RLock()
paddy@15 86 defer router.t.RUnlock()
paddy@15 87 branches := make([]*branch, len(pieces))
paddy@15 88 path, ok := router.t.match(pieces)
paddy@15 89 if !ok {
paddy@15 90 router.serve404(w, r, start)
paddy@15 91 return
paddy@15 92 }
paddy@15 93 b := router.t.branch
paddy@15 94 for i, pos := range path {
paddy@15 95 b = b.children[pos]
paddy@15 96 branches[i] = b
paddy@15 97 }
paddy@15 98 v := vars(branches, pieces)
paddy@15 99 for key, vals := range v {
paddy@15 100 r.Header[http.CanonicalHeaderKey("Trout-Param-"+key)] = vals
paddy@15 101 }
paddy@15 102 ms := make([]string, len(b.methods))
paddy@15 103 i := 0
paddy@15 104 for m := range b.methods {
paddy@15 105 ms[i] = m
paddy@15 106 i = i + 1
paddy@15 107 }
paddy@15 108 r.Header[http.CanonicalHeaderKey("Trout-Methods")] = ms
paddy@15 109 h := b.methods[r.Method]
paddy@15 110 if h == nil {
paddy@15 111 router.serve405(w, r, start)
paddy@15 112 return
paddy@15 113 }
paddy@15 114 r.Header.Set("Trout-Timer", strconv.FormatInt(time.Now().Sub(start).Nanoseconds(), 10))
paddy@15 115 h.ServeHTTP(w, r)
paddy@15 116 }
paddy@15 117
paddy@15 118 // Endpoint defines a new Endpoint on the Router. The Endpoint should be a URL template, using curly braces
paddy@15 119 // to denote parameters that should be filled at runtime. For example, `{id}` denotes a parameter named `id`
paddy@15 120 // that should be filled with whatever the request has in that space.
paddy@15 121 //
paddy@15 122 // Parameters are always `/`-separated strings. There is no support for regular expressions or other limitations
paddy@15 123 // on what may be in those strings. A parameter is simply defined as "whatever is between these two / characters".
paddy@15 124 func (router *Router) Endpoint(e string) *Endpoint {
paddy@15 125 e = strings.Trim(e, "/")
paddy@15 126 e = strings.ToLower(e)
paddy@15 127 pieces := strings.Split(e, "/")
paddy@15 128 if router.t == nil {
paddy@15 129 router.t = &trie{}
paddy@15 130 }
paddy@15 131 router.t.Lock()
paddy@15 132 defer router.t.Unlock()
paddy@15 133 if router.t.branch == nil {
paddy@15 134 router.t.branch = &branch{
paddy@15 135 parent: nil,
paddy@15 136 children: []*branch{},
paddy@15 137 key: "",
paddy@15 138 isParam: false,
paddy@15 139 methods: map[string]http.Handler{},
paddy@15 140 }
paddy@15 141 }
paddy@15 142 closest := findClosestLeaf(pieces, router.t.branch)
paddy@15 143 b := router.t.branch
paddy@15 144 for _, pos := range closest {
paddy@15 145 b = b.children[pos]
paddy@15 146 }
paddy@15 147 if len(closest) == len(pieces) {
paddy@15 148 return (*Endpoint)(b)
paddy@15 149 }
paddy@15 150 offset := len(closest)
paddy@15 151 for i := offset; i < len(pieces); i++ {
paddy@15 152 piece := pieces[i]
paddy@15 153 var isParam bool
paddy@15 154 if len(piece) > 0 && piece[0:1] == "{" && piece[len(piece)-1:] == "}" {
paddy@15 155 isParam = true
paddy@15 156 piece = piece[1 : len(piece)-1]
paddy@15 157 }
paddy@15 158 b = b.addChild(piece, isParam)
paddy@15 159 }
paddy@15 160 return (*Endpoint)(b)
paddy@15 161 }
paddy@15 162
paddy@15 163 func vars(path []*branch, pieces []string) map[string][]string {
paddy@15 164 v := map[string][]string{}
paddy@15 165 for pos, p := range path {
paddy@15 166 if !p.isParam {
paddy@15 167 continue
paddy@15 168 }
paddy@15 169 _, ok := v[p.key]
paddy@15 170 if !ok {
paddy@15 171 v[p.key] = []string{pieces[pos]}
paddy@15 172 continue
paddy@15 173 }
paddy@15 174 v[p.key] = append(v[p.key], pieces[pos])
paddy@15 175 }
paddy@15 176 return v
paddy@15 177 }
paddy@15 178
paddy@15 179 func findClosestLeaf(pieces []string, b *branch) []int {
paddy@15 180 offset := 0
paddy@15 181 path := []int{}
paddy@15 182 longest := []int{}
paddy@15 183 num := len(pieces)
paddy@15 184 for i := 0; i < num; i++ {
paddy@15 185 piece := pieces[i]
paddy@15 186 var isParam bool
paddy@15 187 if len(piece) > 0 && piece[0:1] == "{" && piece[len(piece)-1:] == "}" {
paddy@15 188 isParam = true
paddy@15 189 piece = piece[1 : len(piece)-1]
paddy@15 190 }
paddy@15 191 offset = pickNextRoute(b, offset, piece, isParam)
paddy@15 192 if offset == -1 {
paddy@15 193 if len(path) == 0 {
paddy@15 194 // exhausted our options, bail
paddy@15 195 break
paddy@15 196 }
paddy@15 197 // no match, maybe save this and backup
paddy@15 198 if len(path) > len(longest) {
paddy@15 199 longest = append([]int{}, path...) // copy them over so they don't get modified
paddy@15 200 }
paddy@15 201 path, offset = backup(path)
paddy@15 202 offset = offset + 1
paddy@15 203 b = b.parent
paddy@15 204 i = i - 2
paddy@15 205 } else {
paddy@15 206 path = append(path, offset)
paddy@15 207 b = b.children[offset]
paddy@15 208 offset = 0
paddy@15 209 }
paddy@15 210 }
paddy@15 211 if len(longest) < len(path) {
paddy@15 212 longest = append([]int{}, path...)
paddy@15 213 }
paddy@15 214 return longest
paddy@15 215 }
paddy@15 216
paddy@15 217 func pickNextRoute(b *branch, offset int, input string, variable bool) int {
paddy@15 218 count := len(b.children)
paddy@15 219 for i := offset; i < count; i++ {
paddy@15 220 if b.children[i].key == input && b.isParam == variable {
paddy@15 221 return i
paddy@15 222 }
paddy@15 223 }
paddy@15 224 return -1
paddy@15 225 }
paddy@15 226
paddy@15 227 // Endpoint defines a single URL template that requests can be matched against. It uses
paddy@15 228 // URL parameters to accept variables in the URL structure and make them available to
paddy@15 229 // the Handlers associated with the Endpoint.
paddy@15 230 type Endpoint branch
paddy@15 231
paddy@15 232 // Handler associates the passed http.Handler with the Endpoint. This http.Handler will be
paddy@15 233 // used for all requests, regardless of the HTTP method they are using, unless overridden by
paddy@15 234 // the Methods method. Endpoints without a http.Handler associated with them will not be
paddy@15 235 // considered matches for requests, unless the request was made using an HTTP method that the
paddy@15 236 // Endpoint has an http.Handler mapped to.
paddy@15 237 func (e *Endpoint) Handler(h http.Handler) {
paddy@15 238 (*branch)(e).setHandler("", h)
paddy@15 239 }
paddy@15 240
paddy@15 241 // Methods simple returns a Methods object that will enable the mapping of the passed HTTP
paddy@15 242 // request methods to a Methods object. On its own, this function does not modify anything. It
paddy@15 243 // should, instead, be used as a friendly shorthand to get to the Methods.Handler method.
paddy@15 244 func (e *Endpoint) Methods(m ...string) Methods {
paddy@15 245 return Methods{
paddy@15 246 e: e,
paddy@15 247 m: m,
paddy@15 248 }
paddy@15 249 }
paddy@15 250
paddy@15 251 // Methods defines a pairing of an Endpoint to the HTTP request methods that should be mapped to
paddy@15 252 // specific http.Handlers. Its sole purpose is to enable the Methods.Handler method.
paddy@15 253 type Methods struct {
paddy@15 254 e *Endpoint
paddy@15 255 m []string
paddy@15 256 }
paddy@15 257
paddy@15 258 // Handler maps a Methods object to a specific http.Handler. This overrides the http.Handler
paddy@15 259 // associated with the Endpoint to only handle specific HTTP method(s).
paddy@15 260 func (m Methods) Handler(h http.Handler) {
paddy@15 261 b := (*branch)(m.e)
paddy@15 262 for _, method := range m.m {
paddy@15 263 b.setHandler(method, h)
paddy@15 264 }
paddy@15 265 }