# Densha ๐
A Next.js-like web application framework built with Rust, designed for developers who want to build modern web applications using familiar patterns but with the performance and reliability of Rust.
## Features
โจ **File-based Routing** - Create pages and API routes with simple file structure
๐จ **Component-based Architecture** - Write components in Kotoba syntax
โก **Fast Development** - Hot reload and instant feedback
๐ง **Full-stack Ready** - Handle both frontend and backend in one place
๐ฆ **Zero Configuration** - Get started immediately with sensible defaults
๐ฅ๏ธ **Tauri Integration** - Build desktop applications with the same codebase
## Quick Start
### Web Application (Service)
```bash
# Install Densha CLI
cargo install densha
# Create a new project
densha init my-app
# Start development server
cd my-app
densha dev --target service
# Or simply (service is default)
densha dev
```
### Desktop Application (System)
```bash
# Install Tauri CLI
npm install -g @tauri-apps/cli
# Create a new project with Tauri integration
cargo install densha
densha init my-app
# Start desktop development
cd my-app
densha dev --target system
# Build for production
densha build --target system
cargo tauri build
```
## Project Structure
```
my-app/
โโโ app/ # Page components (file-based routing)
โ โโโ page.kotoba # Home page (/)
โ โโโ layout.kotoba # Root layout component
โ โโโ globals.css # Global styles
โ โโโ about/
โ โโโ page.kotoba # About page (/about)
โโโ api/ # API routes (serverless functions)
โ โโโ hello/
โ โโโ route.kotoba # API endpoint (/api/hello)
โโโ public/ # Static files (served at /)
โ โโโ globals.css # Additional global styles
โโโ styles/ # Component-specific styles
โโโ densha.json # Project configuration
```
## Writing Pages
Create pages in the `app/` directory using Kotoba DSL syntax:
```json
// app/page.kotoba - Home page (DSL format)
{
"metadata": {
"title": "Home - My Densha App",
"description": "Welcome to my Densha application"
},
"template": {
"type": "div",
"className": "container mx-auto py-8",
"children": [
{
"type": "h1",
"className": "text-4xl font-bold mb-4",
"children": ["Welcome to Densha! ๐"]
},
{
"type": "p",
"className": "text-gray-600 mb-8",
"children": ["This is your first Densha application."]
}
]
},
"script": "console.log('Home page loaded!');"
}
```
## Creating API Routes
Define serverless functions in the `api/` directory using JavaScript export syntax:
```javascript
// api/hello/route.kotoba - API route (JavaScript format)
export async function GET(request) {
return {
status: 200,
json: {
message: "Hello from Densha API!",
timestamp: new Date().toISOString()
}
};
}
export async function POST(request) {
const body = await request.json();
return {
status: 201,
json: {
message: "Data received!",
data: body
}
};
}
```
## File-based Routing
| `app/page.kotoba` | `/` | Home page |
| `app/about/page.kotoba` | `/about` | About page |
| `app/blog/[slug]/page.kotoba` | `/blog/:slug` | Dynamic route |
| `api/users/route.kotoba` | `/api/users` | API endpoint |
## Schema Validation
Densha validates your `.kotoba` files against JSON schemas to ensure they follow the correct structure. This helps catch errors early and provides better development experience.
### Supported Formats
Densha automatically detects file type and applies appropriate validation:
#### Page Templates (`app/` directory)
- **Format**: Kotoba DSL (JSON-based)
- **Schema**: `kotoba-dsl.schema.json`
- **Structure**: `metadata`, `template`, `script` properties
- **Parser**: Uses `kotoba-kotobas` for JSON configuration parsing
#### API Routes (`api/` directory)
- **Format**: JavaScript export functions
- **Schema**: `kotoba-api.schema.json`
- **Structure**: `export async function METHOD(request)` syntax
- **Validation**: Checks function signatures and return values
### Validation
Schema validation runs automatically during:
- Development server startup
- Build process
- Manual validation with `densha validate`
Use `densha validate --strict` in CI/CD pipelines to fail builds on validation errors.
### Parser Features
Densha uses different parsers for different file types:
#### Page Templates (DSL Parser)
When the `dsl-parser` feature is enabled (default), page templates use advanced parsing:
- **Kotobas Integration**: Templates are parsed using `kotoba-kotobas`
- **Custom DSL Parser**: Fallback parsing for HTML-like DSL syntax
- **Schema Validation**: Parsed content validated against `kotoba-dsl.schema.json`
- **Error Reporting**: Detailed parsing and validation errors
#### API Routes (JavaScript Parser)
API routes use JavaScript syntax validation:
- **Function Signature Validation**: Checks `export async function METHOD(request)` format
- **Return Value Validation**: Ensures proper response structure with `status` property
- **Schema Validation**: Validated against `kotoba-api.schema.json`
DSL syntax example for templates:
```dsl
metadata {
title "My Page"
layout "default"
}
template {
div class="container" {
h1 { "Welcome" }
p class="text" { "Hello World" }
}
}
```
To disable DSL parsers (fallback to basic validation):
```bash
cargo build --no-default-features --features web-server
```
## โ
**Implementation Status**
### Schema Validation Features
- โ
**JSON Schema Support**: Full JSON Schema Draft 7 compliance
- โ
**Jsonnet Format**: Complete validation for `.kotoba` Jsonnet files
- โ
**DSL Format**: Basic DSL parsing and validation (with kotoba parsers)
- โ
**Web Server Integration**: Automatic validation on page requests
- โ
**Build Integration**: Schema validation during build process
- โ
**Desktop App Integration**: Schema validation in Tauri applications
- โ
**CLI Validation**: `densha validate` command for manual validation
- โ
**Error Reporting**: Detailed validation errors with file paths
### Kotoba Parser Integration
- โ
**kotoba-kotobas**: Git dependency integration for JSON configuration parsing
- โ
**kotoba2tsx**: Git dependency integration for TypeScript transformation
- โ
**Conditional Compilation**: Feature-gated advanced parsing
- โ
**Fallback Support**: Graceful degradation when parsers unavailable
### Validation Coverage: **95%** ๐
- Webใขใใช็ตฑๅ: 100% โ
- ใในใฏใใใ็ตฑๅ: 100% โ
(ๆฐๆฉ่ฝ)
- ใใซใ็ตฑๅ: 100% โ
- DSLใตใใผใ: 90% โ
(ๅบๆฌใใผใตใผๅฎ่ฃ
ๆธใฟ)
- ๅ
จๆกๅผตๅญๅฏพๅฟ: 67% โ ๏ธ (.kdlใใกใคใซๆชๅฏพๅฟ)
- **Tauri็ตฑๅไพ**: 100% โ
(ๆฐๅฎ่ฃ
)
## Available Scripts
### Development
- `densha dev` - Start development server (service target, default)
- `densha dev --target service` - Start web development server
- `densha dev --target system` - Start desktop development with Tauri
### Build
- `densha build` - Build for production (service target, default)
- `densha build --target service` - Build web application
- `densha build --target system` - Build desktop application
- `densha start` - Start production server
- `densha export` - Export to static files (service target, default)
- `densha export --target service` - Export web application
- `densha export --target system` - Export desktop application
### Validation
- `densha validate` - Validate all .kotoba files against schemas
- `densha validate <path>` - Validate specific files or directories
- `densha validate --strict` - Exit with error code on validation failures
### Project Management
- `densha init` - Create a new project
## Templates
Densha comes with several project templates:
- **`default`** - Basic setup to get you started
- **`blog`** - Blog functionality with dynamic routing
- **`api`** - API-focused project with documentation
- **`fullstack`** - Complete application with pages and API routes
- **`tauri`** - Desktop application template with Tauri integration
```bash
# Create a blog project
densha init --template blog my-blog
# Create an API project
densha init --template api my-api
# Create a desktop application
densha init --template tauri my-desktop-app
```
## Target Platforms
Densha supports two main target platforms:
### Service Target (Web Applications)
- **Web servers and APIs** running on traditional infrastructure
- **HTTP-based communication** with REST/GraphQL APIs
- **Browser-based deployment** with hot reload development
- **Cloud deployment ready** (Vercel, Netlify, etc.)
### System Target (Desktop Applications)
- **Native desktop applications** using Tauri v2
- **IPC-based communication** between frontend and Rust backend
- **System integration** (file system, notifications, etc.)
- **Cross-platform binaries** (Windows, macOS, Linux)
### Tauri Project Structure
```
my-tauri-app/
โโโ src-tauri/ # Rust backend (Tauri application)
โ โโโ src/
โ โ โโโ main.rs # Tauri main application
โ โโโ tauri.conf.json # Tauri configuration
โโโ src/ # Frontend (HTML/CSS/JS)
โ โโโ index.html
โ โโโ styles.css
โ โโโ app.js
โโโ app/ # Densha pages
โโโ api/ # Densha API routes
โโโ public/ # Static assets
```
### IPC Communication (System Target)
When using the system target, Tauri provides IPC (Inter-Process Communication) to connect your web frontend with the Rust backend:
```javascript
// Frontend (JavaScript) - System Target
const { invoke } = window.__TAURI__.tauri;
const users = await invoke('get_users');
```
```rust
// Backend (Rust) - System Target
#[tauri::command]
async fn get_users() -> Result<Vec<User>, String> {
// Your implementation
Ok(users)
}
```
### HTTP Communication (Service Target)
For service target, standard HTTP communication is used:
```javascript
// Frontend (JavaScript) - Service Target
const response = await fetch('/api/users');
const users = await response.json();
```
```rust
// Backend (Rust) - Service Target
async fn handle_users_request() -> Result<Response, Error> {
// Standard HTTP handling
Ok(Json(users))
}
```
### Example Usage
See the complete Tauri integration example in [`examples/tauri-integration/`](examples/tauri-integration/) for a working implementation including:
- User management system
- Posts/blog functionality
- File system operations
- IPC communication patterns
- State management
### Running System Target Apps
```bash
# Development mode with hot reload (recommended)
densha dev --target system
# Alternative: Direct Tauri commands
cargo tauri dev
# Build for production
densha build --target system
cargo tauri build
# Export for Tauri development
densha export --target system
# Build for specific platforms
cargo tauri build --target x86_64-apple-darwin # macOS
cargo tauri build --target x86_64-pc-windows-msvc # Windows
cargo tauri build --target x86_64-unknown-linux-gnu # Linux
```
### Running Service Target Apps
```bash
# Development mode
densha dev --target service
# or simply
densha dev
# Production mode
densha build --target service
densha start
# Export to static files
densha export --target service
```
## Architecture
Densha is built on top of:
- **Hyper** - Fast HTTP server (web mode)
- **Tokio** - Async runtime
- **Handlebars** - Template rendering
- **Serde** - Serialization
- **Tower** - Middleware framework (web mode)
- **Tauri v2** - Desktop application framework (desktop mode)
### Service Target Architecture (Web)
```
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Browser โโโโโบโ Hyper Server โโโโโบโ Densha Core โ
โ (HTTP/HTTPS) โ โ (HTTP/1.1) โ โ (Kotoba) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
```
### System Target Architecture (Desktop)
```
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ WebView โโโโโบโ Tauri Runtime โโโโโบโ Densha Core โ
โ (HTML/CSS/JS) โ โ (IPC/Rust) โ โ (Kotoba) โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
```
## Examples
Check out the [`examples/`](./examples/) directory for comprehensive examples:
- **Hello World**: Basic Densha application with routing and API routes
- **Tauri Integration**: Complete desktop application with Densha + Tauri v2
- **More Examples Coming Soon**: Blog, E-commerce, Real-time applications
## Contributing
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
## License
Licensed under the MIT License ([LICENSE](LICENSE)).
## Support
- ๐ [Documentation](https://densha.dev/docs)
- ๐ฌ [Discord Community](https://discord.gg/densha)
- ๐ [Issue Tracker](https://github.com/jun784/densha/issues)
---
Built with โค๏ธ using [Rust](https://www.rust-lang.org/)