Starting project template for Rust, Axum, Sqlite backend and Svelte frontend. Simple Single-Page-App (SPA) example. Does not use SvelteKit.
Work in progress (new features coming), but should be usable as a starting point.
cargo install cargo-generatecargo generate AndreiBozantan/svelte-axum-project -n <your-project-name>--template optionInstall the following:
Change current directory in the project folder:
cd <your-project-name> - to go to the project root folder.Initialization - run once before starting in dev mode:
npm run dev:initRun the project in dev mode, with hot reloading:
npm run devBy default, the backend will be available at http://localhost:3000 and the frontend at http://localhost:5173.
In dev mode, the vite config is set to proxy the backend API requests to the backend server, so you can access the API at http://localhost:5173/.
Execute npm run build in the project root folder, to build the frontend and backend in release mode. The npm script will build the frontend before the backend, as the frontend static files are embedded in the backend binary.
Optionally, you can execute npm run clean before the build, to remove all previous build artifacts, including the node_modules folders, so that the build starts from a clean state.
After running the clean command, you have to run npm run dev:init once, to reinitialize the project before running in dev mode.
The frontend static files are embedded directly into the Rust binary at compile time using the rust-embed crate.
Whenever you make changes to the frontend code and want them to be reflected in the backend server (the one running on port 3000), you must:
cd frontend && npm run buildcargo run (or cargo build)cargo run (debug) or cargo build --release, the files currently sitting in frontend/dist will be baked into the resulting executable.npm run dev), you typically don't need to worry about embedding. The Vite dev server (port 5173) serves the frontend with hot-reloading and proxies API requests to the backend. You only need to build/embed when preparing for a production-like test or final deployment../backendRun cargo run from inside the repo root folder to start the backend server independently from the frontend.
The backend can be configured using TOML files in the project root directory:
configs.default.toml - Default configurationconfigs.development.toml - Development-specific overridesconfigs.production.toml - Production configuration exampleconfigs.local.toml - Local overrides (git-ignored)You can run the database migrations by using the migrate command provided by the backend.
It will run all pending migrations from the migrations directory, or the embedded migrations if the directory does not exist.
./your-app migrate run # run this in production
or
cargo sqlx migrate run # run this in development
When deploying to production, do not copy the migrations directory to the production server. You should use the embedded migrations instead, which are included in the binary.
./frontend.Run npm run dev from inside the ./frontend directory to start serving the frontend.
This template includes Google OAuth2 SSO integration. To set it up:
@gmail.com account, this may be selected by default as "Internal" is restricted to Workspace users.openid, https://www.googleapis.com/auth/userinfo.email, and https://www.googleapis.com/auth/userinfo.profile.http://localhost:5173 (for the Svelte dev server).http://localhost:3000/api/auth/oauth/google/callback.⚠️ IMPORTANT SECURITY NOTE: Never commit OAuth secrets to git!
configs.local.toml in the project root directory (this file is already in .gitignore).[oauth] section from configs.default.toml into your configs.local.toml.[oauth]
google_client_id = "your-client-id-here"
google_client_secret = "your-client-secret-here"
Alternatively, you can use environment variables: APP_OAUTH_GOOGLE_CLIENT_ID and APP_OAUTH_GOOGLE_CLIENT_SECRET.
npm run devOAuth2 users are stored in the same users table with:
sso_provider: "google"sso_id: Google user IDpassword_hash: NULL (since OAuth users don't have passwords)The OAuth flow generates the same JWT tokens as regular login, so all existing authentication middleware works seamlessly.