How to write CGI scripts using Haskell?

I want to make a web application in Haskell (for example, it could be a blog, forum or some dynamic web pages), what do I need?

(I know that I need an http server (such as apache or lighttpd). I also know that I need to know some Haskell programs.)

How do I get everything to work together? I do not understand the whole package / setup.

Do I need mod_haskell or other modules?

Please explain to me how apache modules work and how to install them?

+4
source share
3 answers

Imagine that you are creating a dynamic website in your chosen programming language.

When a user arrives at your site, they go to http://name-of-your-site.com and this is transferred to your server.

When the request arrives at port 80, it is collected by your HTTP server, which is probably Apache, but could be LightHttpd or any other HTTP server. This will receive a request and decide what to do with it.

Now, imagining that your site is written in python, it will be stored as a bunch of .py files somewhere, and therefore the request should be passed at runtime python with instructions to run the file and to return the output from this file. This is the role of mod_python - receiving requests from the server and transmitting them at runtime. Most mods also process a thread pool - suppose you have twenty requests in one minute, if each of them is passed at runtime python in a simple way, then you will have twenty python threads, all running, working together, and then dying as the process completes, typically Apache modifications will support multiple threads up and down to save them during startup and simply transfer new requests to one of the existing threads, so that when it completes one request, it will receive nother interface. CGI containers perform the same task in different ways, the reason you can choose one over the other will most likely be related to which HTTP server you are using (mod_python is for Apache, for example, something like FastCGI used more with LightHttpd) and for performance reasons. If you use something like FastCGI, then you may need a second level of interface between the CGI container and the programming runtime.

So, the layers you work with look something like this:

HTTP Server-> CGI Layer -> Programming Language Runtime -> your code Apache -> mod_python -> Python Runtime -> index.py LightHttpd -> FastCGI+python_cgi -> Python Runtime -> index.py 

Obviously, I used Python as an example here, but you can find equivalent mods and cgi containers for most major languages ​​(and many esoteric ones) and the Http stack that you work with will be generally similar in most cases.

+6
source

Short answer to the question about the name: Yes.

See http://hackage.haskell.org/package/cgi

Network.cgi

A simple library for writing CGI programs. See http://hoohoo.ncsa.uiuc.edu/cgi/interface.html for the CGI specification.

Here is a simple example, including error handling (not many that might go wrong with Hello World)

  import Network.CGI cgiMain :: CGI CGIResult cgiMain = output "Hello World!" main :: IO () main = runCGI (handleErrors cgiMain) 

Regarding the integration of parts.

CGI is a programming protocol and interface between a web server and some external program, exchanging standard input and output data and exchanging environment variables.

You need a web server that supports CGI (most of them), and you need to configure the server so that it invokes the CGI program for some special requests (for example, URLs with some special file extension). For Apache web server see http://httpd.apache.org/docs/2.0/howto/cgi.html

+6
source

Perhaps you can find HAppS .

+1
source

All Articles