yggdrasil/src/server/mod.rs

66 lines
2.2 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::debug;
use tide::{Request, Response, utils::After};
use yggdrasil::*;
mod account;
mod auth;
mod authlib;
mod minecraft;
mod session;
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>() {
debug!("{:?}", err.to_owned());
let body = err.to_json();
let status = err.2;
res.set_status(status);
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(|req: Request<Database>| async move {
let res = Response::builder(200)
.header("x-authlib-injector-api-location", format!("{}/authlib/", req.state().config.external_base_url))
.build();
Ok(res)
});
// 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("/auth/").nest(auth::nest(db.to_owned()));
app.at("/session/").nest(session::nest(db.to_owned()));
// Listen
app.listen(format!("{}:{}", &db.config.address, &db.config.port)).await?;
Ok(())
}