13#define _POSIX_C_SOURCE 200809L
16#include <cjson/cJSON.h>
45static char *dup_or_null(
const char *s) {
54 for (
size_t i = 0;
i <
sizeof(
all) /
sizeof(
all[0]);
i++) {
62 for (openapi_doc *
d = g_docs;
d;
d =
d->next) {
63 if (
d->app == app)
return d;
70 for (
const openapi_note *
n = doc->notes;
n;
n =
n->next) {
71 if (
n->method == method &&
strcmp(
n->path, path) == 0)
return n;
76static void doc_destroy(
void *ctx) {
79 for (openapi_doc **
p = &g_docs; *
p;
p = &(*p)->next) {
107 if (!out)
return NULL;
109 for (
const char *r =
route; *r;) {
110 if (*r !=
':' || (r !=
route && r[-1] !=
'/')) {
114 const char *name = ++r;
115 while (*r && *r !=
'/') r++;
117 memcpy(
w, name, (
size_t)(r - name));
156 for (
char *
c =
verb; *
c;
c++) *
c = (
char)(*
c | 0x20);
166 while (*
t ==
' ')
t++;
214 bool enabled = find_doc(app) !=
NULL;
221 doc->json_path =
cwist_strdup(json_path ? json_path :
"/openapi.json");
223 doc->title = dup_or_null(
meta->title);
224 doc->version = dup_or_null(
meta->version);
225 doc->description = dup_or_null(
meta->description);
226 doc->server_url = dup_or_null(
meta->server_url);
228 if (!doc->json_path) {
239 if (!cwist_error_is_ok(&
err)) doc_destroy(doc);
243 const char *summary,
const char *tags) {
244 if (!app || !method || !path)
return;
258 note->next = doc->notes;
265 note->summary = dup_or_null(summary);
266 note->tags = dup_or_null(tags);
Memory allocation wrappers backed by libttak.
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
char * cwist_strdup(const char *src)
Duplicate a string using libttak-backed storage.
char * cwist_strndup(const char *src, size_t n)
Duplicate up to n bytes of a string (libttak-backed).
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
cwist_error_t cwist_sstring_assign(cwist_sstring *str, const char *data)
Assign a C string.
@ CWIST_HTTP_INTERNAL_ERROR
cwist_error_t cwist_http_header_add(cwist_http_header_node **head, const char *key, const char *value)
Add a header to the list.
const char * cwist_http_method_to_string(cwist_http_method_t method)
Convert method enum to string.
struct openapi_doc openapi_doc
void cwist_app_enable_openapi(cwist_app *app, const char *json_path, const cwist_openapi_meta_t *meta)
Serve the app's OpenAPI 3.1 document.
struct openapi_note openapi_note
void cwist_openapi_annotate(cwist_app *app, const char *method, const char *path, const char *summary, const char *tags)
Attach a summary and tags to one operation of the document.
OpenAPI 3.1 generation — source annotations and runtime serving.
Read-only walk of an app's route table for in-tree consumers (the runtime OpenAPI generator in openap...
void cwist_app_foreach_route(cwist_app *app, cwist_route_visit_fn visit, void *ctx)
Main Application Context.
Integrated error structure used throughout the system.
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
cwist_http_status_t status_code
cwist_http_header_node * headers
cwist_error_t cwist_app_get_ex(cwist_app *app, const char *path, cwist_handler_ex_func handler, void *user_ctx, cwist_handler_ctx_destroy_func destroy)
Registers a GET route whose handler receives a user context.