{"id":18606721,"url":"https://github.com/amandasaurus/rust-cgi","last_synced_at":"2025-04-04T15:06:40.549Z","repository":{"id":50466739,"uuid":"132510033","full_name":"amandasaurus/rust-cgi","owner":"amandasaurus","description":"Create CGI programmes in Rust with hyper's http types","archived":false,"fork":false,"pushed_at":"2025-02-16T16:27:41.000Z","size":58,"stargazers_count":56,"open_issues_count":3,"forks_count":14,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T14:09:26.337Z","etag":null,"topics":["cgi","http","rust"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amandasaurus.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,"publiccode":null,"codemeta":null}},"created_at":"2018-05-07T19:56:28.000Z","updated_at":"2025-03-23T06:28:42.000Z","dependencies_parsed_at":"2025-03-13T18:31:00.669Z","dependency_job_id":null,"html_url":"https://github.com/amandasaurus/rust-cgi","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amandasaurus%2Frust-cgi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amandasaurus%2Frust-cgi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amandasaurus%2Frust-cgi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amandasaurus%2Frust-cgi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amandasaurus","download_url":"https://codeload.github.com/amandasaurus/rust-cgi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198449,"owners_count":20900079,"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":["cgi","http","rust"],"created_at":"2024-11-07T02:26:50.523Z","updated_at":"2025-04-04T15:06:40.516Z","avatar_url":"https://github.com/amandasaurus.png","language":"Rust","readme":"cgi\n===\n[![Crate](https://img.shields.io/crates/v/cgi.svg)](https://crates.io/crates/cgi)\n[![License](https://img.shields.io/crates/l/cgi.svg)](LICENSE)\n\nEasily create CGI (Common Gateway Interface) programs in Rust, based on\n[`http`](https://crates.io/crates/http) types.\n\nInstallation \u0026 Usage\n--------------------\n\n`Cargo.toml`:\n\n```toml\n[dependencies]\ncgi = \"0.6\"\n```\n\nUse the `cgi_main!` macro, with a function that takes a `cgi::Request` and returns a\n`cgi::Response`.\n\n```rust\nextern crate cgi;\n\ncgi::cgi_main! { |request: cgi::Request| -\u003e cgi::Response {\n     cgi::text_response(200, \"Hello World\")\n} }\n```\n\nIf your function returns a `Result`, you can use `cgi_try_main!`:\n\n```rust\nextern crate cgi;\n\ncgi::cgi_try_main! { |request: cgi::Request| -\u003e Result\u003ccgi::Response, String\u003e {\n    let greeting = std::fs::read_to_string(\"greeting.txt\").map_err(|_| \"Couldn't open file\")?;\n\n    Ok(cgi::text_response(200, greeting))\n} }\n```\n\nIt will parse and extract the CGI environmental variables, and the HTTP request body to create\n`Request\u003cu8\u003e`, call your function to create a response, and convert your `Response` into the\ncorrect format and print to stdout. If this program is not called as CGI (e.g. missing\nrequired environmental variables), it will gracefully fall back to using reasonable values\n(although the values themselves may be subject to change).\n\nIt is also possible to call the `cgi::handle` function directly inside your `main` function:\n\n```rust\nextern crate cgi;\n\nfn main() { cgi::handle(|request: cgi::Request| -\u003e cgi::Response {\n    cgi::html_response(200, \"\u003chtml\u003e\u003cbody\u003e\u003ch1\u003eHello World!\u003c/h1\u003e\u003c/body\u003e\u003c/html\u003e\")\n})}\n```\n\nResponse Shortcuts\n------------------\n\nSeveral shortcuts create shortcuts easily:\n\n- `cgi:empty_response(status_code)` - A HTTP Reponse with no body and that HTTP\nstatus code, e.g. `return rust_igi::empty_response(404);` to return a\n[HTTP 404 Not Found](https://en.wikipedia.org/wiki/HTTP_404).\n\n- `cgi::html_response(status_code, text)` - Converts `text` to bytes (UTF8) and\nsends that as the body with that `status_code` and HTML `Content-Type` header.\n\n- `cgi::string_response(status_code, text)` - Converts `text` to bytes (UTF8),\nand sends that as the body with that `status_code` but no `Content-Type` header.\n\n- `cgi::binary_response(status_code, content_type, blob)` - Sends `blob` with\nthat status code and the provided content type header.\n\nRe-exports\n----------\n\n`http` is re-exported, (as `cgi::http`).\n\n`cgi::Response`/`Request` are `http::Response\u003cVec\u003cu8\u003e\u003e`/`Request\u003cVec\u003cu8\u003e\u003e`.\n\nRunning locally\n---------------\n\nPython provides a simple CGI webserver you can use to run your scripts. The\nbinaries must be in a `cgi-bin` directory, so you'll need to create that\ndirectory and copy your binary into it. Given a project named `example`, run\nthis in your project root directory (i.e. where `Cargo.toml` is):\n\n```shell\nmkdir cgi-bin\ncargo build\ncp target/debug/example cgi-bin/example\npython3 -m http.server --cgi\n```\n\nand then open http://localhost:8000/cgi-bin/example.\n\nMSRV policy\n-----------\n\nCurrently the minimum supported Rust version (MSRV) is 1.51.0.\nMSRV increases will be kept to a minimum, and will always be accompanied with a minor version bump.\n\nSee also\n--------\n\n- [Rustdoc for this crate](https://docs.rs/cgi/latest/cgi/)\n- [http crate](https://github.com/hyperium/http)\n- [RFC 3875 - The Common Gateway Interface (CGI) v1.1](https://tools.ietf.org/html/rfc3875)\n\nWhy?\n----\n\nCGI is old, and easy to deploy. Just drop a binary in the right place, and\nApache (or whatever) will serve it up. Rust is fast, so for simple things,\nthere should be less downsides to spinning up a custom HTTP server.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famandasaurus%2Frust-cgi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famandasaurus%2Frust-cgi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famandasaurus%2Frust-cgi/lists"}