Last active
November 16, 2022 20:28
-
-
Save gdamjan/cfef32a09ef4e3b17f6516a1a831c896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust-cgi" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
hyper_cgi= "22.4.15" | |
hyper = "0.14.23" | |
tokio = "1.21.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{convert::Infallible, net::SocketAddr}; | |
use hyper::{Body, Request, Response, Server}; | |
use hyper::service::{make_service_fn, service_fn}; | |
use hyper_cgi::do_cgi; | |
async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> { | |
let cmd = tokio::process::Command::new("./test.sh"); | |
let (response, stderr) = hyper_cgi::do_cgi(req, cmd).await; | |
if stderr.len() > 0 { | |
let s = String::from_utf8_lossy(&stderr); | |
eprintln!("cgi-error: {}", s); | |
} | |
Ok(response) | |
} | |
#[tokio::main] | |
async fn main() { | |
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | |
let make_svc = make_service_fn(|_conn| async { | |
Ok::<_, Infallible>(service_fn(handle)) | |
}); | |
let server = Server::bind(&addr).serve(make_svc); | |
if let Err(e) = server.await { | |
eprintln!("server error: {}", e); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# mode: 0755 | |
env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment