Files
ECC/skills/tinystruct-patterns/references/architecture.md
T
@JamesandClaude Sonnet 5 fb1f81e0d7 docs: sync tinystruct-patterns skill with latest tinystruct framework (1.7.34)
Sync skills/tinystruct-patterns against tinystruct's own canonical copy of
this skill (.agents/skills/tinystruct-patterns/), which the framework repo
keeps up to date with each release:

- Document the bin/dispatcher / bin/dispatcher.cmd launcher generation
  workflow (never hand-write or copy it; the framework generates it via
  ApplicationManager.init()).
- Add native MCP server/tool integration (org.tinystruct.mcp.*), including
  the prompt-injection warning for tool return values and overloaded-tool
  schema merging.
- Add annotation-based DB mapping (@Table/@Id/@Column) alongside XML
  mapping, the --mapping annotation generator flag, and table
  auto-creation via database.autocreate.
- Add the programmatic logging/diagnostics subsystem (ANSI console colors,
  StackWalker-based caller tracing, per-package level overrides).
- Update the architecture package map accordingly.

Also update the Japanese translation (docs/ja-JP/skills/tinystruct-patterns)
to match, which had drifted to an older, condensed version of this skill.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-24 21:10:57 +08:00

4.2 KiB

tinystruct Architecture and Configuration

When to Use

Choose tinystruct when you need a lightweight, high-performance Java framework that treats CLI and HTTP as equal citizens. Ideal for microservices, CLI utilities, and data-driven applications with a small footprint and zero-dependency JSON handling.

How It Works

Core Architecture

The framework operates on a singleton ActionRegistry that maps URL patterns (or command strings) to Action objects. When a request arrives, the system resolves the path and invokes the corresponding method handle.

Key Abstractions

Class/Interface Role
AbstractApplication Base class for all tinystruct applications. Extend this.
@Action annotation Maps a method to a URI path (web) or command name (CLI). The single routing primitive.
ActionRegistry Singleton that maps URL patterns to Action objects via regex. Never instantiate directly.
Action Wraps a MethodHandle + regex pattern + priority + Mode for dispatch.
Context Per-request state store. Access via getContext(). Holds CLI args and HTTP request/response.
Dispatcher CLI entry point (bin/dispatcher). Reads --import to load applications.
HttpServer Built-in HTTP server. Start with bin/dispatcher start --import org.tinystruct.system.HttpServer.

Package Map

org.tinystruct/
├── AbstractApplication.java      ← extend this
├── Application.java              ← interface
├── ApplicationException.java     ← checked exception
├── ApplicationRuntimeException.java ← unchecked exception
├── application/
│   ├── Action.java               ← runtime action wrapper
│   ├── ActionRegistry.java       ← singleton route registry
│   └── Context.java              ← request context
├── system/
│   ├── annotation/Action.java    ← @Action annotation + Mode enum
│   ├── Dispatcher.java           ← CLI dispatcher
│   ├── HttpServer.java           ← built-in HTTP server
│   ├── EventDispatcher.java      ← event bus
│   └── Settings.java             ← reads application.properties
├── data/
│   ├── component/Builder.java    ← JSON object (use instead of Gson/Jackson)
│   ├── component/Builders.java   ← JSON array
│   ├── component/AbstractData.java ← base POJO for DB persistence
│   ├── component/Condition.java  ← fluent SQL query builder
│   ├── component/FieldType.java  ← SQL-to-Java type mappings
│   ├── annotation/               ← @Table, @Id, @Column mapping annotations
│   ├── Mapping.java              ← reads @Table/@Column annotations or .map.xml metadata, cached per class
│   ├── tools/TableCreator.java   ← creates missing tables (database.autocreate)
│   ├── DatabaseOperator.java     ← low-level JDBC wrapper
│   └── FileEntity.java           ← file upload representation
├── http/                         ← Request, Response, Constants
│   └── SSEPushManager.java       ← Server-Sent Events management
└── net/                          ← URLRequest, HTTPHandler (outbound HTTP)

Template Behavior and Dispatch Flow

By default, the framework assumes a view template is required. If templateRequired is true, toString() looks for a .view file in src/main/resources/themes/<ClassName>.view. Use setVariable("name", value) to pass data to templates, which use {%name%} for interpolation.

Examples

Minimal Application Initialization

@Override
public void init() {
    this.setTemplateRequired(false); // Skip .view template lookup for data-only apps
    // Do NOT call setAction() here — use @Action annotation instead
}

Action Definition and CLI Invocation

@Action("hello")
public String hello() {
    return "Hello, tinystruct!";
}

Execution via Dispatcher:

bin/dispatcher hello
bin/dispatcher greet/James
bin/dispatcher echo --words "Hello" --import com.example.HelloApp

Configuration Access

Located at src/main/resources/application.properties:

String port = this.getConfiguration("server.port");