KLone: C web programming framework

June 3rd, 2007 edited by Tincho

Article submitted by Kari Pahula. We are running out of articles ! Please help DPOTD and submit good articles about software you like NOW !

PHP is well known for its coding style, mixing HTML with source code inside special <?php code > tags. There are tools and frameworks for that kind of web development using other programming languages like Perl and Python, but there is one fairly surprising choice of programming language that you could use instead, namely C.

KLone is a web application development framework that takes HTML with C embedded in as its input and turns it all into a single binary that is the server and the web app in one package.

Let’s step through an example of how to make a simple Hello World type of web app. First off, apt-get install klone-package. That’ll install KLone and a few tools you can use in a Debian environment. Change to a directory where you have a write permission.

$ make-klone-project create -p myhello
$ cd myhello-0.1

There will be a number of files and directories in the project directory. The important ones are debian/, where you have the files necessary for generating Debian packages containing your web app. We’ll return to that part later on. For now, let’s concentrate on the other directory of interest, userdata/.

$ cd userdata
$ mkdir etc
$ cd etc

Create and edit a file called kloned.conf.

server_list my_http
allow_root yes

    type      http
    addr.type IPv4
    addr.port 8880
    dir_root  /www

Now we’re ready for action!

$ cd ..
$ mkdir www
$ cd www

Create and edit a file called index.kl1. Any files that end with the .kl1 suffix will be treated as HTML/C files.

#include <time.h>
time_t now;
<head><title>A hello world app for debaday</title></head>>
<h1>Hello World</h1>
now = time(0);
io_printf(out, "Time is now %s\n", ctime(&now));

That’s really all there is to it. Now, return to the project root directory and to build a kloned server, just run:

$ kloned-build -o myapp userdata
$ ./myapp -F

That -F switch is there so that the server won’t start as a daemon. Now you can access http://localhost:8880/ with the web browser of your choice to see a friendly greeting and to know the current time. But this isn’t the whole story, yet. Stop the web app you just created, run apt-get install dpkg-dev and then do the following:

$ dpkg-buildpackage -rfakeroot
$ sudo dpkg -i ../myhello_0.1*deb

You have just installed a Debian package containing your web app. As long as the package is installed, you can rely on having your app start at boot time.

If you want to see more examples of how to use KLone, feel free to visit KoanLogic’s web pages.

KLone is available in Debian in Etch and later, and in Ubuntu since Edgy.

Posted in Debian, Ubuntu |

9 Responses

  1. Kirill Says:

    very interesting for C programmers, but not for PHP,PERL etc programmers.

  2. logan Says:

    >very interesting for C programmers, but not for PHP,PERL etc programmers.

    Why? PHP programmers may be different :)
    And some of us will be very interests this tool

    //sorry for bad English..

  3. CFA Says:

    imho, it also ugly as php, mixing code and html code is a very bad idea

  4. ury Says:

    what about mvc, ORM ?

    very, very interesting, but seems to be p-o-c, not a vital solution.

  5. aki Says:

    rails ftw!

  6. Pedro de Medeiros Says:

    Well, it is perfect for embedded devices, like web control panels for ADSL modems and things like that.

  7. Adam Victor Nazareth Brandizzi Says:

    It seems a good solution to embedded Web applications, like wireless routers and cable modems configuration software.

  8. Reed Says:

    This is really neat for writing very quick tools with a web interface. Does it offer a library of functions to e.g. make it easier to get CGI parameters?


  9. Radek Hnilica Says:

    Well the article is interesting. I build the simple web server. But I didn’t find how to link it static. So I’m not abble to move it to another host.