Help with setting up RESTful API type of setups using C under linux?
So my boss wants to update how we do some things, and he wants to setup part of the system RESTfully, but reusing most of the code we've already got, which are .cgi scripts written up in C. I've started going through and converting all of my error setup to use the HTTP errors, while dumping my output back as JSON arrays, rather than webpages.
But my cgi files are still cgi files, all sitting in the cgi directory. I can't figure out how to word my google searches to bring up what I'm trying to find out, which is how to map incoming requests, so that, for instance, a GET to www.example.com/users/12?session=34534545 goes to my user_manager.cgi script in view mode for user number 12, while a PUT to www.example.com/users/12?session=34534545 goes to it in update mode, or a POST to www.example.com/users/12/orders goes to write_order.cgi and writes up a new order for user 12.
Do I have to write up some sort of parsing script that pulls apart everything after every incoming request, figures out which script it needs to hit and then calls that script with all of the variables being sent along? Or are there (surely!) plugin modules for apache2 servers that do what I need doing, that I can just add some routing info into?
jeff47
(26,549 posts)Faced with this, I'd probably use a REST framework in Java or python, parse out the parameters, and then use the language's native interface to call the C code.
But a big part of that approach is I have no idea about any C/C++ REST frameworks. I have to assume they exist, but I have no idea which ones do not suck.
Erich Bloodaxe BSN
(14,733 posts)I've used system calls before in C to call one piece of code from another, but not outside of the language. I almost exclusively work in C, with a tiny bit of php or javascript if I can't avoid it, but haven't ever worked in java or python.
jeff47
(26,549 posts)Basically, the way Python/C or Java's JNI work is your C code would reside in a .so or an executable. You'd load them in Java or Python, and then call a function in you write in C that matches a particular signature. In that function you deal with memory management from Java/Python, and then directly call your C functions like normal.
But going to another language adds a lot of complexity. So unless you're wanting to learn a new language, or you're trying to convince the boss to rewrite everything, it'll be a lot easier to use one of those C frameworks.
Erich Bloodaxe BSN
(14,733 posts)amee
(2 posts)Yes. You write logic that runs on the web server to parse each incoming request and branch to the appropriate CGI; called a gateway... a single file to handle all of your CGI `redirects`. This is how things would normally work. A gateway CGI, or some other server side logic to parse the request and branch.
Other workable solutions might include; with Apache, mod rewrite. With IIS, server-side filters.