Add a session manager
This commit is contained in:
parent
a778d590f8
commit
3ce2fb04eb
2 changed files with 25 additions and 22 deletions
5
TODO.md
5
TODO.md
|
|
@ -12,9 +12,8 @@
|
||||||
[x] Create a `GET /new-project` endpoint that returns a HTML page with a project creation form. Keep it simple.
|
[x] Create a `GET /new-project` endpoint that returns a HTML page with a project creation form. Keep it simple.
|
||||||
[x] Add .env file support using the dotenv crate. Load environment variables on server startup.
|
[x] Add .env file support using the dotenv crate. Load environment variables on server startup.
|
||||||
[x] Create a `GET /login` endpoint that returns a simple HTML login form (username and password fields).
|
[x] Create a `GET /login` endpoint that returns a simple HTML login form (username and password fields).
|
||||||
[ ] Create a `POST /login` endpoint that validates credentials against USERNAME and PASSWORD environment variables. On success, set a session cookie.
|
[x] Add a rouille::session manager to set a session cookie.
|
||||||
[ ] Implement session management. Create a simple in-memory session store that tracks authenticated sessions by cookie token.
|
[ ] Create a `POST /login` endpoint that validates credentials against USERNAME and PASSWORD environment variables.
|
||||||
[ ] Add authentication middleware or helper function to check if a request has a valid session cookie.
|
|
||||||
[ ] Protect write endpoints (POST /projects, and any future write operations) with authentication. Redirect to /login if not authenticated.
|
[ ] Protect write endpoints (POST /projects, and any future write operations) with authentication. Redirect to /login if not authenticated.
|
||||||
[ ] Add a login button to the front page (GET /projects) that links to /login.
|
[ ] Add a login button to the front page (GET /projects) that links to /login.
|
||||||
[ ] When logged in, show a "Create Project" button on the front page that links to /new-project.
|
[ ] When logged in, show a "Create Project" button on the front page that links to /new-project.
|
||||||
|
|
|
||||||
42
src/main.rs
42
src/main.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use maud::{DOCTYPE, html};
|
use maud::{DOCTYPE, html};
|
||||||
use rouille::{router, try_or_400};
|
use rouille::{router, try_or_400, session};
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
mod project;
|
mod project;
|
||||||
|
|
@ -14,24 +14,28 @@ fn main() {
|
||||||
println!("Starting server on localhost:8080");
|
println!("Starting server on localhost:8080");
|
||||||
|
|
||||||
rouille::start_server("localhost:8080", move |request| {
|
rouille::start_server("localhost:8080", move |request| {
|
||||||
router!(request,
|
// Wrap all requests with session management
|
||||||
(GET) ["/"] => {
|
// This sets a session cookie with a 1-hour timeout
|
||||||
rouille::Response::redirect_302("/projects")
|
session::session(request, "SESSION", 3600, |_session| {
|
||||||
},
|
router!(request,
|
||||||
(GET) ["/main.css"] => {
|
(GET) ["/"] => {
|
||||||
let css = include_bytes!("main.css");
|
rouille::Response::redirect_302("/projects")
|
||||||
rouille::Response::from_data("text/css", css.as_ref())
|
},
|
||||||
},
|
(GET) ["/main.css"] => {
|
||||||
(GET) ["/project-card.js"] => {
|
let css = include_bytes!("main.css");
|
||||||
let js = include_bytes!("project-card.js");
|
rouille::Response::from_data("text/css", css.as_ref())
|
||||||
rouille::Response::from_data("application/javascript", js.as_ref())
|
},
|
||||||
},
|
(GET) ["/project-card.js"] => {
|
||||||
(GET) ["/projects"] => display_projects(),
|
let js = include_bytes!("project-card.js");
|
||||||
(GET) ["/new-project"] => new_project_form(),
|
rouille::Response::from_data("application/javascript", js.as_ref())
|
||||||
(GET) ["/login"] => login_form(),
|
},
|
||||||
(POST) ["/projects"] => create_project(request),
|
(GET) ["/projects"] => display_projects(),
|
||||||
_ => rouille::Response::empty_404()
|
(GET) ["/new-project"] => new_project_form(),
|
||||||
)
|
(GET) ["/login"] => login_form(),
|
||||||
|
(POST) ["/projects"] => create_project(request),
|
||||||
|
_ => rouille::Response::empty_404()
|
||||||
|
)
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue