on d452c09 help sign in
285 lines 11 kB history raw
1---
2next: true
3sort: 7
4title: Web
5---
7# Web
9<div class="row">
10<div class="col-md-8">
12Your Urbit runs its own webserver, `%eyre`, that handles all things HTTP. Here we'll cover the features of `%eyre` that most resemble a traditional web server.
14By default `%eyre` uses Tree, which is sort of like a combination of Jekyll or Kirby and Finder on OSX. Tree is a simple ui for browsing the contents of the filesystem, statically publishing a blog or any kind of content and for running single-page apps.
16We ship `tree` as compiled JavaScript on your Urbit, but the source lives in its own repo [here](https://github.com/urbit/tree).
18</div>
19</div>
21## Quickstart
23Your Urbit runs a web server and can be found at either `http://localhost:8080/` (or `8081` if you're running other processes that might be occupying `8080`, you can determine which port your urbit is on by looking at your boot sequence — it's specified there), or `http://your-urbit.urbit.org/`.
25(Make sure you have a desk mounted to unix, as covered in the [filesystem walkthrough](/filesystem))
27To switch the desk you're serving from:
29 |serve %sandbox
31To host a file on the web try putting the following in `/home/web/test.md` (from Unix):
33 # Hello
35 This is a simple markdown file.
37Create a folder with two more markdown files in it and copy it to `/home/web/test/`. (In urbit, there is no distinction between a director and a file--in this case `test` is both a file and a directory. So, if you're adding files directly from unix, simply `mkdir test` and urbit will treat the file and directory as the same entity.)
39Now modify `/home/web/test.md` to list the children using the `<list/>` JSX:
41 # Hello
43 This is a simple markdown file.
45 <div><list /></div>
47To view your file as raw `md`:
49 http://localhost:8080/test.md
51To view your file as raw `html`:
53 http://localhost:8080/test.html
55## Manual
57## `/web`
59This is the equivalent of a `public_html` directory. Your Urbit looks in this directory to serve content to the world. You can change what desk you're serving from using `|serve`.
61Switch to the `%sandbox` desk:
63 |serve %sandbox
65Let's quickly step through the default contents of `web/`:
67`404.hoon` - The default 404 page.
68`dojo.hoon` - Page body that loads the `dojo` React module.
69`lib/css/bootstrap.css` - The Urbit-flavored bootstrap
70`lib/css/codemirror.css` - Syntax highlighting
71`lib/css/fonts.css` - The Urbit custom fonts
72`lib/js/hoon.js` - Hoon syntax highlighting
73`lib/js/sole.js` - `dojo` operational transformation JS
74`lib/js/urb.js` - Base Urbit JS library. Handles AJAX and polling.
75`static.md` - Sample static file
76`talk/` - Talk compiled JS / CSS
77`talk.hoon` - Page body that loads the `dojo` React module.
78`tree/` - Tree compiled JS / CSS
79`tree.hoon` - Page body that loads the `tree` React module.
81## Tree
83Tree is a simple publishing tool for files that live on your Urbit. You're probably reading this file using Tree right now. The main driving use-case for Tree has been for us to host our own documentation and the surrounding discussion. Tree remains a pretty simple and flexible tool for getting content online. We think it's pretty great.
85Tree is a simple static publishing tool and the basic UI for browsing the filesystem. We take it one step further and make it possible to write modules (tiny little apps), like Talk and Dojo, that can be run directly inside of Tree.
87In the browser Tree is a [React](https://facebook.github.io/react/) / [Flux](https://facebook.github.io/react/blog/2014/05/06/flux.html) app that uses a slightly modifed version of [Bootstrap](http://getbootstrap.com/). On your Urbit Tree is a renderer that translates data in `%clay`, our filesystem, into JSON / JSX that can be read by the client.
89Your Urbit comes with a pre-built copy of the tree (as a single `.js` file). If you're interested in helping to develop tree or learning about how it works, it has its own GitHub repo [here](https://github.com/urbit/tree). We have our own fork of Bootstrap, which lives [here](https://github.com/urbit/bootstrap).
91## Publishing files
93Publishing files in Tree is simple. Try dropping a file in `/web` (as covered in the Quickstart) and navigating to it.
95The best example of more complicated usage of Tree is covered by the Urbit docs themselves. See anything while perusing the docs that you'd like to use yourself? The docs are all on GitHub [here](https://github.com/urbit/docs).
97## YAML
99We use [YAML](http://yaml.org/)-style frontmatter to do odds and ends like override the title. A YAML block looks something like this:
101```
102---
103next: true
104sort: 5
105title: Tree
106---
107```
109and appears at the top of a file. YAML values can't contain newlines but can contain spaces. Here are the fields we support:
111### `title`
113String. Example: `title: Tree manual`.
115This sets the title in the nav or in lists as distinct from the filename.
117### `next`
119Boolean. Example: `next: true`
121Adds a 'next' link at the bottom of the page that links to the next item as decided by the sort order.
123### `sort`
125Integer. Example: `sort: 7`
127User-defined sort. Indicates the sort order as distinct from alphabetical sort. These numbers should not be overlapping.
129### `hide`
131Boolean. Example: `hide: true`
133Whether or not to hide the item from the tree.
135### `date`
137Urbit date. Example: `~2016.2.22`
139Used for sorting posts by date.
141## Using components
143Tree is built using React with a small library of components that you can add to your pages using [JSX](https://facebook.github.io/jsx/) tags.
145If you don't know anything about JSX that's okay: basically we just give you a few custom HTML tags that you can use to create components on the page.
147For those familiar with React / Flux each JSX tag corresponds to a component in [the source](https://github.com/urbit/tree/tree/master/js/components).
149### `<list>`
151Example: `<list dataPath="/posts" titlesOnly="true"></list>`
153Creates a list of children at a path. Defaults to the current path.
155Optional properties:
157- `dataPath`: Path to load kids from.
158- `titlesOnly`: Only show the 'title' which is either the filename or the first h1.
159- `sortBy`: Can be `date` if items have a `date` YAML.
160- `className`: Class to be assigned to the container.
162### `<kids>`
164Example: `<kids dataPath="/posts"></kids>`
166Includes all the children at the specified path separated by `<hr>` tags.
168Optional properties:
170- `dataPath`: Path to load kids from.
171- `className`: Class to be assigned to the container.
173### `<toc>`
175Example: `<toc noHeader="true"></toc>`
177Creates a table of contents from the `h1` - `h6` tags on the page.
179Optional properties:
181- `match`: Tag name to match. If you wanted to only match `h2` tags it could be `match="h2"`
182- `noHeader`: By default we add a 'Table of Contents' header to the top. Remove it with `noHeader="true"`.
184## Implementation
186On the frontend Tree is mostly just a standard Flux app: components generate actions that get dispatched to stores which are picked up by components. If you're not familiar with Flux, [this overview](https://facebook.github.io/flux/docs/overview.html) is a good place to start.
188As a file browser Tree does a simple job: it fetches data and keeps a cache of what it has seen. To do this simple job we do three fancy things: `manx` to JSON encoding, the Async component and Modules.
190### `manx` to JSON format
192Since we want to use React to manage our DOM lifecycle we actually transfer the DOM structure of each page as JSON over the wire, and 'reactify' it on the client side.
194On the Urbit side we actually have a type for html: `++manx`. Our wire format for sending HTML as JSON is really similar to a `++manx`. If you're not interested here's what the json looks like:
196```
197"string" || {gn:"string", ga:{key:"string", [...]}, c:[...]}
198```
200To make this clearer let's look at some HTML and it's JSON representation:
202```
203<div class="blue">
204 <h1>Title</h1>
205</div>
206```
208```
209{gn:"div", ga:{className:"blue"}, [
210 {gn:"h1", ga:null, "Title"}
211]}
212```
214To see the mechanics of how this is handled on the frontend, checkout `components/Reactify.coffee` in the `urbit/tree` repo.
216### Async component
218Each of our components (even those we fetched from the server) can depend on data that may not exist in our stores we wrap our components in a general-purpose Async component. You can find the component in `components/Async.coffee` in the `urbit/tree` repo.
220When we say that we just fetch HTML as JSON it's actually slightly more complicated. When we fetch a node in the filesystem we might not want to fetch its entire contents. Sometimes we just get the title as a part of a list of children, sometimes we might get a snippet (in a list with previews), and sometimes we get the whole thing. On the Urbit side these results are provided by `ren/tree/json`.
222From the perspective of the frontend we just send queries on a path that obey a simple API. To see this in action, here's our `BodyComponent`:
224```
225query {
226 body:'r'
227 name:'t'
228 path:'t'
229 meta:'j'
230 sein:'t'
232```
234Here we're asking for the body, name, path, metadata and parent of the current path. When it loads that data gets passed to our component as props.
236### Module basics
238A module is a special component that can kind of 'take over' the page. It can change properties of the nav bar as well as load custom scripts / css.
240Talk, for example, looks like this:
242```
243<module nav_title="Talk" nav_no-dpad="" nav_no-sibs="" nav_subnav="talk-station">
244 <script src="/~~/~/at/lib/js/urb.js" />
245 <script src="/talk/main.js" />
246 <link href="/talk/main.css" rel="stylesheet" />
247 <talk></talk>
248</module>
249```
251A module will first download all the scripts and styles that it specifies before trying to load any contained components. This means that you can define React components in external scripts and a module can be entirely self contained. The `<talk>` component isn't a part of Tree — it's loaded first before it's executed.
253If you're interested in seeing how a module works you can check out the source for Talk [here](https://github.com/urbit/talk). Dojo also ships on your Urbit, its source is [here](https://github.com/urbit/sole).
255## `urb.js`
257The most basic interface between your browser and your Urbit is `urb.js`. This small library provides the basic utilities for sending and receiving messages from an Urbit.
259### `window.urb.appl`
261Each JSON message sent to your Urbit needs an app endpoint. Can be set as a global `window.urb.appl` or set on a per-request basis in the payload to `send()`.
263### `window.urb.oryx`
265CSRF token. Written to the page by `%eyre`, our webserver, when logged in. Required part of any authenticated request and included by default.
267### `window.urb.req(method,url,params,json,cb)`
269Our basic request wrapper. You can see here how a request needs to be packaged to be properly understood by your Urbit.
271### `window.urb.subscribe({path:"string"})`
273Subscribe to an endpoint. Subscription data is returned through the continuous long poll.
275### `window.urb.unsubscribe({path:"string"})`
277Close subscription on specified path.
279### `window.urb.poll()`
281Mechanics of the continuous long poll. Used mostly internally.
283### `window.urb.util`
285Library of utility and helper functions.