yggdrasil/src/server/mod.rs

56 lines
1.9 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 tide::{Request, Response, utils::After};
use yggdrasil::*;
mod account;
mod auth;
mod authlib;
mod services;
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>() {
let body = err.to_json();
res.set_status(err.2);
res.set_body(body);
// TODO: pass through
// err.3: bool
}
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("/account/").nest(account::nest(db.to_owned()));
app.at("/auth/").nest(auth::nest(db.to_owned()));
app.at("/services/").nest(services::nest(db.to_owned()));
app.at("/session/").nest(session::nest(db.to_owned()));
app.at("/authlib/").nest(authlib::nest(db.to_owned()));
// Listen
app.listen(format!("{}:{}", &db.config.address, &db.config.port)).await?;
Ok(())
}