The base class for all tinystruct applications. It provides:
public class MyApp extends AbstractApplication {
@Override
public void init() {
// Initialize application
}
@Override
public String version() {
return "1.0.0";
}
}
Actions are the core building blocks of tinystruct applications. They handle both web requests and CLI commands.
@Action(
value = "endpoint", // URL pattern or command name
description = "Description", // Action description
mode = Action.Mode.ALL // Execution mode (ALL, WEB, CLI)
)
@Action("users") // Automatically matches /users, /users/123, /users/123/posts
Tinystruct automatically matches the right functionality based on the URL pattern. There’s no need to define variables like {id}
in the @Action annotation. The framework intelligently routes requests to the appropriate method based on the parameters.
# Application settings
application.name=MyApp
application.mode=development
# Server settings
server.port=8080
server.host=localhost
# Database settings
database.type=MySQL
database.url=jdbc:mysql://localhost:3306/mydb
String appName = getConfiguration().get("application.name");
int port = Integer.parseInt(getConfiguration().get("server.port"));
Repository repository = Type.MySQL.createRepository();
repository.connect(getConfiguration());
// Execute query
List<Row> results = repository.query("SELECT * FROM users");
// Execute update
repository.execute("UPDATE users SET name = ? WHERE id = ?",
"John Doe", 1);
@Action("api/data")
public String getData(Request request, Response response) {
String param = request.getParameter("key");
// Set content type to JSON
response.headers().add(Header.CONTENT_TYPE.set("application/json"));
// Create JSON response
Builder builder = new Builder();
builder.put("key", param);
return builder.toString();
}
@Action(value = "generate",
description = "Generate POJO objects",
mode = Action.Mode.CLI)
public void generate() {
// Command implementation
}
@Action("secure/endpoint")
public Response secureEndpoint(Request request) {
if (!isAuthenticated(request)) {
throw new UnauthorizedException();
}
// Protected code
}
@Action("admin/users")
public Response adminOnly(Request request) {
if (!hasRole(request, "ADMIN")) {
throw new ForbiddenException();
}
// Admin-only code
}
try {
// Your code
} catch (ApplicationException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new ApplicationRuntimeException(e.getMessage(), e);
}