svelte-axum-backend Svelte Themes

Svelte Axum Backend

Axum backend for simple REST API to save user to SQLite database

Rust Axum Backend with SQLite

A modern REST API backend built with Rust, Axum web framework, and SQLite database. (Vibe coded)

๐Ÿš€ Features

  • Fast & Lightweight: Built with Rust for maximum performance
  • Modern Web Framework: Uses Axum for routing and HTTP handling
  • SQLite Database: Embedded database with SQLx for type-safe queries
  • CORS Support: Configured for frontend integration
  • Modular Architecture: Clean separation of concerns
  • Static File Serving: Serves frontend assets from dist/ directory

๐Ÿ“‹ Prerequisites

  • Rust (latest stable version)
  • Cargo (comes with Rust)

๐Ÿ› ๏ธ Installation

  1. Clone the repository or navigate to the backend directory
  2. Install dependencies:
    cargo build
    

๐Ÿš€ Running the Server

Start the development server:

cargo run

The server will start on http://localhost:3000

๐Ÿ“š API Endpoints

GET /api/hello

Returns a greeting message.

Response:

{
  "message": "Hello from Axum backend with SQLite!"
}

GET /api/users

Retrieves all users from the database.

Response:

[
  {
    "id": 1,
    "name": "Prop"
  },
  {
    "id": 2,
    "name": "Bob"
  }
]

POST /api/users

Creates a new user.

Request Body:

{
  "name": "John Doe"
}

Response:

{
  "id": 3,
  "name": "John Doe"
}

๐Ÿ—‚๏ธ Project Structure

src/
โ”œโ”€โ”€ main.rs          # Application entry point and server setup
โ”œโ”€โ”€ database.rs      # Database initialization and connection
โ”œโ”€โ”€ handlers.rs      # Route handlers and business logic
โ”œโ”€โ”€ models.rs        # Data structures and schemas
โ””โ”€โ”€ routes.rs        # Route definitions and configuration

๐Ÿ—„๏ธ Database

The application uses SQLite with the following schema:

Users Table

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL
);

The database file (users.db) is automatically created on first run with initial seed data.

๐Ÿ”ง Configuration

CORS Settings

  • Allowed Origin: http://localhost:5173 (Vite dev server)
  • Allowed Methods: GET, POST
  • Allowed Headers: Content-Type

Server Settings

  • Host: 0.0.0.0
  • Port: 3000

๐Ÿงช Development

Building

cargo build

Running in Development

cargo run

Checking Code

cargo check

๐Ÿ“ฆ Dependencies

  • axum: Modern web framework
  • tokio: Async runtime
  • sqlx: Async SQL toolkit with SQLite support
  • serde: Serialization/deserialization
  • tower: Service abstractions and middleware
  • tower-http: HTTP-specific middleware (CORS, static files)

๐Ÿ”’ Environment

The application is configured for development with:

  • Debug logging enabled
  • CORS configured for local frontend development
  • SQLite database for easy local development

๐Ÿ“ License

This project is part of a learning/playground repository.

Top categories

Loading Svelte Themes