Skip to content

senzenn/rust-rest-api-axum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Axum Auth API

A complete Rust API with authentication and post management using Axum and SQLite.

Features

  • User authentication (register, login, profile)
  • JWT token authentication
  • Post CRUD operations
  • SQLite database
  • Input validation
  • CORS support

Quick Start

  1. Setup

    git clone <repository-url>
    cd api-rustone
  2. Environment

    # Create .env file
    DATABASE_URL=sqlite:./api_rust_one.db
    JWT_SECRET=your-super-secret-jwt-key
    RUST_LOG=info
  3. Run

    cargo run

Server starts at http://127.0.0.1:8081

API Endpoints

Auth

  • POST /auth/register - Register user
  • POST /auth/login - Login user
  • GET /auth/profile - Get profile (auth required)
  • PUT /auth/profile - Update profile (auth required)

Posts

  • GET /posts - Get all posts
  • GET /posts/{id} - Get specific post
  • POST /posts - Create post (auth required)
  • GET /posts/my - Get user's posts (auth required)
  • PUT /posts/{id} - Update post (auth required)
  • DELETE /posts/{id} - Delete post (auth required)

Testing

Run Tests

# All tests
cargo test

# Integration tests only
cargo test --test integration_test

# Unit tests only
cargo test --lib

Manual API Testing

# Start server
cargo run

# Test endpoints
curl -X POST http://localhost:8081/auth/register \
  -H "Content-Type: application/json" \
  -d '{"name": "Test User", "email": "[email protected]", "password": "TestPass123"}'

curl -X POST http://localhost:8081/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email": "[email protected]", "password": "TestPass123"}'

# Use returned token for protected endpoints
curl -X GET http://localhost:8081/auth/profile \
  -H "Authorization: Bearer YOUR_TOKEN_HERE"

Response Format

Success:

{
  "message": "Success message",
  "data": { ... }
}

Error:

{
  "error": "Error type",
  "message": "Error description"
}

Project Structure

src/
├── main.rs              # App entry point
├── lib.rs               # Library exports
├── model/               # Data models
├── handlers/            # Route handlers
├── db/                  # Database setup
└── helpers/             # Utilities

Tech Stack

  • Framework: Axum
  • Database: SQLite
  • Auth: JWT + bcrypt
  • Validation: Custom regex
  • Logging: Tracing # axum-rest-auth

rust-rest-api-axum

axum-rest-auth-axum

rust-rest-api-axum

rust-rest-api-axum

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages