HTTP Server Example
Build an HTTP event processing pipeline with request routing and response handling.
Overview
This example demonstrates:
- HTTP request as event source
- Path-based routing with conditions
- Request transformation
- Response generation
Example
go
type ListUsersRequest struct {
Name string `in:"query=name"`
Email string `in:"query=email"`
}
func (l ListUsersRequest) Get(key string) (any, error) {
switch key {
case "name":
return l.Name, nil
case "email":
return l.Email, nil
default:
return nil, fmt.Errorf("unknown: %s", key)
}
}
type CreateUserRequest struct {
Name string `json:"name"`
Email string `json:"email"`
}
func (c CreateUserRequest) Get(key string) (any, error) {
switch key {
case "name":
return c.Name, nil
case "email":
return c.Email, nil
default:
return nil, fmt.Errorf("unknown: %s", key)
}
}Running
bash
go run main.go
# Test
curl http://localhost:8080/api/users
curl -X POST http://localhost:8080/api/users -d '{"name":"bob"}'Key Concepts
- Generic HTTP source with type-safe event parsing via
httpin - Matcher-based routing to register source with
httppackage - Shared source for multiple route rules
- Type-safe handlers for each endpoint