(define-syntax (routes stx)
  (syntax-parse stx
    [ (routes (element ...) ...)
      (with-syntax ([ name  (format-id stx "axio-routes") ])
        #'(define name (list (route-element element ...) ...)))]))

(define-syntax (route-element stx)
  (syntax-parse stx
    [ (route-element route:string
                     handler:id
                     (~alt (~optional (~or* (~seq #:methods (methods:expr 
...))
                                            (~seq #:method method:expr))
                                      #:name "#:method, or #:methods 
option")
                           (~optional (~seq #:when guard:expr)
                                      #:name "#:when option"))
                     ...)
      (with-syntax ([ method-list (let ([ m  (attribute method)  ]
                                        [ ms (attribute methods) ])
                                    (cond [ m    (list m) ]
                                          [ ms   ms       ]
                                          [ else '()      ]))]
                    [ guard-func (let ([ fun (attribute guard) ])
                                   (if fun
                                       fun
                                       #f)) ])
        #'(list route handler 'method-list guard-func))]))

(routes
 ("/foo" foo-handler #:method put)
 ("/bar" bar-handler #:methods (put update) #:when use-bar-handler?)
 ("/baz" baz-handler))

axio-routes

==>  '(("/foo" #<procedure:foo-handler> (put) #f) ("/bar" 
#<procedure:bar-handler> (put update) #<procedure:use-bar-handler?>) 
("/baz" #<procedure:baz-handler> () #f))

I know it ain't pretty yet, but it works now, so I can enhance it later. I 
should probably use a struct for each route vs. a list.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/ffc7ec25-f663-4d1d-a3d6-d3097d9c3963%40googlegroups.com.

Reply via email to