All Projects → amandasaurus → rust-cgi

amandasaurus / rust-cgi

Licence: AGPL-3.0 License
Create CGI programmes in Rust with hyper's http types

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to rust-cgi

Custom-Software-For-Xiaomi-Dafang
API and panel site for Xiaomi Dafang
Stars: ✭ 36 (+28.57%)
Mutual labels:  cgi
quickserv
Dangerously user-friendly web server for quick prototyping and hackathons
Stars: ✭ 275 (+882.14%)
Mutual labels:  cgi
markdownshare.com
The code which was previously used at http://markdownshare.com/
Stars: ✭ 29 (+3.57%)
Mutual labels:  cgi
chronicle2
Chronicle is a simple blog compiler, written in Perl with minimal dependencies.
Stars: ✭ 19 (-32.14%)
Mutual labels:  cgi
bookmarks
A PySide2 based file and asset manager for animation and CG productions.
Stars: ✭ 33 (+17.86%)
Mutual labels:  cgi
docker-cgi-python
🐳Docker file for cgi using python2.7, 3.6, 3.7, 3.8, 3.9 and 3.10🐍
Stars: ✭ 13 (-53.57%)
Mutual labels:  cgi
trusted-cgi
Lightweight runner for lambda functions/apps in CGI like mode
Stars: ✭ 150 (+435.71%)
Mutual labels:  cgi
go-dcgi
CGI, but with Docker containers
Stars: ✭ 12 (-57.14%)
Mutual labels:  cgi
php2python
Convert PHP code to Python under CGI (beta)
Stars: ✭ 44 (+57.14%)
Mutual labels:  cgi
fano
Pascal web application framework
Stars: ✭ 21 (-25%)
Mutual labels:  cgi
googleDriveVFXServer-pipeline
Transform a Google Drive server into a VFX pipeline ready server
Stars: ✭ 15 (-46.43%)
Mutual labels:  cgi

rust-cgi

Easily create CGI[1][2] programmes[3] in Rust based on http types.

crates.io released version badge
crates.io released licencegpl

Installation & Usage

Cargo.toml:

[dependencies]
cgi = "0.6"

Use the cgi_main! macro, with a function that takes a cgi::Request and returns a cgi::Response.

extern crate cgi;

cgi::cgi_main! { |request: cgi::Request| -> cgi::Response {
     cgi::text_response(200, "Hello World")
} }

If your function returns a Result, you can use cgi_try_main!:

extern crate cgi;

cgi::cgi_try_main! { |request: cgi::Request| -> Result<cgi::Response, String> {
    let greeting = std::fs::read_to_string("greeting.txt").map_err(|_| "Couldn't open file")?;

    Ok(cgi::text_response(200, greeting))
} }

It will parse & extract the CGI environmental variables, and the HTTP request body to create Request<u8>, call your function to create a response, and convert your Response into the correct format and print to stdout. If this programme is not called as CGI (e.g. missing required environmental variables), it will panic.

It is also possible to call the cgi::handle function directly inside your main function:

extern crate cgi;

fn main() { cgi::handle(|request: cgi::Request| -> cgi::Response {
    cgi::html_response(200, "<html><body><h1>Hello World!</h1></body></html>")
})}

Response Shortcuts

Several shortcuts create shortcuts easily:

cgi:empty_response(status_code)

A HTTP Reponse with no body and that HTTP status code, e.g. return cgi::empty_response(404); to return a HTTP 404 Not Found.

cgi::html_response(status_code, text)

Converts text to bytes (UTF8) and sends that as the body with that status_code and HTML Content-Type header.

cgi::string_response(status_code, text)

Converts text to bytes (UTF8), and sends that as the body with that status_code, e.g. `return

cgi::string_response(200, "Hello World!")

returns a simple plain text response.

cgi::binary_response(status_code, blob)

Sends blob with that status code.

Re-exports

http is re-exported, (as cgi::http).

cgi::Response/Request are http::Response<Vec<u8>>/Request<Vec<u8>>.

Running locally

Python provides a simple CGI webserver you can use to run your scripts. The binaries must be in a cgi-bin directory, so you’ll need to create that directory and copy your binary into it. Given a project named example, run this in your project root directory (i.e. where Cargo.toml is):

mkdir cgi-bin
cargo build
cp target/debug/example cgi-bin/example
python3 -m http.server --cgi

See also

Things using this

  • 'Suggestions welcome!'

Why?

CGI is old, and easy to deploy. Just drop a binary in the right place, and Apache (or whatever) will serve it up. Rust is fast, so for simple things, there should be less downsides to spinning up a custom HTTP server.

Copyright

Copyright GNU Affero GPL v3 (or later). See the file LICENCE


1. Retro!
2. Common Gateway Interface 1.1, RFC 3875
3. Yes, I’m spelling it programme, the correct way.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].