views:

119

answers:

2

Hi .. My objective is create an apache module that will provide RESTful services (i.e., we have some legacy code that controls/queries some networking equipment and we would now like to expose that functionality as a RESTful service). I guess the flow might look something like this:

WebBrowser -- issues RESTful URI---> [Apache (my_module) ] -->.. ..---> Interface to existing Legacy code.

I have been mucking around various wikis, blogs, forums, articles etc. but I just can't seem to understand how those RESTful urls will get to (my_module) in apache [you can tell I have never worked with web-servers internals, much less modules, before]. I mean, do I have to edit that httpd.conf file and say something like: Send all urls that look like http://baseurl/restservices/... to my_module. If so, how do I do it?

Also, what will my_module actually get? Does it get the full http request message and it has to parse it like typical CGI programs?

Further, what is the best way for my_module to interact with my legacy code? E.g., Open a TCP connection to it and send messages and write wrapper around legacy code to interpret those messages. Or can my_module directly invoke the functions in my legacy code somehow if I compiled my entire legacy code as a module in apache?

Thanks for any hints. If u know of a good tutorial, please point me to it. I'm looking for a high level overview that will give me the architecture (the developers under me can then follow up on the nitty-gritty details).

+2  A: 

I'd write an extension for PHP or Python and use mod_php / mod_wsgi

I think you are approaching this in the wrong way:

Apache modules are not really how you want to handle a URL if your requirements are quote basic. Depending on the language your legacy code is in, I would advise:

Binding its API into a python or PHP module, and have that script called by Apache through normal means. It is also a lot simple (in many cases) to glue a C-call style compiled language to these scripting languages rather than Apache itself.

It also has the advantage of adding an abstractions which allows you to layer additional logic in a scripting language on your core legacy code. You may also want to preprocess data and validate it from the request before handing it into your legacy code.

Both PHP and Python also have RESTful frameworks and utilities.

If you do write an Apache module, then check out Writing Apache Modules with Perl and C

See: Developing PHP Extensions in C, Extending Python in C or C++ ... also if using Python checkout the WSGI stuff.

Aiden Bell
Thanks for the answer. The legacy code is in "C". Load expectation is several end-users connecting to it and all of them possibly making 10-100 calls/sec. That might be anywhere between 100-1000 requests/sec. Hence, I guess to improve performance, the decision to implement a apache module seemed prudent rather than apache calling a python cgi script for each request (which would be very time consuming). So I was thinking of implement a apache module (preferably in C, I assume, since apache is also in C and it would all recompile nicely).
G.A.
My advice, keep it simple and bind to a scripting language. The overhead isn't that massive and machines typically cost less than increased code complexity and lack of flexibility over time. Plus implementing your own Restful handlers to decode POST and whatnot will be a nightmare. But, the book in my post should tell you what you need. Apache's runtime is basically a ton of hooks to C functions that you register through a struct.
Aiden Bell
Yes, I think that will be the first direction I'll take - i.e., avoid apache modules and try to see if prototype without it will be acceptable to customer.
G.A.
+1  A: 

I'd agree with Aiden. Writing Apache modules is not for the faint hearted and you definitely don't want to go there unless you absolutely must. You would need to be prepared to become very conversant with how Apache works.

If you still think you need to, then look at:

http://httpd.apache.org/apreq/

This is a library which uses existing Apache Runtime Libraries and which provides higher level functionality for dealing with POST data, cookies etc from C code hooked into Apache via a custom module.

The book Aiden mentions though is a bit dated. Better off getting:

The Apache Modules Book: Application Development with Apache

Graham Dumpleton