From cfffea0a89039e1b85652c67f9af74b6d1464fdd Mon Sep 17 00:00:00 2001 From: Ankan Saha Date: Thu, 11 Jun 2026 13:48:02 +0530 Subject: [PATCH] feat(http): add HTTP router and utility functions with Bangla support - Implemented `router_banao` to create a new router with various HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS) using Bangla keywords. - Added middleware support with `majhe` for handling requests. - Introduced `bebohar` for mounting sub-routers. - Implemented CORS support with `cors_chharpao` for handling cross-origin requests. - Added static file serving with `file_dao`. - Implemented redirection with `ghurao` and cookie management with `kuki_rakho`. - Added HTML response serving with `html_uttor`. - Introduced logging support with `log_chalu`. - Enhanced tests for new features including path parameters, query parameters, middleware, CORS, static files, redirection, cookies, and full production stack. - Updated version to 9.3.0. --- .gemini/settings.json | 16 - Documentation/app/docs/http-routing/page.tsx | 223 ++++++ Documentation/app/docs/http-server/page.tsx | 115 +++- Documentation/package.json | 2 +- Extension/package.json | 2 +- Extension/snippets/banglacode.json | 167 +++++ Extension/syntaxes/banglacode.tmLanguage.json | 2 +- GEMINI.md | 178 ----- SYNTAX.md | 55 +- VERSION | 2 +- main.go | 4 +- src/evaluator/builtins/builtins_http.go | 231 +++---- .../builtins/builtins_http_advanced.go | 135 ++++ .../builtins/builtins_http_router.go | 635 ++++++++++-------- .../builtins/builtins_http_router_builtins.go | 159 +++++ src/evaluator/builtins/builtins_http_utils.go | 244 +++++++ src/repl/repl.go | 2 +- test/http_router_test.go | 320 +++++++++ 18 files changed, 1874 insertions(+), 618 deletions(-) delete mode 100644 .gemini/settings.json delete mode 100644 GEMINI.md create mode 100644 src/evaluator/builtins/builtins_http_advanced.go create mode 100644 src/evaluator/builtins/builtins_http_router_builtins.go create mode 100644 src/evaluator/builtins/builtins_http_utils.go diff --git a/.gemini/settings.json b/.gemini/settings.json deleted file mode 100644 index 9bd4773..0000000 --- a/.gemini/settings.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "version": "1.0", - "project": { - "name": "BanglaCode", - "type": "Programming Language Interpreter", - "language": "Go", - "runtime": "Go ≥1.20" - }, - "context": { - "fileName": ["GEMINI.md", "SYNTAX.md"], - "hierarchical": true - }, - "tools": { - "enabled": ["codeSearch", "fileOperations", "shellCommands"] - } -} diff --git a/Documentation/app/docs/http-routing/page.tsx b/Documentation/app/docs/http-routing/page.tsx index 6226d15..3605e86 100644 --- a/Documentation/app/docs/http-routing/page.tsx +++ b/Documentation/app/docs/http-routing/page.tsx @@ -426,6 +426,229 @@ pathao authRouter; // Always export your router`}
+

Path Parameters

+

Use :name segments in route paths. Values are available in req["params"] as a MAP.

+
+{`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}); +});`} +
+ +
+ +

Query String Parsing

+

req["query"] is a parsed MAP. Use req["query_raw"] for the raw string.

+
+{`// 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}); +});`} +
+ +
+ +

Auto JSON Body Parsing

+

When the request has Content-Type: application/json, req["json"] is auto-parsed. Otherwise it is khali.

+
+{`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); +});`} +
+ +
+ +

Middleware (majhe - মাঝে)

+

Runs before every route handler. Call agorao() (আগাও = go forward) to pass to the next layer.

+
+{`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"); +});`} +
+ +
+ +

CORS (cors_chharpao - ছাড়পাও)

+

Enables Cross-Origin Resource Sharing. Call before defining routes.

+
+{`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" +});`} +
+ +
+ +

Static File Serving (file_dao - ফাইল দাও)

+
+{`dhoro app = router_banao(); +file_dao(app, "/public", "./static_files"); +// GET /public/style.css → serves ./static_files/style.css`} +
+ +
+ +

Cookie Handling

+

Read cookies from req["kukis"]. Set cookies with kuki_rakho() (কুকি রাখো).

+
+{`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}); +});`} +
+ +
+ +

Redirect (ghurao - ঘোরাও)

+
+{`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 +});`} +
+ +
+ +

HTML File Response (html_uttor - HTML উত্তর)

+
+{`app.ana("/", kaj(req, res) { + html_uttor(res, "./views/index.html"); +});`} +
+ +
+ +

Error Middleware (bhul_sambhalo - ভুল সামলাও)

+

Catches errors returned by route handlers. Register after all routes.

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

Performance Features

+
+{`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 (লগ চালু)`} +
+ +
+ +

Sub-router Mounting (bebohar - ব্যবহার)

+
+{`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`} +
+ +
+ +

Full Production Example

+
+{`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);`} +
+ +
+

Related Topics