trifid/trifid-api/src/main.rs

131 lines
5.0 KiB
Rust
Raw Normal View History

2023-04-04 13:56:05 +00:00
// trifid-api, an open source reimplementation of the Defined Networking nebula management server.
// Copyright (C) 2023 c0repwn3r
//
// 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/>.
2023-04-02 19:25:52 +00:00
use actix_request_identifier::RequestIdentifier;
2023-05-11 00:32:19 +00:00
use actix_web::{
web::{Data, JsonConfig},
App, HttpResponse, HttpServer,
};
2023-04-04 01:53:32 +00:00
use log::{info, Level};
2023-04-02 16:08:36 +00:00
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
2023-05-11 00:32:19 +00:00
use std::error::Error;
use std::time::Duration;
2023-05-31 17:24:15 +00:00
use actix_cors::Cors;
2023-04-04 01:53:32 +00:00
2023-04-02 16:08:36 +00:00
use crate::config::CONFIG;
2023-04-02 17:06:16 +00:00
use crate::error::{APIError, APIErrorsResponse};
2023-05-15 18:51:27 +00:00
use crate::keystore::keystore_init;
2023-04-02 19:25:52 +00:00
use crate::tokens::random_id_no_id;
2023-05-11 00:32:19 +00:00
use trifid_api_migration::{Migrator, MigratorTrait};
2023-02-20 18:42:15 +00:00
2023-05-11 00:32:19 +00:00
pub mod auth_tokens;
2023-05-14 17:47:49 +00:00
pub mod codegen;
2023-04-02 16:08:36 +00:00
pub mod config;
2023-05-11 00:32:19 +00:00
pub mod crypto;
pub mod cursor;
2023-04-02 17:06:16 +00:00
pub mod error;
2023-05-14 17:47:49 +00:00
pub mod keystore;
2023-04-02 19:47:50 +00:00
pub mod magic_link;
2023-05-11 00:32:19 +00:00
pub mod routes;
pub mod timers;
pub mod tokens;
2023-04-02 19:25:52 +00:00
pub struct AppState {
2023-05-15 18:51:27 +00:00
pub conn: DatabaseConnection,
2023-04-02 19:25:52 +00:00
}
2023-02-20 18:42:15 +00:00
2023-04-02 16:08:36 +00:00
#[actix_web::main]
2023-02-03 02:39:41 +00:00
async fn main() -> Result<(), Box<dyn Error>> {
2023-04-02 16:08:36 +00:00
simple_logger::init_with_level(Level::Debug).unwrap();
2023-02-03 02:39:41 +00:00
2023-05-14 17:47:49 +00:00
info!("Creating keystore...");
2023-05-15 18:51:27 +00:00
let _keystore = keystore_init()?;
2023-05-14 17:47:49 +00:00
2023-04-02 16:08:36 +00:00
info!("Connecting to database at {}...", CONFIG.database.url);
2023-02-03 02:39:41 +00:00
2023-04-02 16:08:36 +00:00
let mut opt = ConnectOptions::new(CONFIG.database.url.clone());
opt.max_connections(CONFIG.database.max_connections)
.min_connections(CONFIG.database.min_connections)
.connect_timeout(Duration::from_secs(CONFIG.database.connect_timeout))
.acquire_timeout(Duration::from_secs(CONFIG.database.acquire_timeout))
.idle_timeout(Duration::from_secs(CONFIG.database.idle_timeout))
.max_lifetime(Duration::from_secs(CONFIG.database.max_lifetime))
.sqlx_logging(CONFIG.database.sqlx_logging)
.sqlx_logging_level(log::LevelFilter::Info);
2023-02-03 02:39:41 +00:00
2023-04-02 16:08:36 +00:00
let db = Database::connect(opt).await?;
2023-02-03 02:39:41 +00:00
2023-04-02 17:06:16 +00:00
info!("Performing database migration...");
Migrator::up(&db, None).await?;
let data = Data::new(AppState { conn: db });
2023-04-02 17:06:16 +00:00
HttpServer::new(move || {
App::new()
2023-05-31 17:24:15 +00:00
.wrap(Cors::permissive())
2023-04-02 17:06:16 +00:00
.app_data(data.clone())
2023-05-15 18:51:27 +00:00
.app_data(
JsonConfig::default()
.content_type_required(false)
.error_handler(|err, _req| {
let api_error: APIError = (&err).into();
actix_web::error::InternalError::from_response(
err,
HttpResponse::BadRequest().json(APIErrorsResponse {
errors: vec![api_error],
}),
)
.into()
2023-05-11 00:32:19 +00:00
}),
2023-05-15 18:51:27 +00:00
)
2023-04-02 19:25:52 +00:00
.wrap(RequestIdentifier::with_generator(random_id_no_id))
2023-04-02 19:47:50 +00:00
.service(routes::v1::auth::magic_link::magic_link_request)
.service(routes::v1::signup::signup_request)
2023-04-02 23:12:08 +00:00
.service(routes::v1::auth::verify_magic_link::verify_magic_link_request)
.service(routes::v1::totp_authenticators::totp_authenticators_request)
2023-04-03 01:47:32 +00:00
.service(routes::v1::verify_totp_authenticators::verify_totp_authenticators_request)
2023-04-03 17:28:12 +00:00
.service(routes::v1::auth::totp::totp_request)
2023-04-03 22:39:49 +00:00
.service(routes::v1::networks::get_networks)
2023-04-04 01:53:14 +00:00
.service(routes::v1::organization::create_org_request)
2023-04-04 13:31:41 +00:00
.service(routes::v1::networks::get_network_request)
2023-04-06 00:02:21 +00:00
.service(routes::v1::roles::create_role_request)
.service(routes::v1::roles::get_roles)
.service(routes::v1::roles::get_role)
2023-04-06 00:58:45 +00:00
.service(routes::v1::roles::delete_role)
2023-04-27 16:53:10 +00:00
.service(routes::v1::roles::update_role_request)
.service(routes::v1::trifid::trifid_extensions)
2023-04-28 00:40:56 +00:00
.service(routes::v1::hosts::get_hosts)
2023-04-28 01:47:18 +00:00
.service(routes::v1::hosts::create_hosts_request)
.service(routes::v1::hosts::get_host)
2023-05-09 01:46:33 +00:00
.service(routes::v1::hosts::delete_host)
2023-05-09 14:38:31 +00:00
.service(routes::v1::hosts::edit_host)
2023-05-11 00:32:19 +00:00
.service(routes::v1::hosts::block_host)
2023-05-11 17:13:30 +00:00
.service(routes::v1::hosts::enroll_host)
2023-05-11 17:32:33 +00:00
.service(routes::v1::hosts::create_host_and_enrollment_code)
2023-05-14 17:47:49 +00:00
.service(routes::v2::enroll::enroll)
.service(routes::v1::dnclient::dnclient)
2023-05-31 01:52:29 +00:00
.service(routes::v2::whoami::whoami)
.service(routes::v1::hosts::get_host_overrides)
.service(routes::v1::hosts::update_host_overrides)
2023-05-11 00:32:19 +00:00
})
.bind(CONFIG.server.bind)?
.run()
.await?;
2023-02-03 02:39:41 +00:00
Ok(())
2023-04-02 16:08:36 +00:00
}