feat: #6 update dependency rocket to 0.4.0
This commit is contained in:
parent
e6de032ca8
commit
79b67e0c1a
11
Cargo.toml
11
Cargo.toml
@ -7,9 +7,8 @@ license = "MIT"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
rocket = "0.3.16"
|
||||
rocket_codegen = "0.3.16"
|
||||
rocket_contrib = { version = "0.3.17", default-features = true, features = ["tera_templates"] }
|
||||
rocket = "0.4.0"
|
||||
rocket_contrib = { version = "0.4.0", default-features = true, features = ["tera_templates"] }
|
||||
diesel = { version = "1.0.0", features = ["postgres", "r2d2", "chrono"] }
|
||||
dotenv = "0.9.0"
|
||||
r2d2 = "0.8"
|
||||
@ -20,8 +19,8 @@ tera = "*"
|
||||
pulldown-cmark = { version = "0.1.2", default-features = false }
|
||||
chrono = { version = "*", features = ["serde"] }
|
||||
rust-crypto = "^0.2"
|
||||
juniper = "0.10"
|
||||
juniper_codegen = "0.10"
|
||||
juniper_rocket = "0.1.3"
|
||||
juniper = "0.11.1"
|
||||
juniper_codegen = "0.11.1"
|
||||
juniper_rocket = "0.2.0"
|
||||
rand = "0.6.0"
|
||||
rss = "1.6.1"
|
@ -1,5 +1,4 @@
|
||||
#![feature(custom_attribute, plugin)]
|
||||
#![plugin(rocket_codegen)]
|
||||
#![feature(proc_macro_hygiene,decl_macro, custom_attribute, plugin)]
|
||||
extern crate chrono;
|
||||
extern crate crypto;
|
||||
#[macro_use]
|
||||
@ -12,6 +11,7 @@ extern crate juniper_codegen;
|
||||
extern crate juniper_rocket;
|
||||
extern crate pulldown_cmark;
|
||||
extern crate r2d2;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate rocket_contrib;
|
||||
@ -23,8 +23,7 @@ extern crate tera;
|
||||
extern crate rand;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use rocket_contrib::Template;
|
||||
|
||||
use rocket_contrib::templates::Template;
|
||||
mod guard;
|
||||
mod models;
|
||||
mod pg_pool;
|
||||
@ -42,7 +41,7 @@ fn main() {
|
||||
let database_url = std::env::var("DATABASE_URL").expect("database_url must be set");
|
||||
|
||||
rocket::ignite()
|
||||
.catch(catchers![
|
||||
.register(catchers![
|
||||
catacher::not_found_catcher,
|
||||
catacher::unauthorized,
|
||||
])
|
||||
|
@ -48,9 +48,8 @@ pub struct User {
|
||||
pub last_login_at: NaiveDateTime,
|
||||
}
|
||||
|
||||
#[derive_FromForm]
|
||||
#[derive(GraphQLObject)]
|
||||
#[derive(Queryable, Debug, Serialize, Insertable, AsChangeset)]
|
||||
#[derive(FromForm, Queryable, Debug, Serialize, Insertable, AsChangeset)]
|
||||
#[table_name = "setting"]
|
||||
pub struct Setting {
|
||||
pub name: String,
|
||||
|
@ -9,32 +9,30 @@ use rocket::State;
|
||||
use crate::models::Token;
|
||||
use crate::pg_pool::{DbConn, Pool};
|
||||
|
||||
#[derive_FromForm]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, FromForm)]
|
||||
pub struct LoginForm {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, FromForm, Debug)]
|
||||
pub struct Admin {
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Debug)]
|
||||
pub struct AdminToken {
|
||||
pub admin: Admin,
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive_FromForm]
|
||||
#[derive(Debug, FromForm)]
|
||||
pub struct NewPasswordForm {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive_FromForm]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, FromForm)]
|
||||
pub struct ArticleEditForm {
|
||||
pub id: Option<i32>,
|
||||
pub title: String,
|
||||
|
@ -16,32 +16,30 @@ use rocket::http::Cookies;
|
||||
use rocket::http::Status;
|
||||
use rocket::request::FlashMessage;
|
||||
use rocket::request::Form;
|
||||
use rocket::response::Failure;
|
||||
use rocket::response::Flash;
|
||||
use rocket::response::Redirect;
|
||||
use rocket_contrib::Template;
|
||||
use tera::Context;
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
#[get("/login")]
|
||||
fn admin_login() -> Template {
|
||||
pub fn admin_login() -> Template {
|
||||
let context = Context::new();
|
||||
Template::render("admin/login", &context)
|
||||
}
|
||||
|
||||
|
||||
#[post("/login", data = "<user>")]
|
||||
fn admin_authentication(user: Form<LoginForm>, conn: DbConn, mut cookies: Cookies) -> Result<Redirect, Failure> {
|
||||
pub fn admin_authentication(user: Form<LoginForm>, conn: DbConn, mut cookies: Cookies) -> Result<Redirect, Status> {
|
||||
use crate::schema::{users, users::dsl::*};
|
||||
|
||||
let user_form = user.get();
|
||||
let fetched = users::table.filter(username.eq(&user_form.username)).first::<User>(&*conn);
|
||||
let fetched = users::table.filter(username.eq(&user.username)).first::<User>(&*conn);
|
||||
if fetched.is_err() {
|
||||
return Err(Failure(Status::Unauthorized));
|
||||
return Err(Status::Unauthorized);
|
||||
}
|
||||
let user: User = fetched.unwrap();
|
||||
|
||||
if !user.authenticated(user_form.password.as_str()) {
|
||||
return Err(Failure(Status::Unauthorized));
|
||||
if !user.authenticated(user.password.as_str()) {
|
||||
return Err(Status::Unauthorized);
|
||||
}
|
||||
|
||||
cookies.add_private(Cookie::new("LOG_SESSION", user.username));
|
||||
@ -53,7 +51,7 @@ fn admin_authentication(user: Form<LoginForm>, conn: DbConn, mut cookies: Cookie
|
||||
|
||||
|
||||
#[get("/")]
|
||||
fn admin_index(admin: Admin, conn: DbConn, flash: Option<FlashMessage>) -> Template {
|
||||
pub fn admin_index(admin: Admin, conn: DbConn, flash: Option<FlashMessage>) -> Template {
|
||||
let mut context = Context::new();
|
||||
|
||||
let articles = Article::load_all(true, &conn);
|
||||
@ -67,7 +65,7 @@ fn admin_index(admin: Admin, conn: DbConn, flash: Option<FlashMessage>) -> Templ
|
||||
|
||||
|
||||
#[get("/article/new")]
|
||||
fn article_creation(_admin: Admin) -> Result<Template, Failure> {
|
||||
pub fn article_creation(_admin: Admin) -> Result<Template, Status> {
|
||||
let mut context = Context::new();
|
||||
|
||||
let article = Article {
|
||||
@ -86,12 +84,12 @@ fn article_creation(_admin: Admin) -> Result<Template, Failure> {
|
||||
|
||||
|
||||
#[get("/article/<article_id>")]
|
||||
fn article_edit(_admin: Admin, conn: DbConn, article_id: i32) -> Result<Template, Failure> {
|
||||
pub fn article_edit(_admin: Admin, conn: DbConn, article_id: i32) -> Result<Template, Status> {
|
||||
let mut context = Context::new();
|
||||
let fetched_article = Article::find(article_id, &conn);
|
||||
|
||||
if let Err(_err) = fetched_article {
|
||||
return Err(Failure(Status::NotFound));
|
||||
return Err(Status::NotFound);
|
||||
}
|
||||
|
||||
let article: Article = fetched_article.unwrap();
|
||||
@ -101,10 +99,10 @@ fn article_edit(_admin: Admin, conn: DbConn, article_id: i32) -> Result<Template
|
||||
}
|
||||
|
||||
#[post("/article", data = "<article>")]
|
||||
fn save_article(admin: Admin, conn: DbConn, article: Form<ArticleEditForm>) -> Result<Flash<Redirect>, Failure> {
|
||||
pub fn save_article(admin: Admin, conn: DbConn, article: Form<ArticleEditForm>) -> Result<Flash<Redirect>, Status> {
|
||||
use crate::schema::{articles};
|
||||
|
||||
let article = Article::form_article_edit_form(article.get(), admin.id);
|
||||
let article = Article::form_article_edit_form(&article, admin.id);
|
||||
let _fetched_article: QueryResult<Article> = match article.id {
|
||||
Some(article_id) => diesel::update(articles::table.find(article_id)).set(&article).get_result(&*conn),
|
||||
|
||||
@ -115,21 +113,21 @@ fn save_article(admin: Admin, conn: DbConn, article: Form<ArticleEditForm>) -> R
|
||||
}
|
||||
|
||||
#[post("/password", data = "<password_form>")]
|
||||
fn change_password(admin: Admin, conn: DbConn, password_form: Form<NewPasswordForm>) -> Flash<Redirect> {
|
||||
pub fn change_password(admin: Admin, conn: DbConn, password_form: Form<NewPasswordForm>) -> Flash<Redirect> {
|
||||
use crate::schema::{users};
|
||||
|
||||
let mut admin_user: User = users::table.find(admin.id).first::<User>(&*conn).unwrap();
|
||||
|
||||
admin_user.password = User::password_generate(&password_form.get().password).to_string();
|
||||
admin_user.password = User::password_generate(&password_form.password).to_string();
|
||||
let _result: QueryResult<User> = diesel::update(users::table.find(admin_user.id)).set(&admin_user).get_result(&*conn);
|
||||
Flash::new(Redirect::moved("/admin"), "success", "password is changed successfully")
|
||||
}
|
||||
|
||||
#[post("/setting", data = "<setting_form>")]
|
||||
fn change_setting(admin: Admin, conn: DbConn, setting_form: Form<Setting>) -> Flash<Redirect> {
|
||||
pub fn change_setting(admin: Admin, conn: DbConn, setting_form: Form<Setting>) -> Flash<Redirect> {
|
||||
use crate::schema::{setting};
|
||||
|
||||
let new_setting = Setting { name: setting_form.get().name.clone(), value: setting_form.get().value.clone() };
|
||||
let fetched_setting: QueryResult<Setting> = diesel::update(setting::table.find(&setting_form.get().name)).set(&new_setting).get_result(&*conn);
|
||||
let new_setting = Setting { name: setting_form.name.clone(), value: setting_form.value.clone() };
|
||||
let fetched_setting: QueryResult<Setting> = diesel::update(setting::table.find(&setting_form.name)).set(&new_setting).get_result(&*conn);
|
||||
Flash::new(Redirect::to("/admin"), "success", "setting changed")
|
||||
}
|
@ -6,16 +6,15 @@ use crate::schema::{articles::dsl::*};
|
||||
use crate::schema::articles;
|
||||
use diesel::prelude::*;
|
||||
use rocket::http::Status;
|
||||
use rocket::response::Failure;
|
||||
use rocket::response::NamedFile;
|
||||
use rocket_contrib::Template;
|
||||
use rocket_contrib::templates::Template;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tera::Context;
|
||||
|
||||
|
||||
#[get("/")]
|
||||
fn index(setting: SettingMap, conn: DbConn) -> Template {
|
||||
pub fn index(setting: SettingMap, conn: DbConn) -> Template {
|
||||
let mut context = Context::new();
|
||||
|
||||
let result = articles::table.filter(published.eq(true)).order(publish_at.desc()).load::<Article>(&*conn).expect("cannot load articles");
|
||||
@ -29,13 +28,13 @@ fn index(setting: SettingMap, conn: DbConn) -> Template {
|
||||
}
|
||||
|
||||
#[get("/archives/<archives_id>")]
|
||||
fn single_article(conn: DbConn, archives_id: i32) -> Result<Template, Failure> {
|
||||
pub fn single_article(conn: DbConn, archives_id: i32) -> Result<Template, Status> {
|
||||
let mut context = Context::new();
|
||||
|
||||
let result: Result<_, _> = articles::table.find(archives_id).first::<Article>(&*conn);
|
||||
|
||||
if let Err(_err) = result {
|
||||
return Err(Failure(Status::NotFound));
|
||||
return Err(Status::NotFound);
|
||||
}
|
||||
|
||||
let article: Article = result.unwrap();
|
||||
@ -48,22 +47,22 @@ fn single_article(conn: DbConn, archives_id: i32) -> Result<Template, Failure> {
|
||||
}
|
||||
|
||||
#[get("/statics/<file..>")]
|
||||
fn static_content(file: PathBuf) -> Result<NamedFile, Failure> {
|
||||
pub fn static_content(file: PathBuf) -> Result<NamedFile, Status> {
|
||||
let path = Path::new("static/resources/").join(file);
|
||||
let result = NamedFile::open(&path);
|
||||
if let Ok(file) = result {
|
||||
Ok(file)
|
||||
} else {
|
||||
Err(Failure(Status::NotFound))
|
||||
Err(Status::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/<archive_url>", rank = 5)]
|
||||
fn get_article_by_url(conn: DbConn, archive_url: String) -> Result<Template, Failure> {
|
||||
pub fn get_article_by_url(conn: DbConn, archive_url: String) -> Result<Template, Status> {
|
||||
let mut context = Context::new();
|
||||
let result = articles::table.filter(url.eq(archive_url)).first::<Article>(&*conn);
|
||||
if let Err(_err) = result {
|
||||
return Err(Failure(Status::NotFound));
|
||||
return Err(Status::NotFound);
|
||||
}
|
||||
|
||||
let article = result.unwrap();
|
||||
|
@ -1,5 +1,5 @@
|
||||
|
||||
use rocket_contrib::Json;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[catch(404)]
|
||||
pub fn not_found_catcher() -> String {
|
||||
@ -7,8 +7,8 @@ pub fn not_found_catcher() -> String {
|
||||
}
|
||||
|
||||
#[catch(401)]
|
||||
pub fn unauthorized() -> Json {
|
||||
Json(json!({
|
||||
pub fn unauthorized() -> JsonValue {
|
||||
json!({
|
||||
"message": "unauthorized"
|
||||
}))
|
||||
})
|
||||
}
|
@ -4,42 +4,40 @@ use rocket::State;
|
||||
use crate::graphql::Schema;
|
||||
use juniper_rocket::{GraphQLRequest, GraphQLResponse};
|
||||
use rocket_contrib::json::Json;
|
||||
use rocket::request::Form;
|
||||
use crate::request::{LoginForm, AdminToken};
|
||||
use rocket::response::Failure;
|
||||
use rocket::http::Status;
|
||||
use crate::models::{User, Token};
|
||||
use rocket::request::Form;
|
||||
|
||||
#[get("/graphiql")]
|
||||
fn graphiql() -> content::Html<String> {
|
||||
pub fn graphiql() -> content::Html<String> {
|
||||
juniper_rocket::graphiql_source("/graphql")
|
||||
}
|
||||
|
||||
#[post("/graphql/authorization", data = "<user>")]
|
||||
pub fn graphql_authorization(user: Form<LoginForm>, conn: DbConn) -> Result<Json<Token>, Failure> {
|
||||
let user_form = user.get();
|
||||
let fetched_user = User::find_by_username(&user_form.username, &conn);
|
||||
pub fn graphql_authorization(user: Form<LoginForm>, conn: DbConn) -> Result<Json<Token>, Status> {
|
||||
let fetched_user = User::find_by_username(&user.username, &conn);
|
||||
|
||||
if let None = fetched_user {
|
||||
return Err(Failure(Status::Unauthorized));
|
||||
return Err(Status::Unauthorized);
|
||||
}
|
||||
let user: User = fetched_user.unwrap();
|
||||
|
||||
if !user.authenticated(user_form.password.as_str()) {
|
||||
return Err(Failure(Status::Unauthorized));
|
||||
if !user.authenticated(user.password.as_str()) {
|
||||
return Err(Status::Unauthorized);
|
||||
}
|
||||
Ok(Json(Token::create(user.id, &conn)))
|
||||
}
|
||||
|
||||
|
||||
#[get("/graphql?<request>")]
|
||||
fn get_graphql_handler(token: AdminToken, context: DbConn, request: GraphQLRequest, state: State<Schema>) -> GraphQLResponse {
|
||||
pub fn get_graphql_handler(token: AdminToken, context: DbConn, request: GraphQLRequest, state: State<Schema>) -> GraphQLResponse {
|
||||
let schema = state;
|
||||
request.execute(&schema, &context)
|
||||
}
|
||||
|
||||
#[post("/graphql", data = "<request>")]
|
||||
fn post_graphql_handler(token: AdminToken, context: DbConn, request: GraphQLRequest, state: State<Schema>) -> GraphQLResponse {
|
||||
pub fn post_graphql_handler(token: AdminToken, context: DbConn, request: GraphQLRequest, state: State<Schema>) -> GraphQLResponse {
|
||||
let schema = state;
|
||||
request.execute(&schema, &context)
|
||||
}
|
Loading…
Reference in New Issue
Block a user