Arc Forumnew | comments | leaders | submitlogin
Passing a string to defop for the url?
2 points by bh 3636 days ago | 3 comments
I recently started using Arc for all my programs. (I'm never going back to any other language!) So I took the helloworld code for web apps:

  (defop hello req
    (pr "hello world"))
Now say I have a string url:

  (let url "hello"
    (defop url req
      (pr "hello world")))
But this just defines /url instead. How can I use url to define /hello?


2 points by akkartik 3636 days ago | link

defop is a macro that doesn't evaluate its first arg. So your question is similar to asking why this doesn't bind y to 34:

  (let x 'y
    (= x 34))
To do what you want you'll need to define the url directly. The definition of defop (approximately, after dropping some indirections) is:

  (mac defop (name parms . body)
    `(= (srvops* ',name)
        (fn ,parms
          ,@body)))
So you can do:

  (let url "hello"
    (= (srvops* sym.url)
       (fn (output req)
         (w/stdout output
           (prrn)
           (pr "hello world")))))
There's a few extra details here. You have to work through the details of how defop is defined. Feel free to ask more questions if something is unclear.

-----

2 points by bh 3636 days ago | link

Thanks, I got it. Also, is there a way to catch all requests?

-----

1 point by akkartik 3636 days ago | link

Can you elaborate? You want to call the same function on any request, and pass in the url from the request? Are you using arc 3.1 or anarki?

Edit: on anarki you get this behavior by replacing the function respond in lib/srv.arc with this code:

  (def respond (out req)
    (w/stdout out
      (prrn "HTTP/1.1 200 OK")
      (prrn "Content-Type: text/html; charset=utf-8")
      (prrn "Connection: close")
      (prrn)
      (wildcard-handler req)))

  (def wildcard-handler (req)
    (prn req!op))
This will display whatever op you request.

-----