Developers
A language that doesn't have everything is actually easier to program in than some that do.
A Web server for Software as a Service (SaaS)
Software licenses are costly because they cover lifetime use. And updates are no longer needed once a product offers the features people need. Add piracy and a recession on the top of that and software developers' revenues will dip – whatever they do.
SaaS brings recurring revenues and makes your product more affordable: users need only to pay for the time they want to use it.
G-WAN is, by far, the best tool to migrate your applications from the desktop to the Web – and to bring your costs down.
Java, C/C++, D and Objective-C/C++ <scripts>
G-WAN supports Java, C/C++, D, Objective-C and Objective-C++ natively (without handlers). Soon, G-WAN will also natively support other languages like Ada, Basic, Cobol, Fortran, Google Go, Modula, Pascal and Scheme (cited in alphabetic order).
Scripts let you instantly test your code by pressing F5 in the Web browser. By contrast, Apache, Lighttpd or Nginx modules require you to stop the server, compile and link your C source code, and then restart the server – each time you need to edit your code.
By offering scripts in compiled languages, G-WAN delivers the productivity that has made less efficient languages popular for Web development (G-WAN can natively support PHP if PHP becomes a compiled language).
Google Go, Javascript, Lua, Python, etc.
When compilers are not available, the next best option is a thread-safe library linked with a G-WAN handler (at this time, PHP-embed is not thread-safe). G-WAN handlers let you add features and modify the behavior of G-WAN, like providing support for more programming languages (here are handler examples for Javascript, Lua, Google Go, and Python).
There is older option still widely in use. But despite its name, FastCGI is not compatible with the terms performance and scalability.
A SERVLET example: 'Hello World' (C and Java illustrated)
G-WAN servlets are scripts (here in ANSI C) which let G-WAN build dynamic replies for client requests.
hello.c
This is the traditional example: /csp?hello.c
1 2 3 4 5 6 7 |
#include "gwan.h" // G-WAN API
int main(int argc, char *argv[])
{
xbuf_cat(get_reply(argv), "Hello World");
return 200; // HTTP status (200:'OK')
} |
Compare these 7 lines of code to the 120-line Nginx 'Hello World' module (which requires a dedicated configuration file).
The code above is stored in gwan/.../csp/hello.c under the G-WAN directory. Deploying C scripts is as simple as copying source code files in this folder (or in a /csp sub-folder).
To change the behavior of hello.c, edit its source code (G-WAN will always serve the latest version saved on disk).
hello.java
Here is the same script in Java: /csp?hello.java
1 2 3 4 5 6 7 8 |
public class hello
{
public static int jmain(long env, String[] args)
{
gwan.xbuf_cat(gwan.get_reply(env), "Hello World");
return 200; // HTTP status (200:'OK')
}
} |
Deploying or updating G-WAN Java scripts does not require any configuration (nor any delay: the latest version is executed on-the-fly). And, as a bonus, Java scripts are served as fast as C scripts by G-WAN, making them fly high!
Syntax Errors & Crash Reports
As usual, syntax error reports indicate the offending line in the source code:
Error parsing /csp/hellox.c:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> line 9: too few arguments to function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 | char *name = 0;
6 | get_arg("name=", &name, argc, argv);
7 |
8 | xbuf_xcat(reply, "Hello %s",
9 ! escape_html(name, name) ? name : "you");
10 |
11 | return 200;
12 | }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But identifying the cause of execution errors must be as straightforward as possible. To fix bugs, you just want to know where in your source code the problem takes place – and this too often requires you to dig into frustratingly obscure multi-page core dumps.
G-WAN does it for you: execution error reports indicate the offending source code file and line number in the call chain:
Script: crash_libc.c
Client: 127.0.0.1
Query : csp?crash_libc
Signal : 11:Address not mapped to object
Signal src : 1:SEGV_MAPERR
errno : 0
Code Pointer: 0000f5200b33 (module:/lib/libc.so.6, function:strcpy, line:0)
Access Address: 00000badc0de
Registers : EAX=00000badc0de CS=00000033 EIP=0000f5200b33 EFLGS=000000010202
EBX=000000000001 SS=ec2d8ed4 ESP=0000f5ded828 EBP=0000f5dee020
ECX=000033323130 DS=ec2d8ed4 ESI=0000ec2d8f86 FS=00000033
EDX=000003b03c00 ES=ec2d8ed4 EDI=00000badc0de CS=00000033
Module :Function :Line # PgrmCntr(EIP) RetAddress FramePtr(EBP)
libc.so.6: strcpy: 0 0000f5200b33 0000ec2d8f00 0000f5dee020
servlet: main: 37 0000ec2d8f00 00000042e10c 0000f5dee020
And the crash does not stop the server. Errors are just logged if log files are enabled (useful in daemon mode), and reported on stderr if G-WAN is attached to a terminal (letting you edit the script and press F5 in the Web browser to run the script again).
This makes life easier, allows to write and test code much faster, and makes bug hunting easier than system core dumps.
HANDLERS: FLV 'Pseudo-Streaming'
Unlike G-WAN servlets which can only build dynamic contents, G-WAN handlers can also modify the behavior of the server. This is useful to implement other protocols than HTTP, or to add custom features.
The Flash Player is able to tell a Web server from which position a movie should be downloaded. This is implemented as pre-compiled C modules by Apache (137 lines of code), Lighttpd (352 lines) and Nginx (257 lines).
flv.c
The C script below is much shorter, faster and easier to maintain than the corresponding Apache, Lighttpd or Nginx modules:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include "gwan.h" // G-WAN exported functions
#include <string.h> // memcmp()
int main(int argc, char *argv[])
{
char *query = get_env(argv, QUERY_STRING, 0); // query: "start=200000"
if(!query || memcmp(query, "start=", sizeof("start=") - 1)) // FLV query?
return 200; // HTTP status (200:'OK')
http_t *head = get_env(argv, HTTP_HEADERS, 0); // set HTTP bytes range
head->h_range_from = atol(query + sizeof("start=") - 1); // checked by G-WAN
#define FLV_HEAD "FLV\x1\x1\0\0\0\x9\0\0\0\x9" // insert the FLV Header
http_header(HEAD_ADD | HEAD_AFTER, FLV_HEAD, sizeof(FLV_HEAD) - 1, argv);
return 206; // HTTP status (206:'Partial Content')
} |
This FLV content-type handler is stored as /gwan/.../handlers/flv.c (the file name must match the targeted MIME type).
This is in contrast with the general-purpose connection handler (called main.c, or main.cpp, or main.m, depending on its programming language) which lets you alter the behavior of the server at each possible step (ACCEPT, READ, PARSING, BUILDING, HTTP_ERROR, REPLYING, etc.).
These examples illustrate how C scripts seriously fit the task of rapid (and efficient) Web development – especially when used from a Web framework designed to make things as simple as they should be.
To go further, read the C servlet and handler examples and consult the G-WAN API documentation.
Kids will Learn Compiled programming languages Again
After the CPU frequency halt ("The free lunch is over") and with increasingly tight budgets, efficiency will soon directly make the difference between success and failure – whether your projects are small, or not.
The frenetic wave of abstraction layers that we have been served the last decade ends now. Today, necessity has made "bare-to-the-metal" fashionable again. And not many tools are helping with G-WAN's ease of use and efficiency.