/docs/tutorial/page-1.md

http://github.com/jacksonh/manos · Markdown · 129 lines · 80 code · 49 blank · 0 comment · 0 complexity · 825924dd139b36cb65973cc29ded4ef5 MD5 · raw file

  1. Getting Started with Manos
  2. ==========================
  3. This tutorial will get you writing your first Manos apps in no time.
  4. We assume you already have Manos installed on your machine. If you don't check out
  5. the Installation Guide. To ensure you have Manos running try typing the manos --help
  6. command in a terminal.
  7. manos --help
  8. For this tutorial we are going to create a simple URL shortening service. It will allow
  9. users to submit links for shortening and will also keep track of how many times a shortened
  10. link has been clicked.
  11. Create a Manos Project
  12. -------------------------------
  13. To create a Manos application all you need to do is run the following command:
  14. manos --init <YourApplicationName>
  15. Make sure you don't have any spaces in your application name. An app name should just be letters,
  16. digits and underscores.
  17. We'll call our app Shorty;
  18. manos --init Shorty
  19. This will create a folder named Shorty with a few files and subfolders:
  20. Shorty/
  21. Shorty/Content/css/handheld.css
  22. Shorty/Content/css/style.css
  23. Shorty/Content/js/plugins.js
  24. Shorty/Content/js/dd_belatedpng.js
  25. Shorty/Content/js/jquery-1.4.2.min.js
  26. Shorty/Content/js/profiling
  27. Shorty/Content/js/profiling/charts.swf
  28. Shorty/Content/js/profiling/config.js
  29. Shorty/Content/js/profiling/yahoo-profiling.css
  30. Shorty/Content/js/profiling/yahoo-profiling.min.js
  31. Shorty/Content/js/script.js
  32. Shorty/Content/js/modernizr-1.5.min.js
  33. Shorty/Shorty.cs
  34. Shorty/StaticContentModule.cs
  35. We'll worry more about these files later in the tutorial, but for now here's a
  36. quick overview of what we've got:
  37. * **Content/**: Static content like images, js and css goes here. The supplied files are based on the HTML5 Boilerplate project.
  38. * **Shorty.cs**: This is the main entry point for our app. Its the first thing that will be loaded by Manos.
  39. * **StaticContentModule.cs**: The static content module is in charge of handling static content, things like css files and images.
  40. If you are hosting your static content on a separate server you can remove this module.
  41. Exploring Shorty.cs
  42. --------------------------
  43. The first file we are going to deal with is Shorty.cs. This file is the main entry point into our application and is where we
  44. handle our top level routing. Routes can be added to our app in the constructor or by using method attributes. If you look at Shorty.cs
  45. you'll see there has already been a route to the StaticContentModule added for you:
  46. Route ("/Content/", new StaticContentModule ());
  47. The Route method will route all HTTP calls that match the supplied pattern, in this case "/Content/" to the supplied module or action. Here we
  48. have routed every url request that starts with "/Content/" to the StaticContentModule.
  49. If we just want to handle certain types of HTTP method requests we can use the corresponding Manos routing method. So if we just want to route
  50. HTTP GET requests to the StaticContentModule, we could change the above code to look like this:
  51. Get ("/Content/", new StaticContentModule ());
  52. We'll talk more about routes later.
  53. Hello, Manos World
  54. ------------------
  55. Routes don't necessarily have to go to modules, we can also route them to actions. An action is any function that accepts a single IManosContext
  56. parameter. So lets create our first action, add this line right above the static content route.
  57. Get ("/", ctx => ctx.Response.End ("Hello, Manos World!"));
  58. What we've done here is created a route using the "/" path.
  59. Instead of creating an action method to handle the request we've just used a simple lambda expression that takes the IManosContext and sends
  60. "Hello, Manos World!" as the response.
  61. Routes can be created with simple paths like this or we could create more complex
  62. routes with regular expressions. Something like Get ("/d.d", ...) would route "/dad" and "/dfd". There is also support for simple pattern matching
  63. on routes, so things like Get ("/Article/{name}", ...).
  64. Note that we called a method named End to write our data. End is overloaded with all the same arguments as Write so you don't have to do things like:
  65. response.Write (<some data>);
  66. response.End ();
  67. All Manos actions must have End called when they are finished. End is not called automatically because many scenarios require the Stream to live longer than the action method. Think of Actions as a message from the browser saying 'please send me some data', not necessarily a request and response lifecycle. In comet style applications the response will live much longer than the Action method.
  68. Building and Running Manos Apps
  69. -------------------------------
  70. To test out your application first you'll need to build it. You can build the app with the manos build command.
  71. If you aren't already in the Shorty directory:
  72. cd Shorty/
  73. and now you can build:
  74. manos --build
  75. To run this app all we'll use the manos --server command. This command loads the local compiled manos app
  76. and sets up hosting for it.
  77. manos --server
  78. Once the application is running you can check it out by navigating your browser to http://localhost:8080/
  79. ---
  80. Continue the tutorial in [part two](./page-2.md).