66 lines
1.7 KiB
Rust
66 lines
1.7 KiB
Rust
use std::env;
|
|
|
|
use actix_files::Files;
|
|
use actix_web::{App, HttpResponse, HttpServer, Responder, get, web};
|
|
use scp_core::apis::configuration::Configuration;
|
|
use scp_core::apis::default_api;
|
|
|
|
mod auth;
|
|
mod db;
|
|
mod helper;
|
|
mod jobs;
|
|
mod models;
|
|
mod servers;
|
|
|
|
#[get("/api/hello")]
|
|
async fn hello() -> impl Responder {
|
|
HttpResponse::Ok().json(serde_json::json!({ "message": "hello" }))
|
|
}
|
|
|
|
async fn spa_fallback() -> actix_web::Result<actix_files::NamedFile> {
|
|
Ok(actix_files::NamedFile::open("./frontend/dist/index.html")?)
|
|
}
|
|
|
|
#[get("/api/scp/ping")]
|
|
async fn ping_netcup() -> impl Responder {
|
|
let config = Configuration::default();
|
|
let res = default_api::api_ping_get(&config).await;
|
|
|
|
res.is_ok().to_string()
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
let port = env::var("PORT")
|
|
.unwrap_or_else(|_| "8080".to_string())
|
|
.parse::<u16>()
|
|
.unwrap();
|
|
|
|
db::migrate().await.expect("Error migrating!");
|
|
|
|
let rt_handle = tokio::runtime::Handle::current();
|
|
let scheduler = jobs::init_scheduler(rt_handle.clone());
|
|
|
|
let res = HttpServer::new(|| {
|
|
App::new()
|
|
.service(hello)
|
|
.service(ping_netcup)
|
|
.service(auth::is_scp_logged_in)
|
|
.service(auth::start_flow)
|
|
.service(auth::get_user)
|
|
.service(servers::list_servers)
|
|
.service(
|
|
Files::new("/", "./frontend/dist")
|
|
.index_file("index.html")
|
|
.prefer_utf8(true),
|
|
)
|
|
.default_service(web::route().to(spa_fallback))
|
|
})
|
|
.bind(("127.0.0.1", port))?
|
|
.run()
|
|
.await;
|
|
|
|
scheduler.shutdown();
|
|
res
|
|
}
|