routes: Move project routes to controllers module Much like other routes, just move the files and clean up the tree.

zj
Sep 21, 2021, 9:08 AM
F5DMFQAO2IVQXQQ36Z5FRW5WNXKNTY2NAWMRR3VXIOWTPJ5UUDVAC

Dependencies

  • [2] S6TFYMRG routes: Move pijul routes to controllers module As with the root routes, now the pijul routes are moved. The mounting of the routes is still done in main.rs though the controller module now collects them. This should DRY this code
  • [3] W3M3C7CC Initial commit This change includes a very small hello world application server written in Rust using Rocket.rs. Managing dependencies is done with Nix as that works well between Linux and Mac for me.
  • [4] TWIZ7QV4 db: Add interface to add a project Right now a project has a name, and an owner which is hardcoded to 1. This is because basically I'm speedrunning to implement push/pull of Pijul and then revisit to add depth to features and tests. Model code is now split into files properly too.
  • [5] FS2NWBVN pijul: Start of push/pull work This change includes one API endpoint, .pijul. It allows for getting a channels remote ID. A lot of plumbing around repositories is added too, from init to opening pristine and actions like it.
  • [6] KFVJ3KMW frontend: Introduce navigation bar Minor changes to the front-end mostly, to allow users to register, sign in, and sign out. The sign out route is changed to a GET endpoint, as links in HTML cannot DELETE.
  • [7] ZGXZ2IRI Cleanup leftover files Recorded earlier, but uninteded. Now it's cleaned up again.
  • [8] K4JNAJOF database: Connect to postgres on Rocket boot As database I've chosen PostgreSQL, as my personal experience has been good with it. This change allows Rocket to connect to the database on booting the server. It depends on the DATABASE_URL being set, and for now circumvents the Rocket config helpers as it seemed faster to be up and running this way.
  • [9] 5UNA2DEA routes: Register and authenticate users Allow users to sign up, and sign in/sign out. The routes are added, though the design of the pages is very bare bones still, it's hard to go through the full flow to demo. On the server side: Passwords are stored encrypted in the database with salts. This uses the PG encrypt tooling to prevent against bugs and maintainance costs on this project. When a user is signed in, the user ID is set in a private cookie. Rocket has Guards for routes, which has not been implemented yet for this project.
  • [10] 3E77DEMD routes: Move root route to controllers module The routes were now in modules in the root, which created a messy situation for the future. For now the routes will be moved to the controllers module and later additional changes will be done to further clean this up.
  • [11] DSWQKJRH users: Introduce User guard for routes Rockets guards are very powerful to disallow users for certain routes. This far this wasn't implemented, and allowed no-one other than the first user to sign up. This change introduces the User guard and employs it for a few routes. The guard works by checking the encrypted cookie for the user_id, and perform a database lookup on it.

Change contents

  • file deletion: projects.rs (----------)
    [3.179][3.561:596](),[3.596][3.597:597]()
    use crate::database::Database;
    use lazy_static::lazy_static;
    use regex::Regex;
    use rocket::{
    form::{self, Context, Error, Form},
    response::{Flash, Redirect},
    Route, State,
    };
    use rocket_dyn_templates::Template;
    #[get("/new")]
    Template::render("projects/new", &Context::default())
    }
    fn validate_project_name<'v>(name: &String) -> form::Result<'v, ()> {
    lazy_static! {
    static ref RE: Regex = Regex::new(r"\A[\w\d]{2,20}\z").unwrap();
    }
    if !RE.is_match(&name) {
    Err(Error::validation(
    "only up to 20 letters or digets are to be used as project name",
    ))?;
    }
    Ok(())
    }
    #[derive(FromForm)]
    struct NewProject {
    #[field(validate = validate_project_name())]
    name: String,
    }
    #[post("/", data = "<project>")]
    async fn create(
    db: &State<Database>,
    project: Form<NewProject>,
    ) -> Result<Flash<Redirect>, Flash<Redirect>> {
    Ok(_) => Ok(Flash::success(Redirect::to("/"), "Project is created")),
    Err(_e) => Err(Flash::error(
    Redirect::to("/projects/new"),
    "Something went wrong",
    )),
    }
    }
    pub fn routes() -> Vec<Route> {
    routes![new, create]
    }
    // TODO replace this with user
    match projects::create(db, user.id, project.name.clone()).await {
    user: User,
    use crate::models::projects;
    fn new(_user: User) -> Template {
    use crate::models::users::User;
  • file addition: projects.rs (----------)
    [3.85]
    use crate::database::Database;
    use crate::models::users::User;
    use lazy_static::lazy_static;
    use regex::Regex;
    use rocket::{
    form::{self, Context, Error, Form},
    response::{Flash, Redirect},
    Route, State,
    };
    use rocket_dyn_templates::Template;
    #[get("/projects/new")]
    fn new(_user: User) -> Template {
    Template::render("projects/new", &Context::default())
    }
    fn validate_project_name<'v>(name: &String) -> form::Result<'v, ()> {
    lazy_static! {
    static ref RE: Regex = Regex::new(r"\A[\w\d]{2,20}\z").unwrap();
    }
    if !RE.is_match(&name) {
    Err(Error::validation(
    "only up to 20 letters or digets are to be used as project name",
    ))?;
    }
    Ok(())
    }
    #[derive(FromForm)]
    struct NewProject {
    #[field(validate = validate_project_name())]
    name: String,
    }
    use crate::models::projects;
    #[post("/projects", data = "<project>")]
    async fn create(
    db: &State<Database>,
    project: Form<NewProject>,
    user: User,
    ) -> Result<Flash<Redirect>, Flash<Redirect>> {
    // TODO replace this with user
    match projects::create(db, user.id, project.name.clone()).await {
    Ok(_) => Ok(Flash::success(Redirect::to("/"), "Project is created")),
    Err(_e) => Err(Flash::error(
    Redirect::to("/projects/new"),
    "Something went wrong",
    )),
    }
    }
    pub fn routes() -> Vec<Route> {
    routes![new, create]
    }
  • edit in src/controllers/mod.rs at line 4
    [2.4132]
    [2.4132]
    mod projects;
  • replacement in src/controllers/mod.rs at line 8
    [3.452][2.4143:4190]()
    [root::routes(), pijul::routes()].concat()
    [3.452]
    [3.471]
    [root::routes(), pijul::routes(), projects::routes()].concat()