create-rust-app
create-rust-app copied to clipboard
Adding Axum
Adding a backend framework
Base requirements for each framework:
- [ ] Add new framework option to CLI, update
BackendFrameworkenum - [ ]
main.rswhich starts the server increate-rust-app_cli/template/src/- [ ] sets up logging
- [ ] adds the database pool and the mailer to app's data (every framework has a way of passing data onto all handlers)
- [ ] sets up
/api/todosendpoints (seetodo.rsbelow) - [ ] (only in production:
#[cfg(not(debug_assertions))]) serves files from./frontend/buildwith theindex.htmlas the default - [ ] listens on port 8080
- [ ] returns 404 for all unhandled routes
- [ ]
todo.rswhich serves the CRUD endpoints for the example 'todo' service increate-rust-app_cli/template/src/services- [ ]
GET /: returns a JSON list of all TODO items - [ ]
GET /id: return a single JSON TODO item - [ ]
POST /: creates and returns a single JSON TODO item - [ ]
PUT /:id: updates and returns a single JSON TODO item - [ ]
DELETE /:id: deletes a single item, returns 200 status code
- [ ]
Optional requirements:
(we can get to these later)
- Auth plugin suppot
- [ ] Implement all
/api/authroutes - [ ] Add an extractor/guard for auth
- [ ] Implement all
- Storage plugin support
- [ ] Add an example service which shows file uploads (
files.rs)
- [ ] Add an example service which shows file uploads (
Outline for #11
@Wulf I would like to work on this task.
I have started by adding Axum to the enum BackendFramework and a main.rs+axum file. But, how do I go about testing just the server backend? Also, how is main.rs generated?
I would like to add features for the new backend step by step as I learn more about the axum framework and its workings.
hey @vishalsodani, hope things are well. Sorry for being super late with my reply.
It would be awesome if you added Axum! Step by step is exactly how I integrated poem-web -- my focus was on getting the "Todo" example working.
Some things you should know in general:
- in the template folders, any file with a
+somethingat the end is backend-specific and is not included in the final project if its backend isn't selected. In other words,create-rust-app_cli/template/backend/main.rs+active_webwould be copied into a generated project if the user selected active-web as their framework. (themain.rs+poemone would be ignored). - The generated project relies on the
create-rust-appcrate which exposes features likebackend-actix_webandbackend-poem. When these features are enabled, it exposes plugin features for those backends (i.e. having thebackend-actix_webflag turned on should expose an auth plugin implementation for actix-web). You're more than welcome to put utility stuff for axum in thecreate-rust-appcrate. For example, I've got some 404 not found handlers in there for both actix-web and poem with the idea being that one day we will have a 404 page that is the same across all backends (at least in development).
Apart from that, I went over the codebase once again and found places where you may want to add in Axum-related implementation / CLI options / etc. I've split the notes based on the project it pertains to:
create-rust-app_cli
- In
create-rust-app_cli/src/main.rs,- you should add an option to select
BackendFraemwork::Axumaround line 80 - Also, around line 134, add "backend_axum" to the features array.
- you should add an option to select
- In
create-rust-app_cli/src/content/project- (around line 202) you should specify the axum-only project dependencies.
- (around line 80) please add the
+axumfile selection logic for theremove_non_framework_filesfunction -- I need to generalize this function eventually lol
- In
create-rust-app_cli/template/backend/, add amain.rs+axumfile -- this will be the main.rs for the generated project :) - In
create-rust-app_cli/template/backend/services, copy and paste one of thetodo.rs+...files and rename it totodo.rs+axum
create-rust-app
- In
create-rust-app/Cargo.toml, just addbackend_axum = []as a feature at the bottom of the file (you don't need to do anything else, as long as users can specify axum as a backend -- this is because the CLI adds this feature flag in the generated project's Cargo.toml for thecreate-rust-appdependency -- it's what we did in point 1 above)
I tried to over-explain everything to make it clear so sorry if it was too wordy.
After all this you'll hopefully have a working axum project which you can tweak further.
cd repo/create-rust-app_cli
cargo run -- test-project
# don't select any plugins~
Just a note, don't generate a project named 'test' -- it's not a legal cargo project name apparently -- I've run into this so many times now haha. If you choose to use another name for your generated test project (that isn't "test-project"), you should add it to the exclude array in repo/Cargo.toml (otherwise cargo will complain and say it's part of a workspace or something like that). If all of this fails, just install the project:
cargo install --path repo/create-rust-app_cli
cd /somewhere/else
create-rust-app my-project
# don't select any plugins~
Let me know if I can help in any other way!
@wulf Thanks for sharing so many pointers :) This remove_non_framework_files helped me immediately.
Hi @vishalsodani. Could I ask we done intergrated Axum?
Hi @vishalsodani. Could I ask we done intergrated Axum?
Sorry, no.