~genbyte/retstat

746035cca90dcdd72feb3210d5ae16a72c275765 — Brad Alfirevic 14 days ago 081e106 master
Rewrite with tiny_http and default phrase in body
5 files changed, 119 insertions(+), 403 deletions(-)

M Cargo.lock
M Cargo.toml
M README.md
D Rocket.toml
M src/main.rs
M Cargo.lock => Cargo.lock +44 -360
@@ 1,15 1,10 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "atty"
version = "0.2.14"
name = "ascii"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
 "hermit-abi",
 "libc",
 "winapi",
]
checksum = "bbf56136a5198c7b01a49e3afcbef6cf84597273d298f54432926024107b0109"

[[package]]
name = "autocfg"


@@ 18,137 13,42 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"

[[package]]
name = "base64"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643"
dependencies = [
 "byteorder",
 "safemem",
]

[[package]]
name = "base64"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
dependencies = [
 "byteorder",
]

[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"

[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"

[[package]]
name = "cc"
version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "404b1fe4f65288577753b17e3b36a04596ee784493ec249bf81c7f2d2acd751c"

[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"

[[package]]
name = "cookie"
version = "0.11.2"
name = "chrono"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9fac5e7bdefb6160fb181ee0eaa6f96704b625c70e6d61c465cb35750a4ea12"
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
dependencies = [
 "base64 0.9.3",
 "ring",
 "num-integer",
 "num-traits",
 "time",
 "url",
]

[[package]]
name = "devise"
version = "0.2.0"
name = "chunked_transfer"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74e04ba2d03c5fa0d954c061fc8c9c288badadffc272ebb87679a89846de3ed3"
dependencies = [
 "devise_codegen",
 "devise_core",
]
checksum = "1d29eb15132782371f71da8f947dba48b3717bdb6fa771b9b434d645e40a7193"

[[package]]
name = "devise_codegen"
version = "0.2.0"
name = "getopts"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "066ceb7928ca93a9bedc6d0e612a8a0424048b0ab1f75971b203d01420c055d7"
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
dependencies = [
 "devise_core",
 "quote",
]

[[package]]
name = "devise_core"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf41c59b22b5e3ec0ea55c7847e5f358d340f3a8d6d53a5cf4f1564967f96487"
dependencies = [
 "bitflags",
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"

[[package]]
name = "hermit-abi"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4"
dependencies = [
 "libc",
]

[[package]]
name = "httparse"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"

[[package]]
name = "hyper"
version = "0.10.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a0652d9a2609a968c14be1a9ea00bf4b1d64e2e1f53a1b51b6fff3a6e829273"
dependencies = [
 "base64 0.9.3",
 "httparse",
 "language-tags",
 "log 0.3.9",
 "mime",
 "num_cpus",
 "time",
 "traitobject",
 "typeable",
 "unicase",
 "url",
 "unicode-width",
]

[[package]]
name = "idna"
version = "0.1.5"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e"
checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
dependencies = [
 "matches",
 "unicode-bidi",


@@ 156,27 56,6 @@ dependencies = [
]

[[package]]
name = "indexmap"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "076f042c5b7b98f31d205f1249267e12a6518c1481e9dae9764af19b707d2292"
dependencies = [
 "autocfg",
]

[[package]]
name = "language-tags"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"

[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"

[[package]]
name = "libc"
version = "0.2.70"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 184,15 63,6 @@ checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f"

[[package]]
name = "log"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
dependencies = [
 "log 0.4.8",
]

[[package]]
name = "log"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"


@@ 207,184 77,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"

[[package]]
name = "memchr"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"

[[package]]
name = "mime"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0"
dependencies = [
 "log 0.3.9",
]

[[package]]
name = "num_cpus"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
dependencies = [
 "hermit-abi",
 "libc",
]

[[package]]
name = "pear"
version = "0.1.2"
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c26d2b92e47063ffce70d3e3b1bd097af121a9e0db07ca38a6cc1cf0cc85ff25"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [
 "pear_codegen",
 "autocfg",
 "num-traits",
]

[[package]]
name = "pear_codegen"
version = "0.1.2"
name = "num-traits"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "336db4a192cc7f54efeb0c4e11a9245394824cc3bcbd37ba3ff51240c35d7a6e"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
 "version_check 0.1.5",
 "yansi 0.4.0",
 "autocfg",
]

[[package]]
name = "percent-encoding"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"

[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
dependencies = [
 "unicode-xid",
]

[[package]]
name = "quote"
version = "0.6.13"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
dependencies = [
 "proc-macro2",
]
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"

[[package]]
name = "retstat"
version = "1.0.1"
version = "1.1.0"
dependencies = [
 "rocket",
 "getopts",
 "tiny_http",
]

[[package]]
name = "ring"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c4db68a2e35f3497146b7e4563df7d4773a2433230c5e4b448328e31740458a"
dependencies = [
 "cc",
 "lazy_static",
 "libc",
 "untrusted",
]

[[package]]
name = "rocket"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e20afbad214b001cabbe31dd270b48b3be980a7153ee2ed8392e241f856d651b"
dependencies = [
 "atty",
 "base64 0.10.1",
 "log 0.4.8",
 "memchr",
 "num_cpus",
 "pear",
 "rocket_codegen",
 "rocket_http",
 "state",
 "time",
 "toml",
 "version_check 0.9.1",
 "yansi 0.5.0",
]

[[package]]
name = "rocket_codegen"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2108b35e2c3a35759d3f16cc3002ece05523191d884d3ad6523693fd43324dde"
dependencies = [
 "devise",
 "glob",
 "indexmap",
 "quote",
 "rocket_http",
 "version_check 0.9.1",
 "yansi 0.5.0",
]

[[package]]
name = "rocket_http"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce8ca76247376ea21cf271af0f95e3f2014596e3e4c7cc04e44ee6242a40ff2"
dependencies = [
 "cookie",
 "hyper",
 "indexmap",
 "pear",
 "percent-encoding",
 "smallvec",
 "state",
 "time",
 "unicode-xid",
]

[[package]]
name = "safemem"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"

[[package]]
name = "serde"
version = "1.0.110"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99e7b308464d16b56eba9964e4972a3eee817760ab60d88c3f86e1fecb08204c"

[[package]]
name = "smallvec"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4"

[[package]]
name = "state"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7345c971d1ef21ffdbd103a75990a15eb03604fc8b8852ca8cb418ee1a099028"

[[package]]
name = "syn"
version = "0.15.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-xid",
]

[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 395,33 126,16 @@ dependencies = [
]

[[package]]
name = "toml"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f"
dependencies = [
 "serde",
]

[[package]]
name = "traitobject"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"

[[package]]
name = "typeable"
version = "0.1.2"
name = "tiny_http"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887"

[[package]]
name = "unicase"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33"
checksum = "15ce4fc3c4cdea1a4399bb1819a539195fb69db4bbe0bde5b7c7f18fed412e02"
dependencies = [
 "version_check 0.1.5",
 "ascii",
 "chrono",
 "chunked_transfer",
 "log",
 "url",
]

[[package]]


@@ 443,22 157,16 @@ dependencies = [
]

[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"

[[package]]
name = "untrusted"
version = "0.6.2"
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cd1f4b4e96b46aeb8d4855db4a7a9bd96eeeb5c6a1ab54593328761642ce2f"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"

[[package]]
name = "url"
version = "1.7.2"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb"
dependencies = [
 "idna",
 "matches",


@@ 466,18 174,6 @@ dependencies = [
]

[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"

[[package]]
name = "version_check"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"

[[package]]
name = "winapi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 498,15 194,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

[[package]]
name = "yansi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d60c3b48c9cdec42fb06b3b84b5b087405e1fa1c644a1af3930e4dfafe93de48"

[[package]]
name = "yansi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fc79f4a1e39857fc00c3f662cbf2651c771f00e9c15fe2abc341806bd46bd71"

M Cargo.toml => Cargo.toml +3 -2
@@ 1,6 1,6 @@
[package]
name = "retstat"
version = "1.0.1"
version = "1.1.0"
description = "HTTP Server returning the status in the URL"
authors = ["Brad Alfirevic <brad@genbyte.dev>"]
repository = "https://git.sr.ht/~genbyte/retstat"


@@ 11,4 11,5 @@ license = "ISC"
edition = "2018"

[dependencies]
rocket = "0.4.4"
tiny_http = "0.7.0"
getopts = "0.2"

M README.md => README.md +12 -13
@@ 5,11 5,14 @@
[todo]: https://todo.sr.ht/~genbyte/retstat
[crates]: https://crates.io/crates/retstat

This program will **ret**urn the **stat**us code you provide it in the URL as
the body of the response while also using that as the status code.
This program will **ret**urn the **stat**us code to you. That is, in its HTTP
response, the status code you pass in the URL will be used. The body of the
response is the default phrasing for that code. If you pass a code that is
unknown, "Unknown" will be the body.

retsta was born out of the desire to have a reliable source of status codes so I
could test my shell scripts without hitting someone else's server so many times.
retstat was born out of the desire to have a reliable source of status codes so
I could test my shell scripts without hitting someone else's server so many
times.

I have it live at <https://genbyte.dev/status/>, so some examples:



@@ 17,18 20,14 @@ I have it live at <https://genbyte.dev/status/>, so some examples:
- <https://genbyte.dev/status/500> returns Status 500

There is a very basic service file for systemd in the root of the source tree.
It assumes the bin is at `/usr/bin/retstat` and the Rocket.toml file lives in the
`/etc/retstat` directory.

I'd like to move away from Rocket eventually, as it's needlessly (for this
purpose) heavy. Ideally I'll use something like tiny_http, but I wanted the
threading that comes with Rocket by default.
It assumes the bin is at `/usr/bin/retstat`.

### Configuration
Configuration is controlled through the [`Rocket.toml`](Rocket.toml) file. There
you can change the port and address that the server runs on.
There isn't much configuration available, as this small server doesn't do much.
You can change the listening address with the `-l` cli option. The long option
is `--host`, if you want to use that.

retstat defaults to only running on `localhost`, port `30210`.
retstat defaults to listening on `localhost:30210`.

Below is the recommended configuration for Nginx.
Thanks to [this][so] answer which taught me how to remove the path the proxy

D Rocket.toml => Rocket.toml +0 -3
@@ 1,3 0,0 @@
[global]
address = "localhost"
port = 30210

M src/main.rs => src/main.rs +60 -25
@@ 1,36 1,71 @@
#![feature(proc_macro_hygiene, decl_macro)]
use getopts::Options;
use std::env;
use tiny_http::{Server, StatusCode, Response};

#[macro_use] extern crate rocket;
use std::io::Cursor;
use rocket::http::Status;
use rocket::{Request, Response};
struct CliArgs {
    host: String
}

#[get("/<stat>")]
fn status<'a>(stat: u16) -> Response<'a> {
    let mut resp = Response::new();
fn print_usage(program: &str, opts: Options) {
    let usage = format!("Usage: {} [options]", program);
    print!("{}", opts.usage(&usage));
}

    match Status::from_code(stat) {
        Some(status) => resp.set_status(status),
        None => resp.set_status(Status::NotFound)
    }
fn parse_arguments() -> Option<CliArgs> {
    let args: Vec<String> = env::args().collect();
    let program = args[0].clone();

    resp.set_sized_body(Cursor::new(format!("{}", resp.status().code)));
    let mut opts = Options::new();
    opts.optopt("l", "host", "where the server should listen.\nDefault localhost:30210", "HOST");
    opts.optflag("h", "help", "print this message and exit");
    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(e) => {
            println!("{}", e.to_string());
            print_usage(&program, opts);
            return None;
        }
    };

    resp
}
    if matches.opt_present("h") {
        print_usage(&program, opts);
        return None;
    }

#[catch(404)]
fn not_found<'a>(_req: &Request) -> Response<'a> {
    let mut resp = Response::new();
    resp.set_status(Status::NotFound);
    resp.set_sized_body(Cursor::new("404"));
    let host = match matches.opt_str("l") {
        Some(l) => l,
        None => "localhost:30210".to_owned()
    };

    resp
    Some(CliArgs {
        host
    })
}

fn main() {
    rocket::ignite()
        .mount("/", routes![status])
        .register(catchers![not_found])
        .launch();
    let args = match parse_arguments() {
        Some(a) => a,
        None => return
    };

    let server = match Server::http(args.host) {
        Ok(s) => s,
        Err(e) => {
            println!("Could not start server: {}", e);
            return;
        }
    };

    for request in server.incoming_requests() {
        // Drop the slash on the url
        let url = &request.url()[1..];
        let requested_status = match url.parse::<u16>() {
            Ok(n) => n,
            Err(_) => 404
        };

        let status_code = StatusCode::from(requested_status);
        let response = Response::from_string(status_code.default_reason_phrase()).with_status_code(status_code);
        request.respond(response).ok();
    }
}