Use actix-remote-ip to reduce code base size (#5)
All checks were successful
continuous-integration/drone/push Build is passing

Use https://crates.io/crates/actix-remote-ip to remove some code redundancy

Reviewed-on: #5
This commit is contained in:
2023-04-29 09:08:18 +00:00
parent 07fc94f930
commit 40eb16cef9
7 changed files with 79 additions and 271 deletions

View File

@ -1,11 +1,57 @@
use actix_remote_ip::{RemoteIP, RemoteIPConfig};
use actix_web::middleware::Logger;
use actix_web::{get, web, App, HttpResponse, HttpServer};
use askama::Template;
use light_openid::basic_state_manager::BasicStateManager;
use light_openid::primitives::OpenIDConfig;
use oidc_test_client::app_config::AppConfig;
use oidc_test_client::remote_ip::RemoteIP;
use clap::Parser;
const REDIRECT_URI: &str = "/redirect";
/// Basic OpenID test client
#[derive(Parser, Debug)]
pub struct AppConfig {
/// Listen URL
#[arg(short, long, env, default_value = "0.0.0.0:7510")]
pub listen_addr: String,
/// Public URL, the URL where this service is accessible, without the trailing slash
#[arg(short, long, env, default_value = "http://localhost:7510")]
pub public_url: String,
/// URL where the OpenID configuration can be found
#[arg(short, long, env)]
pub configuration_url: String,
/// OpenID client ID
#[arg(long, env)]
pub client_id: String,
/// OpenID client secret
#[arg(long, env)]
pub client_secret: String,
/// Proxy IP, might end with a "*"
#[clap(long, env)]
pub proxy_ip: Option<String>,
}
impl AppConfig {
pub fn get() -> &'static Self {
&CONF
}
pub fn redirect_url(&self) -> String {
format!("{}{}", self.public_url, REDIRECT_URI)
}
}
lazy_static::lazy_static! {
static ref CONF: AppConfig = {
AppConfig::parse()
};
}
#[get("/assets/bootstrap.min.css")]
async fn bootstrap() -> HttpResponse {
@ -24,6 +70,7 @@ async fn cover() -> HttpResponse {
#[derive(Template)]
#[template(path = "home.html")]
struct HomeTemplate {
remote_ip: String,
redirect_url: String,
}
@ -49,9 +96,10 @@ impl<'a> ErrorTemplate<'a> {
}
#[get("/")]
async fn home() -> HttpResponse {
async fn home(remote_ip: RemoteIP) -> HttpResponse {
HttpResponse::Ok().content_type("text/html").body(
HomeTemplate {
remote_ip: remote_ip.0.to_string(),
redirect_url: AppConfig::get().redirect_url(),
}
.render()
@ -169,6 +217,9 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.wrap(Logger::default())
.app_data(web::Data::new(RemoteIPConfig {
proxy: CONF.proxy_ip.clone(),
}))
.app_data(state_manager.clone())
.service(bootstrap)
.service(cover)
@ -181,3 +232,14 @@ async fn main() -> std::io::Result<()> {
.run()
.await
}
#[cfg(test)]
mod test {
use crate::AppConfig;
#[test]
fn verify_cli() {
use clap::CommandFactory;
AppConfig::command().debug_assert();
}
}