Core
The core API provides the fundamental building blocks for Azu applications, including endpoints, requests, responses, and the main application class.
Azu::Application
The main application class that orchestrates your Azu web application.
Methods
start(middleware : Array(Azu::Handler::Base) = [] of Azu::Handler::Base)
start(middleware : Array(Azu::Handler::Base) = [] of Azu::Handler::Base)Starts the Azu application with the specified middleware stack.
# Start with default middleware
Azu.start
# Start with custom middleware
Azu.start [
Azu::Handler::Rescuer.new,
Azu::Handler::Logger.new,
Azu::Handler::CORS.new,
Azu::Handler::Static.new
]stop
stopGracefully stops the application.
Azu.stopAzu::Endpoint
Base class for all HTTP endpoints in Azu.
Methods
get(path : String)
get(path : String)post(path : String)
post(path : String)put(path : String)
put(path : String)patch(path : String)
patch(path : String)delete(path : String)
delete(path : String)Define HTTP routes for the endpoint.
struct UserEndpoint
include Azu::Endpoint
get "/users/:id"
post "/users"
put "/users/:id"
delete "/users/:id"
def call
# Implementation
end
endcall
callThe main method that handles the HTTP request. Must be implemented by each endpoint.
def call
# Your endpoint logic here
endAzu::Request
Represents an HTTP request with type-safe parameter access.
Properties
params- Hash of route parametersquery- Hash of query parametersbody- Request body contentheaders- HTTP headersmethod- HTTP methodpath- Request path
Request Methods
param(key : String) : String
param(key : String) : StringGet a route parameter by key.
def call
user_id = request.param("id")
# user_id is guaranteed to be a String
endquery(key : String) : String?
query(key : String) : String?Get a query parameter by key.
def call
page = request.query("page") || "1"
endheader(key : String) : String?
header(key : String) : String?Get an HTTP header by key.
def call
content_type = request.header("Content-Type")
endAzu::Response
Base class for all HTTP responses in Azu.
Response Methods
status(code : Int32)
status(code : Int32)Set the HTTP status code.
def call
response.status(201)
# ... rest of response
endheader(key : String, value : String)
header(key : String, value : String)Set an HTTP header.
def call
response.header("Content-Type", "application/json")
# ... rest of response
endbody(content : String)
body(content : String)Set the response body.
def call
response.body("Hello, World!")
endAzu::Router
Handles HTTP routing in Azu applications.
Router Methods
add_route(method : String, path : String, handler : Azu::Endpoint)
add_route(method : String, path : String, handler : Azu::Endpoint)Add a route to the router.
router.add_route("GET", "/users/:id", UserEndpoint.new)route(method : String, path : String) : Azu::Endpoint?
route(method : String, path : String) : Azu::Endpoint?Find a route handler for the given method and path.
handler = router.route("GET", "/users/123")Azu::Component
Base class for real-time components in Azu.
Component Methods
content
contentGenerate the component's HTML content.
def content
# Return HTML string
endon_event(name : String, data : Hash(String, String))
on_event(name : String, data : Hash(String, String))Handle client-side events.
def on_event(name, data)
case name
when "click"
# Handle click event
end
endAzu::Channel
Base class for WebSocket channels in Azu.
Channel Methods
on_connect
on_connectCalled when a client connects to the channel.
def on_connect
# Connection logic
endon_message(message : String)
on_message(message : String)Called when a message is received from the client.
def on_message(message)
# Handle incoming message
endon_disconnect
on_disconnectCalled when a client disconnects from the channel.
def on_disconnect
# Cleanup logic
endAzu::Configuration
Configuration management for Azu applications.
Configuration Properties
port- Server port (default: 3000)host- Server host (default: "0.0.0.0")environment- Application environmentdebug- Debug mode flag
Configuration Methods
configure(&block)
configure(&block)Configure the application.
Azu.configure do |config|
config.port = 8080
config.host = "localhost"
config.debug = true
endAzu::Environment
Environment detection and configuration.
Environment Methods
development? : Bool
development? : BoolCheck if running in development mode.
if Azu::Environment.development?
# Development-specific logic
endproduction? : Bool
production? : BoolCheck if running in production mode.
if Azu::Environment.production?
# Production-specific logic
endAzu::Cache
Caching system for Azu applications.
Cache Methods
get(key : String) : String?
get(key : String) : String?Get a value from the cache.
value = Azu::Cache.get("user:123")set(key : String, value : String, ttl : Time::Span? = nil)
set(key : String, value : String, ttl : Time::Span? = nil)Set a value in the cache.
Azu::Cache.set("user:123", "John Doe", 1.hour)delete(key : String)
delete(key : String)Delete a value from the cache.
Azu::Cache.delete("user:123")Azu::Templates
Template rendering system for Azu applications.
Template Methods
render(template : String, context : Hash(String, String) = {} of String => String) : String
render(template : String, context : Hash(String, String) = {} of String => String) : StringRender a template with the given context.
html = Azu::Templates.render("user.html", {"name" => "John"})register_template(name : String, content : String)
register_template(name : String, content : String)Register a template.
Azu::Templates.register_template("user.html", "<h1>{{ name }}</h1>")Error Handling
Azu provides comprehensive error handling through the Azu::Response::Error class.
Azu::Response::Error
Base class for all Azu errors.
class CustomError < Azu::Response::Error
def initialize(message : String)
super(message, 400)
end
endError Response
Error responses automatically set appropriate HTTP status codes and provide error details.
def call
raise CustomError.new("Invalid input")
rescue CustomError => e
response.status(e.status_code)
response.body(e.message)
endPerformance Monitoring
Azu includes built-in performance monitoring for components and endpoints.
Component Performance
Components automatically track rendering time and memory usage.
class UserComponent < Azu::Component
def content
# Component content
end
endEndpoint Performance
Endpoints can be monitored for response times and resource usage.
struct UserEndpoint
include Azu::Endpoint
def call
# Endpoint logic
end
endNext Steps
Learn about Request Validation
Explore Template Rendering
Understand WebSocket Channels
See Component System
Last updated
Was this helpful?
