Preface
cgull is a way to write Cloudflare Workers in C. You write an ordinary C program that registers a few routes and handles requests; cgull compiles it to WebAssembly and wraps it in the few lines of JavaScript a Worker needs. You don't write the JavaScript, and you need not know it is there.
Here is a complete program:
#include <cgull.h>
static void hello(cg_req *req, cg_res *res)
{
const char *name = cg_param(req, "name");
cg_printf(res, "<h1>Hello, %s!</h1>", cg_esc(name));
}
int main(void)
{
cg_get("/hello/:name", hello);
return cg_run();
}
Deployed, it answers /hello/world with a greeting, from
whichever of Cloudflare's data centres is nearest the visitor. The program is the one you see: there
is no event loop to start and no server to configure. Each request runs
the program from the top of main, in a fresh instance with its
own memory. When the response has been sent, the instance is thrown away,
and with it everything the program allocated.
That makes C an easier language for this than you might expect. You
never call free, because nothing outlives the request. A
crash, even a bad pointer, spoils only the request that caused it, and
the visitor sees a 500. And the program is, underneath, a plain CGI
program of the kind written in the 1990s, so it builds natively too and
can be run under gdb or the sanitizers.
The name is a C program that lives at the edge, like the bird; the
cg of its prefix, as in cg_get, is for CGI, which is
what it is underneath. Say it "seagull".
This book is in the spirit of the one whose cover it borrows. Chapter 1
is a tutorial that gets a Worker running and deployed. Chapter 2 covers the
cgull command. Chapters 3 to 7 take the library a topic at a
time: routing, requests, responses, memory, and what the program can see
of its surroundings. Chapter 8 explains how it all works, which is short,
because there isn't much of it. Appendix A is the reference manual, with
every name in <cgull.h>.
The book is itself a cgull program. Its chapters are HTML files
compiled into the program with C23's #embed; the C code
numbers the sections, works out the page numbers in the contents and the
index, links every function name to its entry in the reference manual, and
answers the search box.
Contents
- Prefaceix
- Chapter 1A Tutorial Introduction1
- 1.1 Getting Started1
- 1.2 The First Program1
- 1.3 Running It1
- 1.4 Parameters2
- 1.5 Forms2
- 1.6 Deploying3
- Chapter 2The cgull Command5
- 2.1 cgull new5
- 2.2 cgull build5
- 2.3 cgull run6
- 2.4 cgull dev and deploy6
- 2.5 cgull setup7
- 2.6 The Project7
- 2.7 Compiler Flags7
- Chapter 3Routing9
- 3.1 Routes9
- 3.2 Patterns9
- 3.3 Methods10
- 3.4 Middleware10
- 3.5 Not Found11
- Chapter 4Requests13
- 4.1 Method and Path13
- 4.2 The Query String13
- 4.3 Headers13
- 4.4 Cookies13
- 4.5 Bodies and Forms13
- Chapter 5Responses15
- 5.1 Status and Headers15
- 5.2 Writing the Body15
- 5.3 Shortcuts15
- 5.4 Redirects16
- 5.5 Cookies16
- 5.6 When Things Go Wrong16
- Chapter 6Strings and Memory17
- 6.1 One Request, One Instance17
- 6.2 Never Free17
- 6.3 Escaping17
- 6.4 Building Strings18
- 6.5 Limits18
- Chapter 7The Environment19
- 7.1 Variables and Secrets19
- 7.2 Logging19
- 7.3 Static Files19
- 7.4 Time and Chance19
- 7.5 What Isn't There20
- Chapter 8How cgull Works21
- 8.1 CGI21
- 8.2 The Request Going In21
- 8.3 The Shim22
- 8.4 Native Builds22
- 8.5 What Comes Next23
- Appendix AReference Manual25
- A.1 Types and Macros25
- A.2 Routing25
- A.3 The Request26
- A.4 The Response27
- A.5 Strings28
- A.6 The Environment29
- Index31