Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .gemini/settings.json

This file was deleted.

223 changes: 223 additions & 0 deletions Documentation/app/docs/http-routing/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,229 @@ pathao authRouter; // Always export your router`}</code></pre>

<hr />

<h2>Path Parameters</h2>
<p>Use <code>:name</code> segments in route paths. Values are available in <code>req[&quot;params&quot;]</code> as a MAP.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro app = router_banao();

// Single param: /users/123
app.ana("/users/:id", kaj(req, res) {
dhoro id = req["params"]["id"];
json_uttor(res, {"user_id": id});
});

// Multiple params: /posts/42/comments/7
app.ana("/posts/:pid/comments/:cid", kaj(req, res) {
dhoro pid = req["params"]["pid"];
dhoro cid = req["params"]["cid"];
json_uttor(res, {"post": pid, "comment": cid});
});`}
</div>

<hr />

<h2>Query String Parsing</h2>
<p><code>req[&quot;query&quot;]</code> is a parsed MAP. Use <code>req[&quot;query_raw&quot;]</code> for the raw string.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`// GET /search?q=hello&page=2
app.ana("/search", kaj(req, res) {
dhoro term = req["query"]["q"]; // "hello"
dhoro page = req["query"]["page"]; // "2"
json_uttor(res, {"term": term, "page": page});
});`}
</div>

<hr />

<h2>Auto JSON Body Parsing</h2>
<p>When the request has <code>Content-Type: application/json</code>, <code>req[&quot;json&quot;]</code> is auto-parsed. Otherwise it is <code>khali</code>.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`app.pathano("/users", kaj(req, res) {
dhoro user = req["json"]; // auto-parsed MAP — no json_poro() needed
dekho("Name:", user["name"]);
json_uttor(res, {"created": sotti}, 201);
});`}
</div>

<hr />

<h2>Middleware (majhe - মাঝে)</h2>
<p>Runs before every route handler. Call <code>agorao()</code> (আগাও = go forward) to pass to the next layer.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro app = router_banao();

// Logging middleware
app.majhe(kaj(req, res, agorao) {
dekho(req["method"], req["path"]);
agorao(); // must call to continue!
});

// Auth middleware
app.majhe(kaj(req, res, agorao) {
jodi (req["headers"]["Authorization"] == khali) {
json_uttor(res, {"error": "Unauthorized"}, 401);
ferao; // stop here — don't call agorao()
}
agorao();
});

app.ana("/", kaj(req, res) {
uttor(res, "Protected page");
});`}
</div>

<hr />

<h2>CORS (cors_chharpao - ছাড়পাও)</h2>
<p>Enables Cross-Origin Resource Sharing. Call before defining routes.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro app = router_banao();

cors_chharpao(app); // allow all origins (default)

// Custom options
cors_chharpao(app, {
"origin": "https://myapp.com",
"methods": "GET,POST,PUT,DELETE"
});`}
</div>

<hr />

<h2>Static File Serving (file_dao - ফাইল দাও)</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro app = router_banao();
file_dao(app, "/public", "./static_files");
// GET /public/style.css → serves ./static_files/style.css`}
</div>

<hr />

<h2>Cookie Handling</h2>
<p>Read cookies from <code>req[&quot;kukis&quot;]</code>. Set cookies with <code>kuki_rakho()</code> (কুকি রাখো).</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`app.ana("/profile", kaj(req, res) {
dhoro sessionToken = req["kukis"]["session"];
json_uttor(res, {"token": sessionToken});
});

app.pathano("/login", kaj(req, res) {
// Basic cookie
kuki_rakho(res, "session", "token123");

// With options
kuki_rakho(res, "session", "token123", {
"httpOnly": sotti,
"maxAge": 86400,
"sameSite": "Lax",
"secure": sotti
});
json_uttor(res, {"ok": sotti});
});`}
</div>

<hr />

<h2>Redirect (ghurao - ঘোরাও)</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`app.ana("/old-page", kaj(req, res) {
ghurao(res, "/new-page"); // 302 Found
});

app.ana("/moved", kaj(req, res) {
ghurao(res, "/permanent", 301); // 301 Moved Permanently
});`}
</div>

<hr />

<h2>HTML File Response (html_uttor - HTML উত্তর)</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`app.ana("/", kaj(req, res) {
html_uttor(res, "./views/index.html");
});`}
</div>

<hr />

<h2>Error Middleware (bhul_sambhalo - ভুল সামলাও)</h2>
<p>Catches errors returned by route handlers. Register after all routes.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`bhul_sambhalo(app, kaj(err, req, res) {
json_uttor(res, {"error": err["message"]}, 500);
});`}
</div>

<hr />

<h2>Performance Features</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro app = router_banao();

goti_shima(app, 100, 60); // rate limit: 100 req/min per IP (গতি সীমা)
sankochon_chalu(app); // gzip compression (সংকোচন চালু)
somoy_shima(app, 30); // 30-second timeout (সময় সীমা)
akaar_shima(app, 1048576); // 1 MB body limit (আকার সীমা)
log_chalu(app); // request logging (লগ চালু)`}
</div>

<hr />

<h2>Sub-router Mounting (bebohar - ব্যবহার)</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro userRoutes = router_banao();
userRoutes.ana("/users", kaj(req, res) { json_uttor(res, {"users": []}); });
userRoutes.pathano("/users", kaj(req, res) { json_uttor(res, {}, 201); });

dhoro app = router_banao();
app.bebohar("/api", userRoutes);
// Now: GET /api/users, POST /api/users`}
</div>

<hr />

<h2>Full Production Example</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`dhoro app = router_banao();

cors_chharpao(app);
log_chalu(app);
sankochon_chalu(app);
somoy_shima(app, 30);
akaar_shima(app, 1048576);
goti_shima(app, 100, 60);

app.majhe(kaj(req, res, agorao) {
dekho(req["method"], req["path"]);
agorao();
});

file_dao(app, "/public", "./static");

app.ana("/users/:id", kaj(req, res) {
dhoro id = req["params"]["id"];
json_uttor(res, {"id": id});
});

app.pathano("/users", kaj(req, res) {
dhoro user = req["json"];
json_uttor(res, {"created": sotti}, 201);
});

app.ana("/search", kaj(req, res) {
dhoro q = req["query"]["q"];
json_uttor(res, {"results": []});
});

bhul_sambhalo(app, kaj(err, req, res) {
json_uttor(res, {"error": err["message"]}, 500);
});

server_chalu(3000, app);`}
</div>

<hr />

<h2>Related Topics</h2>
<ul>
<li><a href="/docs/http-server">HTTP Server Basics</a></li>
Expand Down
115 changes: 114 additions & 1 deletion Documentation/app/docs/http-server/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,127 @@ dhoro data = json_poro(response.body);
dekho(data);`}
/>

<h2>Enhanced HTTP Client (anun)</h2>
<p><code>anun()</code> now supports all HTTP methods via an optional options map. Backward-compatible: one-argument GET still works.</p>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`// GET (unchanged)
dhoro res = anun("https://api.example.com/users");

// POST with JSON body
dhoro res = anun("https://api.example.com/users", {
"method": "POST",
"body": json_banao({"name": "Ankan", "email": "a@b.com"}),
"headers": {"Content-Type": "application/json"}
});

// PUT
dhoro res = anun("https://api.example.com/users/1", {
"method": "PUT",
"body": json_banao({"name": "Updated"})
});

// DELETE
dhoro res = anun("https://api.example.com/users/1", {
"method": "DELETE"
});

dekho("Status:", res["status"]);
dhoro data = json_poro(res["body"]);`}
</div>

<h2>Async HTTP Client (anun_async)</h2>
<div className="bg-secondary/50 p-4 rounded-lg font-mono text-sm whitespace-pre">
{`proyash kaj createUser(userData) {
dhoro res = opekha anun_async("https://api.example.com/users", {
"method": "POST",
"body": json_banao(userData),
"headers": {"Content-Type": "application/json"}
});
ferao json_poro(res["body"]);
}

dhoro user = opekha createUser({"name": "Ankan"});
dekho("Created:", user);`}
</div>

<h2>Request Object Reference</h2>
<p>All fields available on <code>req</code> inside route handlers:</p>
<div className="overflow-x-auto border border-border rounded-lg">
<table className="min-w-full divide-y divide-border text-sm">
<thead>
<tr className="bg-secondary/30">
<th className="px-4 py-2 text-left">Field</th>
<th className="px-4 py-2 text-left">Type</th>
<th className="px-4 py-2 text-left">Description</th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{[
["req[\"method\"]", "STRING", "HTTP method: GET, POST, PUT, ..."],
["req[\"path\"]", "STRING", "Request path: /users/123"],
["req[\"ip\"]", "STRING", "Client IP address"],
["req[\"headers\"]", "MAP", "All request headers"],
["req[\"body\"]", "STRING", "Raw request body"],
["req[\"json\"]", "MAP/NULL", "Auto-parsed JSON body (when Content-Type: application/json)"],
["req[\"form\"]", "MAP/NULL", "URL-encoded form data (when Content-Type: application/x-www-form-urlencoded)"],
["req[\"params\"]", "MAP", "Path params: route /users/:id → req[\"params\"][\"id\"]"],
["req[\"query\"]", "MAP", "Parsed query string: ?q=hi → req[\"query\"][\"q\"]"],
["req[\"query_raw\"]", "STRING", "Raw query string: \"q=hi&page=2\""],
["req[\"kukis\"]", "MAP", "Parsed cookies: req[\"kukis\"][\"session\"]"],
].map(([field, type, desc]) => (
<tr key={field}>
<td className="px-4 py-2 font-mono">{field}</td>
<td className="px-4 py-2">{type}</td>
<td className="px-4 py-2 text-muted-foreground">{desc}</td>
</tr>
))}
</tbody>
</table>
</div>

<h2>Performance Helpers Reference</h2>
<div className="overflow-x-auto border border-border rounded-lg mt-4">
<table className="min-w-full divide-y divide-border text-sm">
<thead>
<tr className="bg-secondary/30">
<th className="px-4 py-2 text-left">Function</th>
<th className="px-4 py-2 text-left">Bengali</th>
<th className="px-4 py-2 text-left">Description</th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{[
["goti_shima(app, max, window)", "গতি সীমা", "Rate limit: max requests per window seconds per IP"],
["sankochon_chalu(app)", "সংকোচন চালু", "Enable gzip compression"],
["somoy_shima(app, secs)", "সময় সীমা", "Request timeout in seconds"],
["akaar_shima(app, bytes)", "আকার সীমা", "Max request body size in bytes"],
["log_chalu(app)", "লগ চালু", "Enable request logging"],
["cors_chharpao(app, opts?)", "ছাড়পাও", "Enable CORS"],
["file_dao(app, url, dir)", "ফাইল দাও", "Serve static files"],
["ghurao(res, url, status?)", "ঘোরাও", "HTTP redirect (default 302)"],
["kuki_rakho(res, name, val, opts?)", "কুকি রাখো", "Set response cookie"],
["html_uttor(res, filepath)", "HTML উত্তর", "Serve HTML file"],
["bhul_sambhalo(app, handler)", "ভুল সামলাও", "Global error middleware"],
].map(([fn, bn, desc]) => (
<tr key={fn}>
<td className="px-4 py-2 font-mono text-xs">{fn}</td>
<td className="px-4 py-2">{bn}</td>
<td className="px-4 py-2 text-muted-foreground">{desc}</td>
</tr>
))}
</tbody>
</table>
</div>

<h2>Best Practices</h2>

<ul>
<li><strong>Always validate input</strong> - Never trust client data</li>
<li><strong>Use appropriate status codes</strong> - 200 for success, 404 for not found, etc.</li>
<li><strong>Set correct content types</strong> - Especially for JSON and HTML</li>
<li><strong>Handle errors gracefully</strong> - Wrap handlers in try-catch</li>
<li><strong>Handle errors gracefully</strong> - Use <code>bhul_sambhalo()</code> for centralized error handling</li>
<li><strong>Use JSON for APIs</strong> - It&apos;s the standard for data exchange</li>
<li><strong>Enable production features</strong> - <code>cors_chharpao</code>, <code>goti_shima</code>, <code>sankochon_chalu</code>, <code>somoy_shima</code> for production deployments</li>
</ul>

<DocNavigation currentPath="/docs/http-server" />
Expand Down
2 changes: 1 addition & 1 deletion Documentation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "documentation",
"version": "9.2.1",
"version": "9.3.0",
"private": true,
"scripts": {
"dev": "next dev --turbopack",
Expand Down
2 changes: 1 addition & 1 deletion Extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "banglacode",
"displayName": "BanglaCode",
"description": "Language support for BanglaCode (.bang, .bangla, .bong) - Bengali Programming Language created by Ankan from West Bengal, India",
"version": "9.2.1",
"version": "9.3.0",
"publisher": "AnkanSaha",
"author": {
"name": "AnkanSaha"
Expand Down
Loading
Loading