put routes into routes.rs
parent
c18625285b
commit
96b666ab41
52
src/main.rs
52
src/main.rs
|
@ -5,50 +5,18 @@ extern crate diesel;
|
|||
extern crate diesel_migrations;
|
||||
|
||||
use actix::prelude::*;
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{error, get, middleware, post, web, App, Error, HttpResponse, HttpServer};
|
||||
use actix_web::{middleware, App, HttpServer};
|
||||
use clap::{App as ClapApp, Arg};
|
||||
use std::env;
|
||||
mod database;
|
||||
mod dataops;
|
||||
mod models;
|
||||
mod schema;
|
||||
use log::info;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
struct State {
|
||||
directory: PathBuf,
|
||||
db: Addr<database::DbExecutor>,
|
||||
hasher: Addr<dataops::HasherWorker>,
|
||||
}
|
||||
|
||||
#[get("/raw/{hash}")]
|
||||
async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> Result<NamedFile, Error> {
|
||||
let response = state
|
||||
.db
|
||||
.send(database::RetrieveByHash {
|
||||
hash: hash.into_inner(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
match response {
|
||||
Ok(result) => match result {
|
||||
Some(path) => Ok(NamedFile::open(path)?),
|
||||
None => Err(error::ErrorNotFound("NOT FOUND")),
|
||||
},
|
||||
Err(e) => Err(error::ErrorInternalServerError(e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/api/refresh")]
|
||||
async fn api_refresh(state: web::Data<State>) -> Result<HttpResponse, Error> {
|
||||
dataops::update_directory(&state.directory, &state.db, &state.hasher)
|
||||
.await
|
||||
.map_err(|_| error::ErrorInternalServerError("UPDATE ERROR"))?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
mod database;
|
||||
mod dataops;
|
||||
mod models;
|
||||
mod routes;
|
||||
mod schema;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
|
@ -89,14 +57,14 @@ fn main() -> std::io::Result<()> {
|
|||
// Start HTTP server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(State {
|
||||
.data(routes::State {
|
||||
directory: path.clone(),
|
||||
db: db_addr.clone(),
|
||||
hasher: hash_addr.clone(),
|
||||
})
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(get_raw)
|
||||
.service(api_refresh)
|
||||
.service(routes::get_raw)
|
||||
.service(routes::api_refresh)
|
||||
})
|
||||
.bind(&bind)?
|
||||
.run();
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
use actix::prelude::*;
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{error, get, post, web, Error, HttpResponse};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub struct State {
|
||||
pub directory: PathBuf,
|
||||
pub db: Addr<crate::database::DbExecutor>,
|
||||
pub hasher: Addr<crate::dataops::HasherWorker>,
|
||||
}
|
||||
|
||||
#[get("/raw/{hash}")]
|
||||
pub async fn get_raw(state: web::Data<State>, hash: web::Path<String>) -> Result<NamedFile, Error> {
|
||||
let response = state
|
||||
.db
|
||||
.send(crate::database::RetrieveByHash {
|
||||
hash: hash.into_inner(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
match response {
|
||||
Ok(result) => match result {
|
||||
Some(path) => Ok(NamedFile::open(path)?),
|
||||
None => Err(error::ErrorNotFound("NOT FOUND")),
|
||||
},
|
||||
Err(e) => Err(error::ErrorInternalServerError(e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/api/refresh")]
|
||||
pub async fn api_refresh(state: web::Data<State>) -> Result<HttpResponse, Error> {
|
||||
crate::dataops::update_directory(&state.directory, &state.db, &state.hasher)
|
||||
.await
|
||||
.map_err(|_| error::ErrorInternalServerError("UPDATE ERROR"))?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
Loading…
Reference in New Issue