<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Bye Merb Welcome Rack"]]></title>
		<link>http://blogs.mentaframework.org/posts/list/2.page</link>
		<description><![CDATA[Latest messages posted in the topic "Bye Merb Welcome Rack"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Bye Merb Welcome Rack</title>
				<description><![CDATA[ The answer to my prayers has just arrived. It looks like a miracle, but it is actually a [i]Rack[/i] (not a hack).<br /> <br /> Welcome to [url="http://rack.rubyforge.org/"]Rack[/url], a foundation framework that makes it easy to write your own web frameworks in Ruby. You can think of it as a Java Servlet API, at least the basic request and response paradigm.<br /> <br /> Rack comes with [i]handlers[/i] for the most common Ruby web servers, such as [url="http://mongrel.rubyforge.org/"]Mongrel[/url] and WEBrick. For sanity reasons we will stick to Mongrel.<br /> <br /> So I have given up Merb and welcome Mongrel + Rack + [url="http://www.kuwata-lab.com/erubis/"]Erubis[/url]. Erubis is a fast template engine for Ruby that we are going to use for our HTML page generators. You can think of Erubis as a Ruby Server Pages implementation, much like JSP.<br /> <br /> Aside from the Mentawai web framework implementation, I wanted a web container as similar to Tomcat as possible when it comes to functionality. Therefore I implemented the following features not currently (as of 0.3.0) supported by Rack:<br /> <br /> :arrow: Support for different applications (web contexts) running in the same server instance<br /> :arrow: Support for session expiration and invalidation in a thread-safe way<br /> :arrow: Support for added_to_session and removed_from_session callbacks (pretty much like [url="http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpSessionBindingListener.html"]HttpSessionBindingListener[/url])<br /> :arrow: Support for custom tags, but since we are using Erubis, a better name is: [i]custom page methods[/i] (more on that later)<br /> <br /> Another great thing of Ruby is RubyGems. It can take some days to hack all the pieces together to make your gem available to the world, but thanks to [url="http://newgem.rubyforge.org/"]newgem[/url] this process can now take some hours instead of days. :-)<br /> <br /> So ramp up your shell console and type:<br /> [code]<br /> C:\&gt;gem -v<br /> 1.0.1<br /> [/code]<br /> You should see 1.0.1 as above. If you don't, just type:<br /> [code]<br /> c:\&gt;gem update --system<br /> [/code]<br /> Wait until your RubyGems is updated to the latest version.<br /> <br /> Then you need to install Mongrel, Rack, Erubis and Mentawai, in any order.<br /> [code]<br /> c:\&gt;gem install mongrel<br /> ...<br /> c:\&gt;gem install rack<br /> ...<br /> c:\&gt;gem install erubis<br /> ...<br /> c:\&gt;gem install mentawai<br /> ...<br /> [/code]<br /> That's it. You are ready to play with all these precious [i]gems[/i]. Type [i]menta [/i]to make sure Mentawai was installed correctly:<br /> [code]<br /> C:\&gt;menta<br /> mentawai 0.5.0 - The Mentawai Web Framework implemented in Ruby<br /> More information on http://www.mentaframework.org<br /> <br /> -v,--version         Show version<br /> -h,--help              Show this help<br /> &lt;appname&gt;        Create blank menta app<br /> [/code]<br /> Now all you have to do is go to a directory such as c:\my_ruby_projects and type [i]menta &lt;app_name&gt;[/i] to create your blank Mentawai application:<br /> [code]<br /> C:\my_ruby_projects&gt;menta HelloMenta<br /> Create menta app "HelloMenta"<br /> To run: ruby start.rb inside "HelloMenta"<br /> [/code]<br /> Success! Go to the [i]HelloMenta [/i]directory and check the files.<br /> [code]<br /> C:\my_ruby_projects\HelloMenta&gt;dir<br />  Volume in drive C has no label.<br />  Volume Serial Number is 94CE-46A6<br /> <br />  Directory of C:\my_ruby_projects\HelloMenta<br /> <br /> 03/17/2008  06:31 PM    &lt;DIR&gt;          .<br /> 03/17/2008  06:31 PM    &lt;DIR&gt;          ..<br /> 03/17/2008  06:31 PM    &lt;DIR&gt;          actions<br /> 03/17/2008  06:31 PM               242 app_manager.rb<br /> 03/17/2008  06:31 PM               476 start.rb<br /> 03/17/2008  06:31 PM    &lt;DIR&gt;          views<br />                2 File(s)            718 bytes<br />                4 Dir(s)  36,571,320,320 bytes free<br /> [/code]<br /> :arrow: The [i]start.rb[/i] file is what you will use to start the web server to serve your application.<br /> :arrow: The [i]app_manager.rb[/i] is the old and famous Mentawai application manager.<br /> :arrow: The [i]/actions[/i] directory is where you will place your actions<br /> :arrow: The [i]/views[/i] directory is where you will place your pages (erubis templates)<br /> <br /> Before we go into more details about these files, let's run our application so we can be happy:<br /> [code]<br /> c:\my_ruby_projects\HelloMenta\ruby start.rb<br /> Loading app manager: app_manager.rb<br /> Initializing application manager...<br /> File mask: ./actions/**/*.rb<br /> Loading ./actions/hello.rb<br /> [/code]<br /> Now point your browser to http://localhost:8081/Hello.mtw?username=Jack and you should see the results of your action.<br /> <br /> [u]The start.rb file:[/u]<br /> [code]<br /> require 'rubygems'<br /> require 'mentawai'<br /> <br /> server = Mentawai::Server.new<br /> server.host = '0.0.0.0'<br /> server.port = 8080<br /> <br /> # Setup the ROOT context (ROOT web application)<br /> server.application('/', 'app_manager.rb', 'AppManager')<br /> <br /> # You can setup as many applications as you want<br /> # Ex:<br /> # server.application('/myapp1', 'myapp1.rb', 'AppManager')<br /> # server.application('/myapp2', 'myapp2.rb', 'AppManager')<br /> <br /> # Start mongrel web server to server you mentawai app<br /> server.start<br /> [/code]<br /> The comments say it all. You can create as many web applications and each one will have its own context path. For each application you specify a context path, the application manager file and the application manager class name. Then you start the server.<br /> <br /> [u]The app_manager.rb file:[/u]<br /> [code]<br /> include Mentawai::Core<br /> <br /> class AppManager &lt; ApplicationManager<br /> <br />   def init(appContext)<br />     action("Hello").on(:success =&gt; Forward.new("hello.erb"))<br />     addPageMethod(:m_my_method, 'Mentawai::Page::Method::Out', 'print')<br />   end<br />   <br /> end<br /> [/code]<br /> That's the good and old application manager. Mentawai has conventions for everything as well. Note how you can define your own page methods. (more about page methods in a later article!)<br /> <br /> [u]The hello.rb action file:[/u]<br /> [code]<br /> <br /> class Hello &lt; Mentawai::Core::Action<br /> <br />     def execute<br />       <br />       puts "Inside action Hello!"<br />       <br />       username = input['username']<br />       <br />       username = "NO USER" if username.nil?<br /> <br />       output['username'] = username.upcase<br />       <br />       :success<br />       <br />     end<br /> end<br /> [/code]<br /> [u]The hello.erb view page:[/u]<br /> [code]<br /> &lt;html&gt;<br /> &lt;body&gt;<br /> &lt;!-- using menta method m_out --&gt;<br /> &lt;h3&gt;Hello1: &lt;%= m_out('username') %&gt;&lt;/h3&gt;<br /> &lt;!-- using custom page method --&gt;<br /> &lt;h3&gt;Hello2: &lt;%= m_my_method('username') %&gt;&lt;/h3&gt;<br /> &lt;/body&gt;<br /> &lt;/html&gt;<br /> [/code]<br /> That's it for now. After we have a solid foundation for the Mentawai architecture, we can start coding all the thousands features of the Mentawai framework. Thanks to Rack it looks like we are in the right track!<br /> ]]></description>
				<guid isPermaLink="true">http://blogs.mentaframework.org/posts/preList/18/29.page</guid>
				<link>http://blogs.mentaframework.org/posts/preList/18/29.page</link>
				<pubDate><![CDATA[Mon, 17 Mar 2008 16:50:47]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>