yggdrasil/src/server/mod.rs

59 lines
2.0 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use log::info;
use tide::{Request, Response, utils::After};
use yggdrasil::*;
mod account;
mod authserver;
mod authlib;
mod minecraft;
mod sessionserver;
pub async fn start(db: Database) -> anyhow::Result<()> {
let mut app = tide::with_state(db.to_owned());
// Error handling middleware
app.with(After(|mut res: Response| async move {
if let Some(err) = res.downcast_error::<errors::YggdrasilError>() {
let body = err.to_json();
res.set_status(err.2);
res.set_body(body);
// TODO: pass through
// err.3: bool
} else if let Some(err) = res.error() {
res.set_body(format!("{}\n", err));
}
Ok(res)
}));
// Index
app.at("/").get(|mut req: Request<Database>| async move {
req.append_header("x-authlib-injector-api-location", "/authlib/");
Ok("Yggdrasil")
});
// Routes
app.at("/authlib/").nest(authlib::nest(db.to_owned()));
app.at("/account/").nest(account::nest(db.to_owned()));
app.at("/minecraft/").nest(minecraft::nest(db.to_owned()));
app.at("/authserver/").nest(authserver::nest(db.to_owned()));
app.at("/sessionserver/").nest(sessionserver::nest(db.to_owned()));
// Listen
app.listen(format!("{}:{}", &db.config.address, &db.config.port)).await?;
Ok(())
}