diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-14 23:17:26 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-14 23:17:26 -0700 |
commit | 2c56f2b65bcf3b3f85db9d2a34501154c9c9404d (patch) | |
tree | d9580fb8578f6e1ea0f47dd1e6402ebd41d3a0e9 /rust/fatcat-api/examples | |
parent | e468707ee53f4a8ff75b00a1eec44921672464b6 (diff) | |
download | fatcat-2c56f2b65bcf3b3f85db9d2a34501154c9c9404d.tar.gz fatcat-2c56f2b65bcf3b3f85db9d2a34501154c9c9404d.zip |
regenerated and pseudo-integrated
Diffstat (limited to 'rust/fatcat-api/examples')
-rw-r--r-- | rust/fatcat-api/examples/ca.pem | 17 | ||||
-rw-r--r-- | rust/fatcat-api/examples/client.rs | 350 | ||||
-rw-r--r-- | rust/fatcat-api/examples/server-chain.pem | 66 | ||||
-rw-r--r-- | rust/fatcat-api/examples/server-key.pem | 28 | ||||
-rw-r--r-- | rust/fatcat-api/examples/server.rs | 78 | ||||
-rw-r--r-- | rust/fatcat-api/examples/server_lib/mod.rs | 26 | ||||
-rw-r--r-- | rust/fatcat-api/examples/server_lib/server.rs | 287 |
7 files changed, 852 insertions, 0 deletions
diff --git a/rust/fatcat-api/examples/ca.pem b/rust/fatcat-api/examples/ca.pem new file mode 100644 index 00000000..d2317fb5 --- /dev/null +++ b/rust/fatcat-api/examples/ca.pem @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICtjCCAZ4CCQDpKecRERZ0xDANBgkqhkiG9w0BAQsFADAdMQswCQYDVQQGEwJV +UzEOMAwGA1UEAxMFTXkgQ0EwHhcNMTcwNTIzMTYwMDIzWhcNMTcwNjIyMTYwMDIz +WjAdMQswCQYDVQQGEwJVUzEOMAwGA1UEAxMFTXkgQ0EwggEiMA0GCSqGSIb3DQEB +AQUAA4IBDwAwggEKAoIBAQCt66py3x7sCSASRF2D05L5wkNDxAUjQKYx23W8Gbwv +GMGykk89BIdU5LX1JB1cKiUOkoIxfwAYuWc2V/wzTvVV7+11besnk3uX1c9KiqUF +LIX7kn/z5hzS4aelhKvH+MJlSZCSlp1ytpZbwo5GB5Pi2SGH56jDBiBoDRNBVdWL +z4wH7TdrQjqWwNxIZumD5OGMtcfJyuX08iPiEOaslOeoMqzObhvjc9aUgjVjhqyA +FkJGTXsi0oaD7oml+NE+mTNfEeZvEJQpLSjBY0OvQHzuHkyGBShBnfu/9x7/NRwd +WaqsLiF7/re9KDGYdJwP7Cu6uxYfKAyWarp6h2mG/GIdAgMBAAEwDQYJKoZIhvcN +AQELBQADggEBAGIl/VVIafeq/AJOQ9r7TzzB2ABJYr7NZa6bTu5O1jSp1Fonac15 +SZ8gvRxODgH22ZYSqghPG4xzq4J3hkytlQqm57ZEt2I2M3OqIp17Ndcc1xDYzpLl +tA0FrVn6crQTM8vQkTDtGesaCWX+7Fir5dK7HnYWzfpSmsOpST07PfbNisEXKOxG +Dj4lBL1OnhTjsJeymVS1pFvkKkrcEJO+IxFiHL3CDsWjcXB0Z+E1zBtPoYyYsNsO +rBrjUxcZewF4xqWZhpW90Mt61fY2nRgU0uUwHcvDQUqvmzKcsqYa4mPKzfBI5mxo +01Ta96cDD6pS5Y1hOflZ0g84f2g/7xBLLDA= +-----END CERTIFICATE----- diff --git a/rust/fatcat-api/examples/client.rs b/rust/fatcat-api/examples/client.rs new file mode 100644 index 00000000..6f7aa151 --- /dev/null +++ b/rust/fatcat-api/examples/client.rs @@ -0,0 +1,350 @@ +#![allow(missing_docs, unused_variables, trivial_casts)] + +extern crate clap; +extern crate fatcat; +#[allow(unused_extern_crates)] +extern crate futures; +#[allow(unused_extern_crates)] +extern crate swagger; +extern crate tokio_core; +#[allow(unused_extern_crates)] +extern crate uuid; + +use clap::{App, Arg}; +#[allow(unused_imports)] +use fatcat::{ApiError, ApiNoContext, ContainerIdGetResponse, ContainerLookupGetResponse, + ContainerPostResponse, ContextWrapperExt, CreatorIdGetResponse, + CreatorLookupGetResponse, CreatorPostResponse, EditgroupIdAcceptPostResponse, + EditgroupIdGetResponse, EditgroupPostResponse, EditorUsernameChangelogGetResponse, + EditorUsernameGetResponse, FileIdGetResponse, FileLookupGetResponse, + FilePostResponse, ReleaseIdGetResponse, ReleaseLookupGetResponse, + ReleasePostResponse, WorkIdGetResponse, WorkPostResponse}; +#[allow(unused_imports)] +use futures::{future, stream, Future, Stream}; +use tokio_core::reactor; + +fn main() { + let matches = App::new("client") + .arg( + Arg::with_name("operation") + .help("Sets the operation to run") + .possible_values(&[ + "ContainerIdGet", + "ContainerLookupGet", + "ContainerPost", + "CreatorIdGet", + "CreatorLookupGet", + "CreatorPost", + "EditgroupIdAcceptPost", + "EditgroupIdGet", + "EditgroupPost", + "EditorUsernameChangelogGet", + "EditorUsernameGet", + "FileIdGet", + "FileLookupGet", + "FilePost", + "ReleaseIdGet", + "ReleaseLookupGet", + "ReleasePost", + "WorkIdGet", + "WorkPost", + ]) + .required(true) + .index(1), + ) + .arg( + Arg::with_name("https") + .long("https") + .help("Whether to use HTTPS or not"), + ) + .arg( + Arg::with_name("host") + .long("host") + .takes_value(true) + .default_value("api.fatcat.wiki") + .help("Hostname to contact"), + ) + .arg( + Arg::with_name("port") + .long("port") + .takes_value(true) + .default_value("8080") + .help("Port to contact"), + ) + .get_matches(); + + let mut core = reactor::Core::new().unwrap(); + let is_https = matches.is_present("https"); + let base_url = format!( + "{}://{}:{}", + if is_https { "https" } else { "http" }, + matches.value_of("host").unwrap(), + matches.value_of("port").unwrap() + ); + let client = if matches.is_present("https") { + // Using Simple HTTPS + fatcat::Client::try_new_https(core.handle(), &base_url, "examples/ca.pem") + .expect("Failed to create HTTPS client") + } else { + // Using HTTP + fatcat::Client::try_new_http(core.handle(), &base_url) + .expect("Failed to create HTTP client") + }; + + // Using a non-default `Context` is not required; this is just an example! + let client = client.with_context(fatcat::Context::new_with_span_id( + self::uuid::Uuid::new_v4().to_string(), + )); + + match matches.value_of("operation") { + Some("ContainerIdGet") => { + let result = core.run(client.container_id_get("id_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("ContainerLookupGet") => { + let result = core.run(client.container_lookup_get("issn_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("ContainerPost") => { + let result = core.run(client.container_post(None)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("CreatorIdGet") => { + let result = core.run(client.creator_id_get("id_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("CreatorLookupGet") => { + let result = core.run(client.creator_lookup_get("orcid_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("CreatorPost") => { + let result = core.run(client.creator_post(None)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("EditgroupIdAcceptPost") => { + let result = core.run(client.editgroup_id_accept_post(56)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("EditgroupIdGet") => { + let result = core.run(client.editgroup_id_get(56)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("EditgroupPost") => { + let result = core.run(client.editgroup_post()); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("EditorUsernameChangelogGet") => { + let result = + core.run(client.editor_username_changelog_get("username_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("EditorUsernameGet") => { + let result = core.run(client.editor_username_get("username_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("FileIdGet") => { + let result = core.run(client.file_id_get("id_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("FileLookupGet") => { + let result = core.run(client.file_lookup_get("sha1_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("FilePost") => { + let result = core.run(client.file_post(None)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("ReleaseIdGet") => { + let result = core.run(client.release_id_get("id_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("ReleaseLookupGet") => { + let result = core.run(client.release_lookup_get("doi_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("ReleasePost") => { + let result = core.run(client.release_post(None)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("WorkIdGet") => { + let result = core.run(client.work_id_get("id_example".to_string())); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + Some("WorkPost") => { + let result = core.run(client.work_post(None)); + println!( + "{:?} (X-Span-ID: {:?})", + result, + client + .context() + .x_span_id + .clone() + .unwrap_or(String::from("<none>")) + ); + } + + _ => panic!("Invalid operation provided"), + } +} diff --git a/rust/fatcat-api/examples/server-chain.pem b/rust/fatcat-api/examples/server-chain.pem new file mode 100644 index 00000000..47d7e201 --- /dev/null +++ b/rust/fatcat-api/examples/server-chain.pem @@ -0,0 +1,66 @@ +Certificate: + Data: + Version: 1 (0x0) + Serial Number: 4096 (0x1000) + Signature Algorithm: sha256WithRSAEncryption + Issuer: C=US, CN=My CA + Validity + Not Before: May 23 16:00:23 2017 GMT + Not After : Apr 29 16:00:23 2117 GMT + Subject: CN=localhost, C=US + Subject Public Key Info: + Public Key Algorithm: rsaEncryption + Public-Key: (2048 bit) + Modulus: + 00:c9:d4:43:60:50:fc:d6:0f:38:4d:5d:5e:aa:7c: + c0:5e:a9:ec:d9:93:78:d3:93:72:28:41:f5:08:a5: + ea:ac:67:07:d7:1f:f7:7d:74:69:7e:46:89:20:4b: + 7a:2d:9b:02:08:e7:6f:0f:1d:0c:0f:c7:60:69:19: + 4b:df:7e:ca:75:94:0b:49:71:e3:6d:f2:e8:79:fd: + ed:0a:94:67:55:f3:ca:6b:61:ba:58:b7:2e:dd:7b: + ca:b9:02:9f:24:36:ac:26:8f:04:8f:81:c8:35:10: + f4:aa:33:b2:24:16:f8:f7:1e:ea:f7:16:fe:fa:34: + c3:dd:bb:2c:ba:7a:df:4d:e2:da:1e:e5:d2:28:44: + 6e:c8:96:e0:fd:09:0c:14:0c:31:dc:e0:ca:c1:a7: + 9b:bf:16:8c:f7:36:3f:1b:2e:dd:90:eb:45:78:51: + bf:59:22:1e:c6:8c:0a:69:88:e5:03:5e:73:b7:fc: + 93:7f:1b:46:1b:97:68:c5:c0:8b:35:1f:bb:1e:67: + 7f:55:b7:3b:55:3f:ea:f2:ca:db:cc:52:cd:16:89: + db:15:47:bd:f2:cd:6c:7a:d7:b4:1a:ac:c8:15:6c: + 6a:fb:77:c4:e9:f2:30:e0:14:24:66:65:6f:2a:e5: + 2d:cc:f6:81:ae:57:c8:d1:9b:38:90:dc:60:93:02: + 5e:cb + Exponent: 65537 (0x10001) + Signature Algorithm: sha256WithRSAEncryption + 1c:7c:39:e8:3d:49:b2:09:1e:68:5a:2f:74:18:f4:63:b5:8c: + f6:e6:a1:e3:4d:95:90:99:ef:32:5c:34:40:e8:55:13:0e:e0: + 1c:be:cd:ab:3f:64:38:99:5e:2b:c1:81:53:a0:18:a8:f6:ee: + 6a:33:73:6c:9a:73:9d:86:08:5d:c7:11:38:46:4c:cd:a0:47: + 37:8f:fe:a6:50:a9:02:21:99:42:86:5e:47:fe:65:56:60:1d: + 16:53:86:bd:e4:63:c5:69:cf:fa:30:51:ab:a1:c3:50:53:cc: + 66:1c:4c:ff:3f:2a:39:4d:a2:8f:9d:d1:a7:8b:22:e4:78:69: + 24:06:83:4d:cc:0a:c0:87:69:9b:bc:80:a9:d2:b7:a5:23:84: + 7e:a2:32:26:7c:78:0e:bd:db:cd:3b:69:18:33:b8:44:ef:96: + b4:99:86:ee:06:bd:51:1c:c7:a1:a4:0c:c4:4c:51:a0:df:ac: + 14:07:88:8e:d7:39:45:fe:52:e0:a3:4c:db:5d:7a:ab:4d:e4: + ca:06:e8:bd:74:6f:46:e7:93:4a:4f:1b:67:e7:a5:9f:ef:9c: + 02:49:d1:f2:d5:e9:53:ee:09:21:ac:08:c8:15:f7:af:35:b9: + 4f:11:0f:43:ae:46:8e:fd:5b:8d:a3:4e:a7:2c:b7:25:ed:e4: + e5:94:1d:e3 +-----BEGIN CERTIFICATE----- +MIICtTCCAZ0CAhAAMA0GCSqGSIb3DQEBCwUAMB0xCzAJBgNVBAYTAlVTMQ4wDAYD +VQQDEwVNeSBDQTAgFw0xNzA1MjMxNjAwMjNaGA8yMTE3MDQyOTE2MDAyM1owITES +MBAGA1UEAxMJbG9jYWxob3N0MQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEB +BQADggEPADCCAQoCggEBAMnUQ2BQ/NYPOE1dXqp8wF6p7NmTeNOTcihB9Qil6qxn +B9cf9310aX5GiSBLei2bAgjnbw8dDA/HYGkZS99+ynWUC0lx423y6Hn97QqUZ1Xz +ymthuli3Lt17yrkCnyQ2rCaPBI+ByDUQ9KozsiQW+Pce6vcW/vo0w927LLp6303i +2h7l0ihEbsiW4P0JDBQMMdzgysGnm78WjPc2Pxsu3ZDrRXhRv1kiHsaMCmmI5QNe +c7f8k38bRhuXaMXAizUfux5nf1W3O1U/6vLK28xSzRaJ2xVHvfLNbHrXtBqsyBVs +avt3xOnyMOAUJGZlbyrlLcz2ga5XyNGbOJDcYJMCXssCAwEAATANBgkqhkiG9w0B +AQsFAAOCAQEAHHw56D1JsgkeaFovdBj0Y7WM9uah402VkJnvMlw0QOhVEw7gHL7N +qz9kOJleK8GBU6AYqPbuajNzbJpznYYIXccROEZMzaBHN4/+plCpAiGZQoZeR/5l +VmAdFlOGveRjxWnP+jBRq6HDUFPMZhxM/z8qOU2ij53Rp4si5HhpJAaDTcwKwIdp +m7yAqdK3pSOEfqIyJnx4Dr3bzTtpGDO4RO+WtJmG7ga9URzHoaQMxExRoN+sFAeI +jtc5Rf5S4KNM2116q03kygbovXRvRueTSk8bZ+eln++cAknR8tXpU+4JIawIyBX3 +rzW5TxEPQ65Gjv1bjaNOpyy3Je3k5ZQd4w== +-----END CERTIFICATE----- diff --git a/rust/fatcat-api/examples/server-key.pem b/rust/fatcat-api/examples/server-key.pem new file mode 100644 index 00000000..29c00682 --- /dev/null +++ b/rust/fatcat-api/examples/server-key.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJ1ENgUPzWDzhN +XV6qfMBeqezZk3jTk3IoQfUIpeqsZwfXH/d9dGl+RokgS3otmwII528PHQwPx2Bp +GUvffsp1lAtJceNt8uh5/e0KlGdV88prYbpYty7de8q5Ap8kNqwmjwSPgcg1EPSq +M7IkFvj3Hur3Fv76NMPduyy6et9N4toe5dIoRG7IluD9CQwUDDHc4MrBp5u/Foz3 +Nj8bLt2Q60V4Ub9ZIh7GjAppiOUDXnO3/JN/G0Ybl2jFwIs1H7seZ39VtztVP+ry +ytvMUs0WidsVR73yzWx617QarMgVbGr7d8Tp8jDgFCRmZW8q5S3M9oGuV8jRmziQ +3GCTAl7LAgMBAAECggEBAKEd1q9j14KWYc64s6KLthGbutyxsinMMbxbct11fdIk +6YhdF3fJ35ETg9IJDr6rWEN9ZRX+jStncNpVfFEs6ThVd3Eo/nI+EEGaaIkikR93 +X2a7fEPn7/yVHu70XdBN6L1bPDvHUeiy4W2hmRrgT90OjGm1rNRWHOm7yugOwIZu +HclzbR9Ca7EInFnotUiDQm9sw9VKHbJHqWx6OORdZrxR2ytYs0Qkq0XpGMvti2HW +7WAmKTg5QM8myXW7+/4iqb/u68wVBR2BBalShKmIf7lim9O3W2a1RjDdsvm/wNe9 +I+D+Iq825vpqkKXcrxYlpVg7hYiaQaW/MNsEb7lQRjECgYEA/RJYby0POW+/k0Jn +jO8UmJVEMiuGa8WIUu/JJWMOmzRCukjSRNQOkt7niQrZPJYE8W6clM6RJTolWf9L +IL6mIb+mRaoudUk8SHGDq7ho1iMg9GK8lhYxvKh1Q6uv8EyVSkgLknAEY0NANKC1 +zNdU5Dhven9aRX2gq9vP4XwMz2MCgYEAzCogQ7IFk+gkp3k491dOZnrGRoRCfuzo +4CJtyKFgOSd7BjmpcKkj0IPfVBjw6GjMIxfQRMTQmxAjjWevH45vG8l0Iiwz/gSp +81b5nsDEX5uv2Olcmcz5zxRFy36jOZ9ihMWinxcIlT2oDbyCdbruDKZq9ieJ9S8g +4qGx0OkwE3kCgYEA7CmAiU89U9YqqttfEq/RQoqY91CSwmO10d+ej9seuEtOsdRf +FIfnibulycdr7hP5TOxyBpO1802NqayJiWcgVYIpQf2MGTtcnCYCP+95NcvWZvj1 +EAJqK6nwtFO1fcOZ1ZXh5qfOEGujsPkAbsXLnKXlsiTCMvMHSxl3pu5Cbg0CgYBf +JjbZNctRrjv+7Qj2hPLd4dQsIxGWc7ToWENP4J2mpVa5hQAJqFovoHXhjKohtk2F +AWEn243Y5oGbMjo0e74edhmwn2cvuF64MM2vBem/ISCn98IXT6cQskMA3qkVfsl8 +VVs/x41ReGWs2TD3y0GMFbb9t1mdMfSiincDhNnKCQKBgGfeT4jKyYeCoCw4OLI1 +G75Gd0METt/IkppwODPpNwj3Rp9I5jctWZFA/3wCX/zk0HgBeou5AFNS4nQZ/X/L +L9axbSdR7UJTGkT1r4gu3rLkPV4Tk+8XM03/JT2cofMlzQBuhvl1Pn4SgKowz7hl +lS76ECw4Av3T0S34VW9Z5oye +-----END PRIVATE KEY----- diff --git a/rust/fatcat-api/examples/server.rs b/rust/fatcat-api/examples/server.rs new file mode 100644 index 00000000..f9b8617c --- /dev/null +++ b/rust/fatcat-api/examples/server.rs @@ -0,0 +1,78 @@ +//! Main binary entry point for fatcat implementation. + +#![allow(missing_docs)] + +// Imports required by this file. +// extern crate <name of this crate>; +extern crate clap; +extern crate fatcat; +extern crate hyper; +extern crate native_tls; +extern crate openssl; +extern crate swagger; +extern crate tokio_proto; +extern crate tokio_tls; + +// Imports required by server library. +// extern crate fatcat; +// extern crate swagger; +extern crate chrono; +extern crate futures; +#[macro_use] +extern crate error_chain; + +use clap::{App, Arg}; +use hyper::server::Http; +use openssl::error::ErrorStack; +use openssl::ssl::{SslAcceptorBuilder, SslMethod}; +use openssl::x509::X509_FILETYPE_PEM; +use swagger::auth::AllowAllAuthenticator; +use tokio_proto::TcpServer; + +mod server_lib; + +// Builds an SSL implementation for Simple HTTPS from some hard-coded file names +fn ssl() -> Result<SslAcceptorBuilder, ErrorStack> { + let mut ssl = SslAcceptorBuilder::mozilla_intermediate_raw(SslMethod::tls())?; + + // Server authentication + ssl.set_private_key_file("examples/server-key.pem", X509_FILETYPE_PEM)?; + ssl.set_certificate_chain_file("examples/server-chain.pem")?; + ssl.check_private_key()?; + + Ok(ssl) +} + +/// Create custom server, wire it to the autogenerated router, +/// and pass it to the web server. +fn main() { + let matches = App::new("server") + .arg( + Arg::with_name("https") + .long("https") + .help("Whether to use HTTPS or not"), + ) + .get_matches(); + + let service_fn = fatcat::server::auth::NewService::new(AllowAllAuthenticator::new( + server_lib::NewService, + "cosmo", + )); + + let addr = "127.0.0.1:8080" + .parse() + .expect("Failed to parse bind address"); + if matches.is_present("https") { + let ssl = ssl().expect("Failed to load SSL keys"); + let builder: native_tls::TlsAcceptorBuilder = + native_tls::backend::openssl::TlsAcceptorBuilderExt::from_openssl(ssl); + let tls_acceptor = builder.build().expect("Failed to build TLS acceptor"); + TcpServer::new( + tokio_tls::proto::Server::new(Http::new(), tls_acceptor), + addr, + ).serve(service_fn); + } else { + // Using HTTP + TcpServer::new(Http::new(), addr).serve(service_fn); + } +} diff --git a/rust/fatcat-api/examples/server_lib/mod.rs b/rust/fatcat-api/examples/server_lib/mod.rs new file mode 100644 index 00000000..a45fbe50 --- /dev/null +++ b/rust/fatcat-api/examples/server_lib/mod.rs @@ -0,0 +1,26 @@ +//! Main library entry point for fatcat implementation. + +mod server; + +mod errors { + error_chain!{} +} + +pub use self::errors::*; +use fatcat; +use hyper; +use std::io; + +pub struct NewService; + +impl hyper::server::NewService for NewService { + type Request = (hyper::Request, fatcat::Context); + type Response = hyper::Response; + type Error = hyper::Error; + type Instance = fatcat::server::Service<server::Server>; + + /// Instantiate a new server. + fn new_service(&self) -> io::Result<Self::Instance> { + Ok(fatcat::server::Service::new(server::Server)) + } +} diff --git a/rust/fatcat-api/examples/server_lib/server.rs b/rust/fatcat-api/examples/server_lib/server.rs new file mode 100644 index 00000000..0dec7ed3 --- /dev/null +++ b/rust/fatcat-api/examples/server_lib/server.rs @@ -0,0 +1,287 @@ +//! Server implementation of fatcat. + +#![allow(unused_imports)] + +use chrono; +use futures::{self, Future}; + +use std::collections::HashMap; + +use swagger; + +use fatcat::models; +use fatcat::{Api, ApiError, ContainerIdGetResponse, ContainerLookupGetResponse, + ContainerPostResponse, Context, CreatorIdGetResponse, CreatorLookupGetResponse, + CreatorPostResponse, EditgroupIdAcceptPostResponse, EditgroupIdGetResponse, + EditgroupPostResponse, EditorUsernameChangelogGetResponse, EditorUsernameGetResponse, + FileIdGetResponse, FileLookupGetResponse, FilePostResponse, ReleaseIdGetResponse, + ReleaseLookupGetResponse, ReleasePostResponse, WorkIdGetResponse, WorkPostResponse}; + +#[derive(Copy, Clone)] +pub struct Server; + +impl Api for Server { + fn container_id_get( + &self, + id: String, + context: &Context, + ) -> Box<Future<Item = ContainerIdGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "container_id_get(\"{}\") - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn container_lookup_get( + &self, + issn: String, + context: &Context, + ) -> Box<Future<Item = ContainerLookupGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "container_lookup_get(\"{}\") - X-Span-ID: {:?}", + issn, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn container_post( + &self, + body: Option<models::ContainerEntity>, + context: &Context, + ) -> Box<Future<Item = ContainerPostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "container_post({:?}) - X-Span-ID: {:?}", + body, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn creator_id_get( + &self, + id: String, + context: &Context, + ) -> Box<Future<Item = CreatorIdGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "creator_id_get(\"{}\") - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn creator_lookup_get( + &self, + orcid: String, + context: &Context, + ) -> Box<Future<Item = CreatorLookupGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "creator_lookup_get(\"{}\") - X-Span-ID: {:?}", + orcid, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn creator_post( + &self, + body: Option<models::CreatorEntity>, + context: &Context, + ) -> Box<Future<Item = CreatorPostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "creator_post({:?}) - X-Span-ID: {:?}", + body, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn editgroup_id_accept_post( + &self, + id: i32, + context: &Context, + ) -> Box<Future<Item = EditgroupIdAcceptPostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "editgroup_id_accept_post({}) - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn editgroup_id_get( + &self, + id: i32, + context: &Context, + ) -> Box<Future<Item = EditgroupIdGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "editgroup_id_get({}) - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn editgroup_post( + &self, + context: &Context, + ) -> Box<Future<Item = EditgroupPostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "editgroup_post() - X-Span-ID: {:?}", + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn editor_username_changelog_get( + &self, + username: String, + context: &Context, + ) -> Box<Future<Item = EditorUsernameChangelogGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "editor_username_changelog_get(\"{}\") - X-Span-ID: {:?}", + username, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn editor_username_get( + &self, + username: String, + context: &Context, + ) -> Box<Future<Item = EditorUsernameGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "editor_username_get(\"{}\") - X-Span-ID: {:?}", + username, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn file_id_get( + &self, + id: String, + context: &Context, + ) -> Box<Future<Item = FileIdGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "file_id_get(\"{}\") - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn file_lookup_get( + &self, + sha1: String, + context: &Context, + ) -> Box<Future<Item = FileLookupGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "file_lookup_get(\"{}\") - X-Span-ID: {:?}", + sha1, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn file_post( + &self, + body: Option<models::FileEntity>, + context: &Context, + ) -> Box<Future<Item = FilePostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "file_post({:?}) - X-Span-ID: {:?}", + body, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn release_id_get( + &self, + id: String, + context: &Context, + ) -> Box<Future<Item = ReleaseIdGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "release_id_get(\"{}\") - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn release_lookup_get( + &self, + doi: String, + context: &Context, + ) -> Box<Future<Item = ReleaseLookupGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "release_lookup_get(\"{}\") - X-Span-ID: {:?}", + doi, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn release_post( + &self, + body: Option<models::ReleaseEntity>, + context: &Context, + ) -> Box<Future<Item = ReleasePostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "release_post({:?}) - X-Span-ID: {:?}", + body, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn work_id_get( + &self, + id: String, + context: &Context, + ) -> Box<Future<Item = WorkIdGetResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "work_id_get(\"{}\") - X-Span-ID: {:?}", + id, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } + + fn work_post( + &self, + body: Option<models::WorkEntity>, + context: &Context, + ) -> Box<Future<Item = WorkPostResponse, Error = ApiError>> { + let context = context.clone(); + println!( + "work_post({:?}) - X-Span-ID: {:?}", + body, + context.x_span_id.unwrap_or(String::from("<none>")).clone() + ); + Box::new(futures::failed("Generic failure".into())) + } +} |