create-rust-app icon indicating copy to clipboard operation
create-rust-app copied to clipboard

Adding Axum

Open Wulf opened this issue 3 years ago • 6 comments

Adding a backend framework

Base requirements for each framework:

  • [ ] Add new framework option to CLI, update BackendFramework enum
  • [ ] main.rs which starts the server in create-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/todos endpoints (see todo.rs below)
    • [ ] (only in production: #[cfg(not(debug_assertions))]) serves files from ./frontend/build with the index.html as the default
    • [ ] listens on port 8080
    • [ ] returns 404 for all unhandled routes
  • [ ] todo.rs which serves the CRUD endpoints for the example 'todo' service in create-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/auth routes
    • [ ] Add an extractor/guard for auth
  • Storage plugin support
    • [ ] Add an example service which shows file uploads (files.rs)

Wulf avatar Apr 01 '22 21:04 Wulf

Outline for #11

Wulf avatar Apr 01 '22 21:04 Wulf

@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.

vishalsodani avatar Apr 24 '22 15:04 vishalsodani

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 +something at 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_web would be copied into a generated project if the user selected active-web as their framework. (the main.rs+poem one would be ignored).
  • The generated project relies on the create-rust-app crate which exposes features like backend-actix_web and backend-poem. When these features are enabled, it exposes plugin features for those backends (i.e. having the backend-actix_web flag turned on should expose an auth plugin implementation for actix-web). You're more than welcome to put utility stuff for axum in the create-rust-app crate. 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::Axum around line 80
    • Also, around line 134, add "backend_axum" to the features array.
  • 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 +axum file selection logic for the remove_non_framework_files function -- I need to generalize this function eventually lol
  • In create-rust-app_cli/template/backend/, add a main.rs+axum file -- this will be the main.rs for the generated project :)
  • In create-rust-app_cli/template/backend/services, copy and paste one of the todo.rs+... files and rename it to todo.rs+axum

create-rust-app

  • In create-rust-app/Cargo.toml, just add backend_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 the create-rust-app dependency -- 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 avatar May 01 '22 16:05 Wulf

@wulf Thanks for sharing so many pointers :) This remove_non_framework_files helped me immediately.

vishalsodani avatar May 02 '22 04:05 vishalsodani

Hi @vishalsodani. Could I ask we done intergrated Axum?

langdon0003 avatar Dec 25 '22 12:12 langdon0003

Hi @vishalsodani. Could I ask we done intergrated Axum?

Sorry, no.

vishalsodani avatar Dec 26 '22 01:12 vishalsodani