mirror of https://github.com/raftario/filite.git
Basic webserver
This commit is contained in:
parent
b573d14ab2
commit
9a0c5f2583
30
src/main.rs
30
src/main.rs
|
@ -3,11 +3,14 @@ extern crate cfg_if;
|
|||
|
||||
use filite::setup::{self, Config};
|
||||
|
||||
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer};
|
||||
use std::fs;
|
||||
use actix_web::{middleware, web, App, HttpServer, HttpResponse, Responder};
|
||||
use std::process;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
use std::fs;
|
||||
|
||||
/// Performs the initial setup
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn init() -> Config {
|
||||
let data_dir = setup::get_data_dir();
|
||||
if !data_dir.exists() {
|
||||
|
@ -31,6 +34,10 @@ fn init() -> Config {
|
|||
})
|
||||
}
|
||||
|
||||
fn index() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world!")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let config = {
|
||||
cfg_if! {
|
||||
|
@ -42,4 +49,23 @@ fn main() {
|
|||
}
|
||||
};
|
||||
setup::init_logger();
|
||||
|
||||
let pool = setup::create_pool(&config.database_url, config.pool_size);
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(pool.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind(&format!("localhost:{}", config.port))
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("Can't bind webserver to specified port: {}.", e);
|
||||
process::exit(1);
|
||||
})
|
||||
.run()
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("Can't start webserver: {}.", e);
|
||||
process::exit(1);
|
||||
});
|
||||
}
|
||||
|
|
10
src/setup.rs
10
src/setup.rs
|
@ -32,13 +32,13 @@ fn get_config_path() -> PathBuf {
|
|||
#[serde(default)]
|
||||
pub struct Config {
|
||||
/// Port to listen on
|
||||
port: u16,
|
||||
pub port: u16,
|
||||
/// SQLite database connection url
|
||||
database_url: String,
|
||||
pub database_url: String,
|
||||
/// SQLite database connection pool size
|
||||
pool_size: u32,
|
||||
pub pool_size: u32,
|
||||
/// Directory where to store static files
|
||||
files_dir: PathBuf,
|
||||
pub files_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
|
@ -137,7 +137,7 @@ pub fn init_logger() {
|
|||
if cfg!(debug_assertions) && env::var_os("RUST_LOG").is_none() {
|
||||
env::set_var("RUST_LOG", "actix_web=debug");
|
||||
} else if !cfg!(debug_assertions) {
|
||||
env::set_var("RUST_LOG", "actix_web=info");
|
||||
env::set_var("RUST_LOG", "actix_web=warn");
|
||||
}
|
||||
env_logger::init();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue