make all urls variable
parent
f5e976103c
commit
a484de34a6
11
src/main.rs
11
src/main.rs
|
@ -122,8 +122,13 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
|
|||
client_secret: env::var("DISCORD_CLIENT_SECRET")
|
||||
.expect("expected DISCORD_CLIENT_SECRET env var"),
|
||||
};
|
||||
let origin = env::var("APP_ORIGIN").expect("expected APP_ORIGIN");
|
||||
|
||||
let state = ApiState { settings, secrets };
|
||||
let state = ApiState {
|
||||
settings,
|
||||
secrets,
|
||||
origin: origin.clone(),
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
let api = Router::new()
|
||||
|
@ -143,12 +148,12 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) {
|
|||
.layer(
|
||||
CorsLayer::new()
|
||||
// TODO: move this to env variable
|
||||
.allow_origin(["https://spacegirl.nl".parse().unwrap()])
|
||||
.allow_origin([origin.parse().unwrap()])
|
||||
.allow_headers(Any)
|
||||
.allow_methods([Method::GET, Method::POST]),
|
||||
)
|
||||
.with_state(Arc::new(state));
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 8100));
|
||||
info!("socket listening on {addr}");
|
||||
axum::Server::bind(&addr)
|
||||
.serve(api.into_make_service())
|
||||
|
|
|
@ -76,6 +76,8 @@ pub(crate) enum Error {
|
|||
|
||||
impl IntoResponse for Error {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
error!("{self}");
|
||||
|
||||
match self {
|
||||
Self::Auth(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg).into_response(),
|
||||
Self::GetUser(error) => (StatusCode::UNAUTHORIZED, error.to_string()).into_response(),
|
||||
|
@ -118,11 +120,13 @@ pub(crate) async fn auth(
|
|||
info!("attempting to get access token with code {}", code);
|
||||
|
||||
let mut data = HashMap::new();
|
||||
|
||||
let redirect_uri = format!("{}/auth", state.origin);
|
||||
data.insert("client_id", state.secrets.client_id.as_str());
|
||||
data.insert("client_secret", state.secrets.client_secret.as_str());
|
||||
data.insert("grant_type", "authorization_code");
|
||||
data.insert("code", code);
|
||||
data.insert("redirect_uri", "https://spacegirl.nl/memes/auth");
|
||||
data.insert("redirect_uri", &redirect_uri);
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ use uuid::Uuid;
|
|||
pub(crate) struct ApiState {
|
||||
pub settings: Arc<tokio::sync::Mutex<Settings>>,
|
||||
pub secrets: auth::DiscordSecret,
|
||||
pub origin: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
Loading…
Reference in New Issue