{"id":22687447,"url":"https://github.com/cablehead/http-sh","last_synced_at":"2025-04-12T20:42:40.643Z","repository":{"id":139658783,"uuid":"528207226","full_name":"cablehead/http-sh","owner":"cablehead","description":"Attach Bash scripts to a HTTP interface","archived":false,"fork":false,"pushed_at":"2025-01-31T08:07:13.000Z","size":91,"stargazers_count":6,"open_issues_count":15,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T22:54:20.611Z","etag":null,"topics":["cli","http-server","rust"],"latest_commit_sha":null,"homepage":"https://ndyg.cross.stream/projects/chat-app","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cablehead.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-08-24T00:25:03.000Z","updated_at":"2025-01-31T08:07:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe83b1ca-91b6-40ed-9392-0da5ce4b7bd9","html_url":"https://github.com/cablehead/http-sh","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cablehead%2Fhttp-sh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cablehead%2Fhttp-sh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cablehead%2Fhttp-sh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cablehead%2Fhttp-sh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cablehead","download_url":"https://codeload.github.com/cablehead/http-sh/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248249652,"owners_count":21072413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","http-server","rust"],"created_at":"2024-12-09T23:18:28.346Z","updated_at":"2025-04-12T20:42:40.635Z","avatar_url":"https://github.com/cablehead.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# http-sh [![CI](https://github.com/cablehead/http-sh/actions/workflows/ci.yml/badge.svg)](https://github.com/cablehead/http-sh/actions/workflows/ci.yml)\n\n`http-sh` lets you attach a Bash scripts to an HTTP interface. If you prefer\n[Nushell](https://www.nushell.sh) to POSIX, this project has a cousin called\n[http-nu](https://github.com/cablehead/http-nu).\n\n## Install\n\n```bash\ncargo install http-sh --locked\n```\n\n## Live examples\n\n- [`chat-app`](https://ndyg.cross.stream/projects/chat-app)\n\n## Overview\n\n### GET: Hello world\n\n```bash\n$ http-sh :3001 -- echo Hello world\n$ curl -s localhost:3001\nHello world\n```\n\nYou can listen to UNIX domain sockets as well\n\n```bash\n$ http-sh ./sock -- echo Hello world\n$ curl -s --unix-socket ./sock localhost\nHello world\n```\n\n### POST: echo\n\n```bash\n$ http-sh :3001 -- cat\n$ curl -s -d Hai localhost:3001\nHai\n```\n\n### Request metadata\n\nThe Request metadata is available as JSON on file descriptor 3.\n\nPairs well with [`jq`](https://github.com/stedolan/jq)\n\n```bash\n$ http-sh :3001 -- bash -c 'jq \u003c\u00263'\n$ curl -s localhost:3001\n{\n  \"headers\": {\n    \"accept\": \"*/*\",\n    \"host\": \"localhost:3001\",\n    \"user-agent\": \"curl/7.79.1\"\n  },\n  \"method\": \"GET\",\n  \"path\": \"/\",\n  \"proto\": \"HTTP/1.1\",\n  \"query\": {},\n  \"remote_ip\": \"127.0.0.1\",\n  \"remote_port\": 51435,\n  \"request_id\": \"0391ND23LWW4KVCZ00G30BZAG\",\n  \"uri\": \"/\"\n}\n\n$ http-sh :3001 -- bash -c 'echo hello: $(jq -r .path \u003c\u00263)'\n$ curl -s localhost:3001/yello\nhello: /yello\n```\n\n### Response metadata\n\nYou can set the Response metadata by writing JSON on file descriptor 4.\nCurrently you can set the Response `status` and `headers`.\n\nPairs well with [`jo`](https://github.com/jpmens/jo)\n\n```\n$ http-sh :3001 -- bash -c 'jo status=404 \u003e\u00264; echo sorry, eh'\n$ curl -si localhost:3001\nHTTP/1.1 404 Not Found\ncontent-type: text/plain\ntransfer-encoding: chunked\ndate: Sat, 25 Feb 2023 05:20:48 GMT\n\nsorry, eh\n```\n\nNote, for streaming responses, you'll want to close fd 4, so the Response is\ninitiated.\n\n```\n$ http-sh :3001 -- bash -c 'exec 4\u003e\u0026-; while true ; do date; sleep 1; done'\n$ curl -s localhost:3001\nSat Feb 25 00:31:41 EST 2023\nSat Feb 25 00:31:43 EST 2023\nSat Feb 25 00:31:44 EST 2023\nSat Feb 25 00:31:45 EST 2023\nSat Feb 25 00:31:46 EST 2023\n...\n```\n\n### [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)\n\nPairs well with [`xcat`](https://github.com/cablehead/xcat)\n\n```bash\n$ http-sh :3001 -- bash -c '\n    jo headers=\"$(jo \"content-type\"=\"text/event-stream\")\" \u003e\u00264\n    exec 4\u003e\u0026-\n    tail -F source.json | xcat -- bash -c \"sed '\\''s/^/data: /g'\\''; echo;\"\n'\n\n# simulate generating events in a seperate process\n$ while true; do jo date=\"$(date)\" ; sleep 1 ; done \u003e\u003e source.json\n\n$ curl -si localhost:3001/\nHTTP/1.1 200 OK\ncontent-type: text/event-stream\ntransfer-encoding: chunked\ndate: Sat, 25 Feb 2023 18:13:37 GMT\n\ndata: {\"date\":\"Sat Feb 25 13:13:35 EST 2023\"}\n\ndata: {\"date\":\"Sat Feb 25 13:13:36 EST 2023\"}\n\ndata: {\"date\":\"Sat Feb 25 13:13:37 EST 2023\"}\n\ndata: {\"date\":\"Sat Feb 25 13:13:38 EST 2023\"}\n\ndata: {\"date\":\"Sat Feb 25 13:13:39 EST 2023\"}\n\n...\n```\n\n## Direct Testing of Script\n\nWhile `http-sh` provides a convenient way to serve HTTP requests and interact with the associated metadata, there might be times when you wish to directly test the script you intend to use with `http-sh` without the HTTP layer.\n\nTo simulate the environment in which `http-sh` invokes your script, you can use the following command:\n\n```bash\necho \"Hai\" | ./root.sh 3\u003c/tmp/req.json 4\u003e\u00261\n```\n\nHere's a breakdown of what's happening:\n\n1. `echo \"Hai\"`: Simulates sending the request body.\n2. `./root.sh`: Your script that processes the input.\n3. `3\u003c/tmp/req.json`: Mimics the JSON metadata that `http-sh` provides on file descriptor 3. To obtain this JSON metadata for testing, check the log line of a specific request from the `http-sh` logs, which are structured in JSON format.\n4. `4\u003e\u00261`: Redirects output from file descriptor 4 to stdout, so you can see the response metadata and main response together in the terminal.\n\nThis method allows you to bypass `http-sh` during development and testing phases, ensuring that your script behaves as expected before integrating it with the HTTP server.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcablehead%2Fhttp-sh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcablehead%2Fhttp-sh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcablehead%2Fhttp-sh/lists"}