From d97b8203631bd938661a4a8e70c8282bbb801021 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 19 Aug 2019 18:40:34 +0300 Subject: [PATCH] upd --- Cargo.toml | 8 ++++--- src/main.rs | 58 ++++++++++++++++----------------------------- templates/base.html | 2 ++ 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 78f5493..a0f365b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ lmdb-zero="0.4" rand="0.7" env_logger = "0.6" tera = "0.11" -# actix="0.7" -# tokio="0.1" -# futures="0.1" \ No newline at end of file + +[profile.release] +opt-level = 3 +debug = false +lto = true diff --git a/src/main.rs b/src/main.rs index 6dcf651..8174811 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -// extern crate actix; extern crate actix_files; extern crate actix_web; extern crate serde; @@ -6,7 +5,6 @@ extern crate serde; extern crate serde_derive; #[macro_use] extern crate serde_json; -#[macro_use] extern crate ledb; #[macro_use] extern crate ledb_derive; @@ -17,29 +15,14 @@ extern crate tera; use tera::Context; -// extern crate futures; - -// extern crate tokio; use actix_web::{ - error, guard, http::header, http::Method, middleware::Logger, middleware::NormalizePath, web, - App, Error, HttpRequest, HttpResponse, HttpServer, Responder, Result, + guard, http::header, middleware::Logger, web, + App, Error, HttpRequest, HttpResponse, HttpServer, Result, }; -use std::cell::Cell; use rand::seq::IteratorRandom; -use std::time::Instant; -use std::{fs, io}; -// use tokio::spawn; -// use futures::{Future}; - -// use actix::Actor; -// use actix::System; - -//use crate::tokio::prelude::Future; - -//use ledb_actix::{Document, Options, Storage, StorageAddrExt}; use ledb::{Options, Storage}; @@ -126,8 +109,8 @@ fn get_question(storage: &Storage, id: u32) -> Result, Error> { return Ok(None); } - let collection = storage.collection("questions").unwrap(); - let last_id = collection.last_id().unwrap(); + let collection = storage.collection("questions").expect("collection questions"); + let last_id = collection.last_id().expect("questions last id"); if id > last_id { Err(Error::from(())) @@ -154,7 +137,7 @@ fn show_question_details(template_file: &str, data: web::Data, id: web let body = data.template.render(template_file, &question).unwrap(); Ok(HttpResponse::Ok().content_type("text/html").body(body)) } else { - Ok(HttpResponse::Found() + Ok(HttpResponse::PermanentRedirect() .header(header::LOCATION, "/q/") .finish()) } @@ -175,15 +158,15 @@ fn show_answer(data: web::Data, id: web::Path) -> Result, req: HttpRequest) -> Result { - let collection = data.storage.collection("questions").unwrap(); +fn index(data: web::Data, _req: HttpRequest) -> Result { + let collection = data.storage.collection("questions").expect("collection questions"); let mut rng = rand::thread_rng(); - let last_id = collection.last_id().unwrap(); - let id = (1..(last_id + 1)).choose(&mut rng).unwrap(); + let last_id = collection.last_id().expect("questions last id"); + let id = (1..=last_id).choose(&mut rng).expect("random id"); - let url = req.url_for("question", &[format!("{}", id)])?; + let url = format!("/q/{}", id); - Ok(HttpResponse::Found() + Ok(HttpResponse::TemporaryRedirect() .header(header::LOCATION, url.as_str()) .finish()) } @@ -193,12 +176,12 @@ fn main() { env_logger::init(); let options: Options = serde_json::from_value(json!({ - "read_only": true, - "no_lock": true, + "read_only": true, + "no_lock": true, })) - .unwrap(); + .expect("options json parse"); - let storage = Storage::new("db", options).unwrap(); + let storage = Storage::new("db", options).expect("db open"); HttpServer::new(move || { let data = AppState { @@ -208,19 +191,20 @@ fn main() { App::new() .wrap(Logger::default()) .data(data) + .service(actix_files::Files::new("/static", "./static")) .route("/q", web::to(index)) .service( web::scope("/q") .service(actix_files::Files::new("/static", "./static")) .service( web::resource("/{id}") - .name("question") // <- set resource name, then it could be used in `url_for` + .name("question") .guard(guard::Get()) .to(show_question), ) .service( web::resource("/{id}/a/") - .name("answer") // <- set resource name, then it could be used in `url_for` + .name("answer") .guard(guard::Get()) .to(show_answer), ) @@ -228,8 +212,8 @@ fn main() { ) .route("/", web::to(index)) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("0.0.0.0:8088") + .expect("HttpServer::bind to 8088") .run() - .unwrap(); + .expect("HttpServer::run"); } diff --git a/templates/base.html b/templates/base.html index 758b3b8..59d85b4 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,6 +2,7 @@ + {% block title %}{% endblock title %} @@ -9,5 +10,6 @@ {% include "nav.html" %}
{% block content %}{% endblock content %}
+ \ No newline at end of file