Warpy's Blitz on Rails server!

Community Forums/Showcase/Warpy's Blitz on Rails server!

Warpy(Posted 2009) [#1]
As part of my ongoing quest to do horrible things to blitzmax, I've written a ruby on rails-style server.
You just include "server.bmx", call start_server and run_server, and off it goes!
It has a rails-style routing syntax, so you can add routes which get mapped to particular controllers and views.

An example of a route is
route.Create "/blog/:postid/:posttitle",["controller => blog", "action => view"]

The first argument is the route pattern - things with a colon in front match any text, and you can also use regexps by putting a \ in front of them.
The second argument is an extra set of labels you can define to help the system decide where to send the request. Every request needs at least a "controller" and an "action" label.

Once a route has been mapped, the system uses reflection to find the type whose name corresponds to the controller label, and a function, or view, belonging to that type whose name is the same as the action label.
That function should take one argument, of type HTTPSession.

A view's job is to do whatever action was requested, then provide the content of the page. The action part is up to you, but the rendering can either be done by directly calling s.render with the text you want, or by using render_template
All the labels from the routing phase are stored in the HTTPSession's "labels" map.

render_template takes one argument, the name of a .bhtml file (without the extension) found in the templates folder. It then fills in the template using the following ruby-like syntax:
Something of the form <%= label %> inserts the value of the given label, found in the HTTPSession's "info" map.
You can also do things like <%= object.name %> to access an object's fields (though you have to insert it into the info map)

There's only one control function so far, and that's <% for OBJECT in COLLECTION %> ..... <% endfor %>.
It works just like an EachIn in blitzmax, and should work with any collection.

Have a poke around the example, it does the basic add/view/list/delete stuff.

Enjoy!


Warpy(Posted 2009) [#2]
PS I couldn't have even got started without James L Boyd's blitzserve 2, and in order to compile it you need grable's modified reflection.bmx found here


Warpy(Posted 2009) [#3]
I'm a bit annoyed nobody's noticed this, because it's really really clever.

Anyway, I've worked on it more today and the templating thing does a lot more, I've integrated my list filtering algorithm from a while ago, and all sorts of other things.

I've written a really simple blog app to test it out, included in the zip.


dmaz(Posted 2009) [#4]
I think some people may just be lost on exactly what it does and what it would be used for...?


Brucey(Posted 2009) [#5]
I'm a bit annoyed nobody's noticed this, because it's really really clever.

Heh. Good luck with that!

Remember, BlitzMax is a games programming language. You might find really clever things will not be of great interest ;-)


N(Posted 2009) [#6]
Neat, although I can't imagine I'd have any practical use for such a thing when I've got Ruby here (and oh how awesome Ruby is- seriously, Ruby is the most awesome of awesome languages). I'll probably fiddle with the source code at the most before dropping it the next time I forget what I'm doing (interval is roughly 8 hours between each bout of horrifying confusion).

Remember, BlitzMax is a games programming language. You might find really clever things will not be of great interest ;-)

Yeah, unfortunately, doing horrible things to BlitzMax isn't very high-up on the interest radar for most people. For example, I tried writing new classes/types at runtime. Interest? Nada.


degac(Posted 2009) [#7]

I'm a bit annoyed nobody's noticed this, because it's really really clever.



Well, consider that someone (like me) doesn't know what exactly is 'Ruby' or what is a 'route'...I understand you have made up a 'server' with a 'special language-syntax-techology' like ruby-on-rails...
So I can say 'oh! great!' but to be honest I don't understand exactly what you have done (not your fault of course, but mine! Maybe after reading the source code I found it very 'inspiring' and useful for other purpose and - then - I can really say 'Wow, this is great!'

So at the moment I can only say thanks for sharing!


Warpy(Posted 2009) [#8]
Yeah, I'm not annoyed _with_ anyone, I was just hoping for a pat on the back :P

I'm carrying on on my own little programming odyssey anyway. I've just written a generic JSON serialiser to have permanent storage. Choosing the right tool for the job is clearly not my modus operandi.


wmaass(Posted 2009) [#9]
Warpy, I think it's very clever and plan to check it out. Keep it coming.


BlitzSupport(Posted 2009) [#10]

I was just hoping for a pat on the back


Well, I thought your server code was really rather special. :P


GaryV(Posted 2009) [#11]
I was just hoping for a pat on the back
We can get puki lubed up and send him over if you want?


N(Posted 2009) [#12]
We can get puki lubed up and send him over if you want?
I'm suing you for acute mental and emotional distress.


slenkar(Posted 2009) [#13]
you mean i can serve a website from my pc ?

You could create an online example for people to surf to.To generate more interest.

I take it, the website cant be built with blitzmax code, it must be built with this 'ruby' like language?


Also I ran the blog.exe and it asked to be unblocked which i did. Then it doesnt do anything when double-clicked, same thing happens when I double click on example.exe.


xlsior(Posted 2009) [#14]
you mean i can serve a website from my pc ?


Anyone can do that, provided that:
- Your ISP allows incoming connections to port 80 (otherwise you'd be forced to pick a non-standard port),
- Your ISP's TOS allows you to run a server (many explicitely prohibit this)
- Your firewall/router knows to forward unsollicited requests for port 80 to your actual PC
- Your windows firewall permits incoming requests on port 80
- You feel confident that the server software is secure enough not to have people break out of its constraints and gain access to other files on your PC.

(Even IIS and Apache have been known to 'leak' through various exploits over the years)


slenkar(Posted 2009) [#15]
its probably not in the TOS :) not sure if port 80 can accept connections either


Warpy(Posted 2009) [#16]
Paxo - yes, it's an http server to serve a website, but not in the traditional just-serving-files or even cgi sense. It uses a 'model - view - controller' approach, which roughly means it maps urls to blitz types and their methods. It doesn't use any ruby, it's pure bmax, but it is inspired by Ruby on Rails, which is the famous package doing the same thing in ruby.

If you run blog.exe, it should eventually say "server up an running and going", then if you go to http://127.0.0.1/ in your browser you should see what it does.


Brucey(Posted 2009) [#17]
its probably not in the TOS :) not sure if port 80 can accept connections either

A reasonably secure OS will require admin rights to allow listening on any ports < 1024.


skn3(Posted 2009) [#18]
Oooo interesting. This could be used to easily do web based configuration for a custom game server.


slenkar(Posted 2009) [#19]
pretty cool,
blog.exe works but example.exe says 'Can't find template list'

what does it use as a database?


Warpy(Posted 2009) [#20]
Oh, example.exe shouldn't be in there any more, it was my first test app. It doesn't really use a database yet, it can serialise objects to JSON and save that to a file, but nothing efficient. Everything's held in memory too, so for database queries I use my clever list filter thing which is in the code archives.


slenkar(Posted 2009) [#21]
so when the program is launched it loads the entire database to memory


Warpy(Posted 2009) [#22]
yep. No good, I know, but I've never written a database before so I'm leaving that until later.

I've spent today making a textile markup parser, and I've uploaded the new version. I should rewrite the readme, almost none of it applies any more.


slenkar(Posted 2009) [#23]
mysql's syntax is a bit annoying, and difficult to debug,


Chroma(Posted 2009) [#24]
Warpy, can this be used for fast 64 player online battles? If so then i'm down.


Warpy(Posted 2009) [#25]
No, it's an http server, for serving web pages.


Mahan(Posted 2009) [#26]
I'm a bit annoyed nobody's noticed this, because it's really really clever.


I just noticed it!

It's just that BlitzMax is not the first language you think about when you think about an WebApp.

As a Python/Django enthusiast I appreciate your work here and I'll check out your code.


... you need grable's modified reflection.bmx ...



You're using this for the dynamic method lookup i suppose? I hope this will be added to the official version of bmx because i don't like "patching" the brl-lib unless absolutely necessary.

I've been working with a Crud (very basic ORM that does not understand relations) at home for a while and for basic things it's great (although it's very very alpha yet)

Your work here (View and Controller mostly from what I understand) together with some ORM/Crud (Model, Persistence) could actually become a framework to build real-world webapps in.

Regarding reflection (which I a'm big fan of myself tbh): I question it's use in a web framework, if you want great performance. Since Bmx is a compiled language there can be very huge performance gains when not using it in production, if the webapp is to be heavily used.

One way to eliminate reflection (or at least reduce it in performance critical parts of the webapp) might be "pre compile-time code generation". Things like the mapping of view-controller, model-db etc, could be auto-generated into .bmx files that are compiled into the final .exe. (build script)

This is in no way a fully thought out plan or anything, but merely some ideas that have popped out of my mind when i saw this.

Here, have this "pat on the back" :-) good work!