account/src/main.rs

195 lines
6.1 KiB
Rust
Raw Normal View History

2025-02-24 10:43:47 +01:00
use std::env;
2025-04-26 21:03:07 +02:00
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2025-04-27 11:09:15 +02:00
use std::sync::Arc;
2025-02-27 10:25:31 +01:00
use std::time::{SystemTime, UNIX_EPOCH};
2025-02-24 10:43:47 +01:00
use dotenvy::dotenv;
2025-03-09 23:47:46 +01:00
use juniper::{EmptyMutation, EmptySubscription};
2025-04-27 11:09:15 +02:00
use minio::s3::ClientBuilder;
use minio::s3::creds::StaticProvider;
use minio::s3::http::BaseUrl;
use once_cell::sync::Lazy;
2025-02-24 10:43:47 +01:00
use rocket::fairing::AdHoc;
2025-04-26 21:03:07 +02:00
use rocket::http::{ContentType, Header, Status};
use rocket::{catch, catchers, routes, Request};
use rocket::response::content::RawXml;
2025-02-27 10:25:31 +01:00
use sqlx::Postgres;
use sqlx::postgres::PgPoolOptions;
2025-03-09 23:47:46 +01:00
use tonic::transport::Server;
use crate::graphql::{Query, Schema};
2025-04-27 11:09:15 +02:00
use crate::nnid::people::S3ClientState;
2025-02-23 19:33:55 +01:00
mod xml;
mod conntest;
2025-02-27 10:25:31 +01:00
mod nnid;
2025-02-24 10:43:47 +01:00
mod account;
2025-02-27 10:25:31 +01:00
mod error;
mod dsresponse;
2025-02-27 21:49:37 +01:00
mod data_wrapper;
2025-04-26 21:03:07 +02:00
// #[deprecated]
mod grpc;
2025-03-09 23:47:46 +01:00
mod graphql;
2025-04-26 13:38:11 +02:00
mod email;
mod papi;
2025-02-27 10:25:31 +01:00
type Pool = sqlx::Pool<Postgres>;
2025-02-23 19:33:55 +01:00
2025-03-09 23:47:46 +01:00
async fn start_grpc(){
let act_database_url = env::var("DATABASE_URL").expect("account database url is not set");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&act_database_url)
.await
.expect("unable to create pool");
let grpc_instance = grpc::AccountService(pool);
let addr: SocketAddr =
SocketAddr::from((
env::var("ROCKET_ADDRESS").ok()
.map(|v| v.parse().expect("unable to read address"))
.unwrap_or(IpAddr::V4(Ipv4Addr::LOCALHOST)),
7071
)
);
tokio::spawn(async move{
Server::builder()
.add_service(grpc::grpc::account_server::AccountServer::new(grpc_instance))
.serve(addr)
.await
.expect("unable to start grpc server");
});
}
2025-04-26 21:03:07 +02:00
#[catch(404)]
fn not_found(_req: &Request) -> (Status, (ContentType, RawXml<&'static str>)) {
(
Status::NotFound,
(
ContentType::XML,
RawXml(
r#"<?xml version="1.0"?>
<errors>
<error>
<cause/>
<code>0008</code>
<message>Not found</message>
</error>
</errors>"#,
),
),
)
}
2025-02-23 19:33:55 +01:00
#[rocket::launch]
async fn launch() -> _ {
2025-02-24 10:43:47 +01:00
dotenv().ok();
2025-03-09 23:47:46 +01:00
start_grpc().await;
2025-02-27 10:25:31 +01:00
let act_database_url = env::var("DATABASE_URL").expect("account database url is not set");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&act_database_url).await
.expect("unable to create pool");
2025-02-24 10:43:47 +01:00
2025-03-09 23:47:46 +01:00
let graph_pool = PgPoolOptions::new()
.max_connections(5)
.connect(&act_database_url).await
.expect("unable to create pool");
2025-04-27 11:12:26 +02:00
pub static S3_URL_STRING: Lazy<Box<str>> = Lazy::new(||
2025-04-27 11:09:15 +02:00
env::var("S3_URL").expect("S3_URL not specified").into_boxed_str()
);
2025-04-27 11:12:26 +02:00
pub static S3_URL: Lazy<BaseUrl> = Lazy::new(||
2025-04-27 11:09:15 +02:00
S3_URL_STRING.parse().unwrap()
);
2025-04-27 11:12:26 +02:00
pub static S3_USER: Lazy<Box<str>> = Lazy::new(||
2025-04-27 11:09:15 +02:00
env::var("S3_USER").expect("S3_USER not specified").into_boxed_str()
);
2025-04-27 11:12:26 +02:00
pub static S3_PASSWD: Lazy<Box<str>> = Lazy::new(||
2025-04-27 11:09:15 +02:00
env::var("S3_PASSWD").expect("S3_PASSWD not specified").into_boxed_str()
);
pub static CDN_URL: Lazy<Box<str>> = Lazy::new(||
env::var("CDN_URL").expect("CDN_URL not specified").into_boxed_str()
);
2025-04-27 11:09:15 +02:00
let s3_client = ClientBuilder::new(S3_URL.clone())
.provider(Some(Box::new(StaticProvider::new(&S3_USER, &S3_PASSWD, None))))
.build()
.expect("failed to create s3 client");
2025-05-10 11:24:27 +02:00
let _guard = sentry::init(("https://03b49d3cc0012089b6f2608c265a721b@o4508799920635904.ingest.de.sentry.io/4509298106826832", sentry::ClientOptions {
release: sentry::release_name!(),
// Capture user IPs and potentially sensitive headers when using HTTP server integrations
// see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info
send_default_pii: true,
..Default::default()
}));
2025-02-23 19:33:55 +01:00
rocket::build()
2025-02-27 10:25:31 +01:00
.manage(pool)
2025-04-27 11:09:15 +02:00
.manage(S3ClientState {
client: Arc::new(s3_client),
})
2025-05-12 10:31:17 +02:00
.manage(graphql::Context {
pool: graph_pool,
api_key: None, // or Some(...) if youre preloading a static API key
})
2025-03-09 23:47:46 +01:00
.manage(Schema::new(
Query,
EmptyMutation::new(),
EmptySubscription::new())
)
2025-02-24 10:43:47 +01:00
.attach(AdHoc::on_response("org", |_, response| Box::pin(async move {
2025-02-27 10:25:31 +01:00
//response.adjoin_header(Header::new("x-organization", "Nintendo"));
response.adjoin_header(Header::new("x-nintendo-date", SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
.to_string()
));
2025-02-27 21:49:37 +01:00
//response.adjoin_header(Header::new("Content-Type", "text/xml; charset=utf-8"));
2025-02-27 10:25:31 +01:00
response.remove_header("x-content-type-options");
response.remove_header("x-frame-options");
response.remove_header("permissions-policy");
2025-02-24 10:43:47 +01:00
})))
2025-02-27 10:25:31 +01:00
.mount("/", routes![
conntest::conntest,
nnid::devices::current_device_status,
nnid::agreements::get_agreement,
nnid::timezones::get_timezone,
nnid::person_exists::person_exists,
2025-04-26 13:38:11 +02:00
nnid::support::validate,
nnid::support::verify_email,
2025-03-05 20:28:25 +01:00
nnid::people::create_account,
nnid::people::get_own_profile,
nnid::people::get_device_owner,
nnid::people::get_own_device,
2025-04-26 21:03:07 +02:00
nnid::people::change_mii,
nnid::oauth::generate_token::generate_token,
nnid::provider::get_nex_token,
2025-03-09 23:47:46 +01:00
nnid::provider::get_service_token,
nnid::mapped_ids::mapped_ids,
papi::login::login,
papi::user::get_user,
2025-03-09 23:47:46 +01:00
//graphql
graphql::graphiql,
graphql::playground,
graphql::get_graphql,
graphql::post_graphql,
2025-02-27 10:25:31 +01:00
])
2025-04-26 21:03:07 +02:00
.register("/", catchers![not_found])
2025-02-23 19:33:55 +01:00
}