view benchmark/bun-http-framework-benchmark/src/c/seobeo.c @ 216:e82b80b24012 default tip

[MrJuneJune] Make webp translate background job.
author June Park <parkjune1995@gmail.com>
date Sat, 28 Feb 2026 21:04:43 -0800
parents a8976a008a9d
children
line wrap: on
line source

/**
 * Seobeo HTTP Framework Benchmark
 *
 * Implements the 3 required endpoints:
 * - GET /          -> "Hi"
 * - GET /id/:id    -> "{id} {name}" with x-powered-by header
 * - POST /json     -> mirrors JSON body
 */

#include "seobeo/seobeo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// GET / - Return "Hi"
Seobeo_Request_Entry* GetIndex(Seobeo_Request_Entry *p_req, Dowa_Arena *p_arena)
{
  (void)p_req;

  Seobeo_Request_Entry *p_resp = NULL;
  Dowa_HashMap_Push_Arena(p_resp, "status", "200", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "content-type", "text/plain", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "body", "Hi", p_arena);

  return p_resp;
}

// GET /id/:id - Return "{id} {name}" with x-powered-by header
Seobeo_Request_Entry* GetId(Seobeo_Request_Entry *p_req, Dowa_Arena *p_arena)
{
  // Get the :id param (stored as ":id" key)
  void *p_id_kv = Dowa_HashMap_Get_Ptr(p_req, ":id");
  const char *id = p_id_kv ? ((Seobeo_Request_Entry*)p_id_kv)->value : "";

  // Get the query param "name" (stored as "query_name")
  void *p_name_kv = Dowa_HashMap_Get_Ptr(p_req, "query_name");
  const char *name = p_name_kv ? ((Seobeo_Request_Entry*)p_name_kv)->value : "";

  // Build response body: "{id} {name}"
  size_t body_len = strlen(id) + 1 + strlen(name) + 1;
  char *body = Dowa_Arena_Allocate(p_arena, body_len);
  snprintf(body, body_len, "%s %s", id, name);

  Seobeo_Request_Entry *p_resp = NULL;
  Dowa_HashMap_Push_Arena(p_resp, "status", "200", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "content-type", "text/plain", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "x-powered-by", "benchmark", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "body", body, p_arena);

  return p_resp;
}

// POST /json - Mirror the JSON body
Seobeo_Request_Entry* PostJson(Seobeo_Request_Entry *p_req, Dowa_Arena *p_arena)
{
  // Get the request body
  void *p_body_kv = Dowa_HashMap_Get_Ptr(p_req, "Body");
  const char *body = p_body_kv ? ((Seobeo_Request_Entry*)p_body_kv)->value : "{}";

  Seobeo_Request_Entry *p_resp = NULL;
  Dowa_HashMap_Push_Arena(p_resp, "status", "200", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "content-type", "application/json", p_arena);
  Dowa_HashMap_Push_Arena(p_resp, "body", (char*)body, p_arena);

  return p_resp;
}

int main()
{
  // Initialize router
  Seobeo_Router_Init();

  // Register routes
  Seobeo_Router_Register("GET", "/", GetIndex);
  Seobeo_Router_Register("GET", "/id/:id", GetId);
  Seobeo_Router_Register("POST", "/json", PostJson);

  // Start server on port 3000 using edge-triggered I/O
  // Using EDGE mode for maximum performance (epoll/kqueue)
  Seobeo_Web_Server_Start(NULL, "3000", SEOBEO_MODE_EDGE, 4);

  return 0;
}