densha 0.1.4

Next.js-like web application framework built with Kotoba
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# 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

| File Path | Route | Example |
|-----------|-------|---------|
| `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/)