<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: urweb</title>
 <link href="http://bluishcoder.co.nz/tag/urweb/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2020-07-10T16:25:05+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Handling POST requests with Ur/Web</title>
   <link href="http://bluishcoder.co.nz/2010/12/16/handling-post-requests-with-urweb.html"/>
   <updated>2010-12-16T18:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2010/12/16/handling-post-requests-with-urweb</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://impredicative.com/ur/&quot;&gt;Ur/Web&lt;/a&gt; makes it easy to write web applications that have HTML forms and handle the posted data. It gets a bit trickier if you have a POST handler that does not have an HTML form in the web application but is instead called by an external service.&lt;/p&gt;

&lt;p&gt;The following is a function that handles a POST request field containing a string. It puts that string in a database:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;table messages : { Message : string }

fun notifyMessage data =
  dml (INSERT INTO messages (Message) VALUES ({[data.Message]}));
  return &amp;lt;xml&amp;gt;&amp;lt;/xml&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This &lt;code&gt;notifyMessage&lt;/code&gt; function won&#39;t type check unless we have a corresponding function that has an HTML form that submits to it. Without an HTML form in the application that uses &lt;code&gt;notifyMessage&lt;/code&gt; we get a compile error like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;post.ur:4:46-4:62: Couldn&#39;t prove field name disjointness
   Con 1:  [#Message = Basis.string]
   Con 2:  &amp;lt;UNIF:T::{Type}&amp;gt;
Hnormed 1:  [#Message = Basis.string]
Hnormed 2:  &amp;lt;UNIF:T::{Type}&amp;gt;
You may be using a disallowed attribute with an HTML tag.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The error is pointing to the fact that it can&#39;t work out the type of &lt;code&gt;data.Message&lt;/code&gt;. The fix is to ensure we have an HTML form in the application with an action that submits to &lt;code&gt;notifyMessage&lt;/code&gt;. Since this particular POST handler is being used by an external caller we need to create a dummy page with a form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun dummyForm () =
  return &amp;lt;xml&amp;gt;
    &amp;lt;body&amp;gt;
      &amp;lt;form&amp;gt;
        &amp;lt;textbox{#Message}/&amp;gt;
        &amp;lt;submit action={notifyMessage}/&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/body&amp;gt;
  &amp;lt;/xml&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This enables the Ur/Web type checker to know that &lt;code&gt;notifyMessage&lt;/code&gt; has POST data containing a &#39;message&#39; field. &lt;code&gt;dummyForm&lt;/code&gt; needs to be added to the &lt;code&gt;.urs&lt;/code&gt; file containing the exported function signatures:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val dummyForm : unit -&amp;gt; transaction page
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&#39;ve created an example application to test this in &lt;a href=&quot;http://github.com/doublec/urweb-post-example&quot;&gt;github&lt;/a&gt;. The &lt;code&gt;main&lt;/code&gt; function for the application displays instructions on how to POST using &lt;code&gt;curl&lt;/code&gt; and displays the messages currently stored in the database:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun main () =
  rows &amp;lt;- listMessages ();
  return &amp;lt;xml&amp;gt;&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Post Example&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;p&amp;gt;Send a curl command like the following to add data
       to the messages table and refresh the page:&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;curl -F &quot;message=Hello&quot; http://127.0.0.1:8080/notifyMessage&amp;lt;/p&amp;gt;
    {rows}
  &amp;lt;/body&amp;gt;
&amp;lt;/xml&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It uses a &lt;code&gt;listMessages&lt;/code&gt; helper function to do a SQL query on the table and outputs an HTML table row for each result in the query:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun listMessages () =
  rows &amp;lt;- queryX (SELECT * FROM messages)
         (fn row =&amp;gt; &amp;lt;xml&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;{[row.Messages.Message]}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/xml&amp;gt;);
  return &amp;lt;xml&amp;gt;&amp;lt;table&amp;gt;{rows}&amp;lt;/table&amp;gt;&amp;lt;/xml&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To build, run and send post data using &lt;code&gt;curl&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/doublec/urweb-post-example
$ cd urweb-post-example
$ make
$ ./post.exe
$ curl -F &quot;message=hello&quot; http://127.0.0.1:8080/notifyMessage
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To display posted messages go to http://localhost:8080/main in a web browser.&lt;/p&gt;

&lt;p&gt;The downside with this workaround is we end up with a &lt;code&gt;dummyForm&lt;/code&gt; page that is publically explosed. This could be prevented from being served by putting the web application behind a reverse proxy which doesn&#39;t expose it. This issue and workaround is a result from a &lt;a href=&quot;http://www.impredicative.com/pipermail/ur/2010-December/000422.html&quot;&gt;discussion on the mailing list&lt;/a&gt; about POST.&lt;/p&gt;

&lt;p&gt;Another issue that popped up during that discussion was that Ur/Web can only handle POSTing of HTML form data. If the Content-Type of the data is something else (like &lt;code&gt;application/json&lt;/code&gt;) then it can&#39;t be handled with the current release of Ur/Web without going through some form of intermediate proxy to convert the data to what Ur/Web expects.&lt;/p&gt;

&lt;p&gt;Hopefully these can be resolved in a later Ur/Web release.&lt;/p&gt;

&lt;p&gt;Update 2010-12-19: The issues above look to be solved now according to the &lt;a href=&quot;http://www.impredicative.com/mantis/view.php?id=38&quot;&gt;Ur/Web bug tracker&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Simple Ur/Web Example</title>
   <link href="http://bluishcoder.co.nz/2010/12/14/simple-urweb-example.html"/>
   <updated>2010-12-14T10:30:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2010/12/14/simple-urweb-example</id>
   <content type="html">&lt;p&gt;I&#39;ve been admiring &lt;a href=&quot;http://www.impredicative.com/ur/&quot;&gt;Ur/Web&lt;/a&gt; from afar for a while now and I&#39;ve decided to dive into it and try it out on a project.&lt;/p&gt;

&lt;p&gt;Ur/Web is described at the website as:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Ur is a programming language in the tradition of ML and Haskell, but featuring a significantly richer type system. Ur is functional, pure, statically-typed, and strict. Ur supports a powerful kind of metaprogramming based on row types.&lt;/p&gt;

&lt;p&gt;Ur/Web is Ur plus a special standard library and associated rules for parsing and optimization. Ur/Web supports construction of dynamic web applications backed by SQL databases. The signature of the standard library is such that well-typed Ur/Web programs &quot;don&#39;t go wrong&quot; in a very broad sense.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Basically it&#39;s a programming language and system for building web applications with a rich type system to prevent common web programming errors from occuring.&lt;/p&gt;

&lt;p&gt;The compiled Ur/Web applications do not use a garbage collector and are supposed to be very efficient in RAM usage. Ur/Web can also be used for some client side programming and allows writing code that uses threads in the web browser, functional reactive programming for client side updates and RPC calls between the browser and server as well as from the server to the browser. Ur/Web has a good &lt;a href=&quot;http://www.impredicative.com/ur/manual.pdf&quot;&gt;reference manual&lt;/a&gt; and an excellent online &lt;a href=&quot;http://www.impredicative.com/ur/demo/&quot;&gt;demo/tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the project I want to write I need to be able to spawn command line processes and get the output. Marc Weber has written a library for Ur/Web that does this called &lt;a href=&quot;http://gitorious.org/some-urweb-utility-libraries/uw-process&quot;&gt;uw-process&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I started with a simple example program to get a taste of Ur/Web that uses &lt;code&gt;uw-process&lt;/code&gt;. The application displays a single page showing the current time on the server as returned by &lt;code&gt;date&lt;/code&gt;. This updates the time every second using Ur/Web&#39;s functional reactive programming system.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/doublec/urweb-clock/blob/master/clock.ur&quot;&gt;clock.ur&lt;/a&gt; file implements the application. The &lt;code&gt;getTime&lt;/code&gt; function uses &lt;code&gt;uw-process&lt;/code&gt; to call &lt;code&gt;date&lt;/code&gt; and return the standard output of that as a string:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun getTime () =
  result &amp;lt;- Process.exec &quot;date&quot; (textBlob &quot;&quot;) 100;
  return (Process.blobText (Process.blob (result)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Process.exe&lt;/code&gt; takes three arguments. The command to run (&quot;date&quot; in this case), a text blob containing the standard input to be used by the command (an empty string here) and the maximum length of data to take from the standard output.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;main&lt;/code&gt; function is what will be executed when the web application is run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun main () =
  s &amp;lt;- source &quot;&quot; ;
  return &amp;lt;xml&amp;gt;&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Current Server Time&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;

    &amp;lt;body onload={spawn (timeLoop s)}&amp;gt;
      &amp;lt;dyn signal={t &amp;lt;- signal s; return &amp;lt;xml&amp;gt;{[t]}&amp;lt;/xml&amp;gt;}/&amp;gt;
    &amp;lt;/body&amp;gt;
  &amp;lt;/xml&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This uses functional reactive programming on the client to continously call the server asking for the time. We use Ur/Web&#39;s support for embedded XML to construct the result page:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;return &amp;lt;xml&amp;gt;&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;Current Server Time&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body onload={spawn (timeLoop s)}&amp;gt;
    &amp;lt;dyn signal={t &amp;lt;- signal s; return &amp;lt;xml&amp;gt;{[t]}&amp;lt;/xml&amp;gt;}/&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/xml&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;onload&#39; handler for the body of the page has this syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{spawn (timeLoop s)}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;spawn&lt;/code&gt; is used to spawn a thread in the browser. Here we spawn a thread to run the &#39;timeLoop&#39; function passing it a source, &lt;code&gt;s&lt;/code&gt;. This is compiled into JavaScript by Ur/Web. The &lt;code&gt;timeLoop&lt;/code&gt; function written in Ur/Web is also compiled to JavaScript:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun timeLoop s =
  t &amp;lt;- rpc (getTime ());
  set s t;
  sleep 1000;
  timeLoop s
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;timeLoop&lt;/code&gt; does a remote procedure call to the server executing the &lt;code&gt;getTime&lt;/code&gt; function shown previously. The source &lt;code&gt;s&lt;/code&gt; is set to the result and then the JavaScript thread sleeps for one second. A tail call is made to loop back to calling timeLoop again when the second is up.&lt;/p&gt;

&lt;p&gt;As this function is called from a JavaScript handler (&lt;code&gt;onload&lt;/code&gt;), it&#39;s compiled into JavaScript to run on the client side. The RPC call is converted into an HTTP request to the server to call the &lt;code&gt;getTime&lt;/code&gt; function and return the result.&lt;/p&gt;

&lt;p&gt;In the body of the HTML we use the source &lt;code&gt;s&lt;/code&gt; in a way to display the time value so that it is automatically updated whenever it changes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;dyn signal={t &amp;lt;- signal s; return &amp;lt;xml&amp;gt;{[t]}&amp;lt;/xml&amp;gt;}/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A source represents a dynamically changing value in the page. When the source changes the page changes automatically. The part that changes is the &lt;code&gt;dyn&lt;/code&gt; element. The &lt;code&gt;signal&lt;/code&gt; attribute of that element has Ur/Web code to call &lt;code&gt;signal&lt;/code&gt; on the source. This retrieves the value that was changed (the time in our example) and we return an XML fragment containing that value. The syntax &lt;code&gt;{[t]}&lt;/code&gt; coerces the value of &lt;code&gt;t&lt;/code&gt; to something safe to be displayed in XML, taking into account cross site scripting issues, HTML encoding, etc.&lt;/p&gt;

&lt;p&gt;The remaining Ur/Web files are &lt;a href=&quot;https://github.com/doublec/urweb-clock/blob/master/clock.urs&quot;&gt;clock.urs&lt;/a&gt; which contains the type signature for the &lt;code&gt;main&lt;/code&gt; function exposed by our application, and &lt;a href=&quot;https://github.com/doublec/urweb-clock/blob/master/clock.urp&quot;&gt;clock.urp&lt;/a&gt; which is the project file.&lt;/p&gt;

&lt;p&gt;To build the example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ urweb clock
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This produces a &lt;code&gt;clock.exe&lt;/code&gt; (a strange name for the resulting file given we&#39;re on a Linux system...). Running this file will start a webserver on port 8080 that runs our application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./clock.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Visiting http://localhost:8080/main will display the server time, updated every second, in the browser.&lt;/p&gt;

&lt;p&gt;This is obviously a pretty trivial example but does show how easy it is to do threads in the browser and RPC calls. The &lt;a href=&quot;http://www.impredicative.com/ur/demo/&quot;&gt;Ur/Web Demo&#39;s&lt;/a&gt; show much more including database usage and various uses of the type system.&lt;/p&gt;

&lt;p&gt;I&#39;ve put this example in &lt;a href=&quot;http://github.com/doublec/urweb-clock&quot;&gt;github&lt;/a&gt;. It can be cloned, built and run with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/doublec/urweb-clock
$ cd urweb-clock
$ git submodule init
$ git submodule update
$ make
$ ./clock.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I include &lt;code&gt;uw-process&lt;/code&gt; as a git submodule and the makefile assumes Ur/Web is installed in &lt;code&gt;/usr/local&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I haven&#39;t written anything about the type system or other features of Ur/Web that make it interesting yet but I hope to look at some of these in later posts.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
