Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
Middleware provides JSON message format support for Velaros WebSocket connections. It handles parsing incoming JSON messages and serializing outgoing messages according to the Velaros JSON message envelope format.
Message Format ¶
Incoming messages should be JSON objects with the following structure:
{
"id": "optional-message-id", // For request/reply correlation
"path": "/route/path", // Required: determines which handler to execute
"data": { /* your data here */ } // Optional: message payload
}
Outgoing messages are automatically wrapped in an envelope:
{
"id": "message-id", // Present if replying or making a request
"data": { /* your response */ } // Your response data
}
Special Response Types ¶
The middleware provides special handling for certain response types:
- Error (string) -> {"error": "message"}
- FieldError/[]FieldError -> {"error": "Validation error", "fields": [...]}
- string -> {"message": "your string"}
- Other types -> {"data": yourData}
Usage ¶
router := velaros.NewRouter()
router.Use(json.Middleware())
router.Bind("/users/:id", func(ctx *velaros.Context) {
var req GetUserRequest
ctx.ReceiveInto(&req) // Automatically uses JSON unmarshaling
ctx.Send(user) // Automatically serialized to JSON
})
The middleware also validates the Sec-WebSocket-Protocol header, expecting "velaros-json" if present.
Types ¶
type Error ¶
type Error string
Error represents a JSON wrapped error. If you want to return a JSON wrapped error, you can use this type. The JSON response will be {"error": "your error message"}.
type FieldError ¶
A FieldError can be used as a response body to indicate that a message body failed validation. The response will be a JSON object with the field name as the key and the error message as the value. A slice of FieldErrors can also be used to return multiple validation errors. The response will be a JSON object like { "error": "Validation error", "fields": [ { "field1": "error message" }, { "field2": "error message" } ] }.