Skip to content

Commit

Permalink
chore(docs): add code sample to landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
exbotanical committed Aug 12, 2023
1 parent 2fb9cf7 commit 7fe3826
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/guide/what-is-ys.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ Ys is a modern, minimal web application framework for the C programming language
- [Zero-config CORS](../documentation/cors.md)
- [Cookies](../documentation/cookies.md)

```c
#include "libys.h"
// ...

ys_response *handler(ys_request *req, ys_response *res) {
ys_set_header(res, "Content-Type", "text/plain");

ys_cookie *c = ys_cookie_init(COOKIE_ID, sid);
ys_cookie_set_expires(c, ys_n_minutes_from_now(10));
ys_set_cookie(res, c);

ys_set_body(res, "Hello World!");
ys_set_status(res, YS_STATUS_OK);

return res;
}

int main() {
ys_router_attr *attr = ys_router_attr_init();
ys_use_cors(attr, ys_cors_allow_all());
ys_router *router = ys_router_init(attr);

ys_router_register(router, "/", handler, YS_METHOD_GET);

ys_server *server = ys_server_init(router, PORT);
ys_server_start(server);

return EXIT_SUCCESS;
}
```
## Why?
C programs tend to be verbose. You seldom see C servers in the wild because the development overhead is absolutely massive and mired in complexity; before you can think about building an API, you'll need to sift through system calls, parse raw requests, juggle socket descriptors, and become intimately familiar with the HTTP specification.
Expand Down

0 comments on commit 7fe3826

Please sign in to comment.