Apache module for restful services

2019-08-30 08:07发布

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条回答
冷血范
2楼-- · 2019-08-30 08:23

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

查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-30 08:32

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.

查看更多
登录 后发表回答