✨ Adds index.html
This commit is contained in:
@@ -78,7 +78,7 @@ pub async fn get(id: &str, meta: ReqMeta, state: &State<AppState>) -> Result<Nam
|
||||
}
|
||||
}
|
||||
|
||||
NamedFile::open(Path::new(state.image_path.as_str()))
|
||||
NamedFile::open(Path::new(state.static_dir.as_str()).join("image.png"))
|
||||
.await
|
||||
.map_err(|_| Status::InternalServerError)
|
||||
}
|
||||
|
||||
17
src/main.rs
17
src/main.rs
@@ -7,21 +7,26 @@ mod schema;
|
||||
use crate::api::hit;
|
||||
use crate::api::image;
|
||||
use crate::api::tracker;
|
||||
use crate::models::AppState;
|
||||
use chrono::Local;
|
||||
use diesel::{Connection, PgConnection};
|
||||
use rocket::State;
|
||||
use rocket::fs::NamedFile;
|
||||
use rocket::http::Status;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> &'static str {
|
||||
"Hello world!"
|
||||
async fn index(state: &State<AppState>) -> Result<NamedFile, Status> {
|
||||
NamedFile::open(Path::new(state.static_dir.as_str()).join("index.html"))
|
||||
.await
|
||||
.map_err(|_| Status::InternalServerError)
|
||||
}
|
||||
|
||||
// TODO: add auth
|
||||
|
||||
fn setup_logging() -> Result<(), fern::InitError> {
|
||||
let level_raw = env::var("LOG_LEVEL").unwrap_or("INFO".to_string());
|
||||
let level = log::LevelFilter::from_str(&level_raw).expect("LOG_LEVEL invalid");
|
||||
@@ -49,11 +54,11 @@ fn rocket() -> _ {
|
||||
setup_logging().expect("Failed to setup logging");
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
let image_path = env::var("IMAGE_PATH").expect("IMAGE_PATH must be set");
|
||||
let static_dir = env::var("STATIC_DIR").expect("STATIC_DIR must be set");
|
||||
let db = PgConnection::establish(&database_url)
|
||||
.expect(&format!("Error connecting to {}", database_url));
|
||||
|
||||
let app_data = models::AppState::new(db, image_path);
|
||||
let app_data = AppState::new(db, static_dir);
|
||||
rocket::build()
|
||||
.manage(app_data)
|
||||
.mount("/", routes![index])
|
||||
|
||||
@@ -6,14 +6,14 @@ pub mod tracker;
|
||||
|
||||
pub struct AppState {
|
||||
pub db: Arc<Mutex<PgConnection>>,
|
||||
pub image_path: String,
|
||||
pub static_dir: String,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(db: PgConnection, image_path: String) -> Self {
|
||||
pub fn new(db: PgConnection, static_dir: String) -> Self {
|
||||
AppState {
|
||||
db: Arc::new(Mutex::new(db)),
|
||||
image_path,
|
||||
static_dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user