Compare commits

...

5 commits

8 changed files with 33 additions and 36 deletions
.github
examples
itconfig-macro/src

3
.github/FUNDING.yml vendored
View file

@ -1,3 +0,0 @@
# These are supported funding model platforms
liberapay: pleshevskiy

View file

@ -4,5 +4,6 @@ use diesel::prelude::*;
pub fn establish_connection() -> PgConnection {
let database_url = config::DATABASE_URL();
PgConnection::establish(database_url).expect(&format!("Error connecting to {}", database_url))
PgConnection::establish(database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}

View file

@ -23,7 +23,7 @@ fn main() {
println!("Displaying {} posts", posts.len());
for post in posts {
print!("\n");
println!();
println!("{}", post.title);
println!("----------");
println!("{}", post.body);

View file

@ -9,9 +9,9 @@ publish = false
[dependencies]
itconfig = { path = "../../itconfig", features = ["macro"] }
hyper = "0.14.4"
hyper = { version = "0.14.4", features = ["full"] }
serde_json = "1.0.62"
tokio = { version = "1.2.0", features = ["macros"] }
tokio = { version = "1.2.0", features = ["macros", "rt-multi-thread"] }
bytes = "1.0.1"
futures-util = { version = "0.3.13", default-features = false }
pretty_env_logger = "0.4.0"

View file

@ -1,4 +1,4 @@
use bytes::buf::BufExt;
use bytes::Buf;
use futures_util::{stream, StreamExt};
use hyper::client::HttpConnector;
use hyper::service::{make_service_fn, service_fn};
@ -6,10 +6,10 @@ use hyper::{header, Body, Client, Method, Request, Response, Server, StatusCode}
use itconfig::config;
config! {
HYPER {
hyper {
PREFER_SCHEMA: String => "http",
HOST < (
static HOST < (
ADDR => "127.0.0.1",
":",
PORT => 8000,
@ -25,19 +25,21 @@ config! {
}
type GenericError = Box<dyn std::error::Error + Send + Sync>;
type HyperResult<T> = std::result::Result<T, GenericError>;
type Result<T> = std::result::Result<T, GenericError>;
const INDEX: &'static [u8] = b"<a href=\"test.html\">test.html</a>";
const INTERNAL_SERVER_ERROR: &'static [u8] = b"Internal Server Error";
const NOTFOUND: &'static [u8] = b"Not Found";
const POST_DATA: &'static str = r#"{"original": "data"}"#;
static INDEX: &[u8] = b"<a href=\"test.html\">test.html</a>";
static INTERNAL_SERVER_ERROR: &[u8] = b"Internal Server Error";
static NOT_FOUND: &[u8] = b"Not Found";
static POST_DATA: &str = r#"{"original": "data"}"#;
async fn client_request_response(client: &Client<HttpConnector>) -> Result<Response<Body>> {
let url = format!("{}/json_api", config::hyper::HOST());
async fn client_request_response(client: &Client<HttpConnector>) -> HyperResult<Response<Body>> {
let req = Request::builder()
.method(Method::POST)
.uri(config::HYPER::JSON_API_URL())
.uri(url)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(POST_DATA))
.body(POST_DATA.into())
.unwrap();
let web_res = client.request(req).await?;
@ -55,7 +57,7 @@ async fn client_request_response(client: &Client<HttpConnector>) -> HyperResult<
Ok(Response::new(body))
}
async fn api_post_response(req: Request<Body>) -> HyperResult<Response<Body>> {
async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {
// Aggregate the body...
let whole_body = hyper::body::aggregate(req).await?;
// Decode as JSON...
@ -71,7 +73,7 @@ async fn api_post_response(req: Request<Body>) -> HyperResult<Response<Body>> {
Ok(response)
}
async fn api_get_response() -> HyperResult<Response<Body>> {
async fn api_get_response() -> Result<Response<Body>> {
let data = vec!["foo", "bar"];
let res = match serde_json::to_string(&data) {
Ok(json) => Response::builder()
@ -89,7 +91,7 @@ async fn api_get_response() -> HyperResult<Response<Body>> {
async fn response_examples(
req: Request<Body>,
client: Client<HttpConnector>,
) -> HyperResult<Response<Body>> {
) -> Result<Response<Body>> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/index.html") => Ok(Response::new(INDEX.into())),
(&Method::GET, "/test.html") => client_request_response(&client).await,
@ -99,18 +101,18 @@ async fn response_examples(
// Return 404 not found response.
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from(NOTFOUND))
.body(NOT_FOUND.into())
.unwrap())
}
}
}
#[tokio::main]
async fn main() -> HyperResult<()> {
async fn main() -> Result<()> {
config::init();
pretty_env_logger::init();
let addr = config::HYPER::HOST().parse().unwrap();
let addr = config::hyper::HOST().parse().unwrap();
// Share a `Client` with all `Service`s
let client = Client::new();

View file

@ -2,11 +2,11 @@
name = "itconfig-rocket-example"
version = "0.1.0"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
edition = "2018"
edition = "2021"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.7"
rocket = "0.5.0-rc.1"
itconfig = { path = '../../itconfig', features = ["macro"] }

View file

@ -3,10 +3,8 @@
#[macro_use]
extern crate rocket;
use itconfig::config;
config! {
ROCKET {
itconfig::config! {
rocket {
HOST: String => "localhost",
PORT: u16 => 9000,
BASE_URL => "/",
@ -14,14 +12,13 @@ config! {
}
#[get("/")]
fn index() -> &'static str {
fn hello() -> &'static str {
"Hello, world!"
}
fn main() {
#[launch]
fn rocket() -> _ {
config::init();
rocket::ignite()
.mount(config::ROCKET::BASE_URL(), routes![index])
.launch();
rocket::build().mount(config::rocket::BASE_URL(), routes![hello])
}

View file

@ -14,7 +14,7 @@ fn fill_env_prefix(prefix: String) -> Box<dyn Fn(Namespace) -> Namespace> {
Box::new(move |mut ns| {
let env_prefix = match &ns.env_prefix {
None => {
let env_prefix = format!("{}{}_", prefix, ns.name.clone().to_string());
let env_prefix = format!("{}{}_", prefix, ns.name.clone());
ns.env_prefix = Some(env_prefix.clone());
env_prefix
}