Crypto Ticker

Construya un ticker de criptomonedas en tiempo real con autenticación por API key y streaming WebSocket. Este tutorial demuestra seguridad basada en tokens, configuración de middleware, y manejo de WebSocket basado en procesos.

Arquitectura

flowchart TB
    subgraph Clients
        Browser[Cliente Browser]
        API[Cliente API]
    end

    subgraph "Capa HTTP"
        Server[http.server
gateway :8081] Static[http.static
public/] subgraph "Router Público" CORS1[cors middleware] AuthEndpoint[auth_token
POST /auth/token] end subgraph "Router WS /ws" CORS2[cors middleware] TokenAuth[token_auth middleware] WSEndpoint[ws_ticker
GET /ws/ticker] WSRelay[websocket_relay] end end subgraph "Capa Seguridad" TokenStore[security.token_store
tokens] Policy[security.policy
user_policy] MemStore[store.memory
token_data] end subgraph "Almacenamiento" DB[db.sql.sqlite
auth.db] end subgraph "Capa Procesos" Supervisor[process.supervisor
processes] WSHandler[ws_handler
per-connection] Ticker[ticker
singleton] end subgraph "Externo" CryptoAPI[API Precios Crypto] end Browser -->|"GET /"| Static API -->|"POST /auth/token"| CORS1 Browser -->|"WS /ws/ticker"| CORS2 CORS1 --> AuthEndpoint AuthEndpoint -->|validate| TokenStore AuthEndpoint -->|"issue token"| API CORS2 --> TokenAuth TokenAuth -->|validate| TokenStore TokenAuth --> WSEndpoint WSEndpoint -->|spawn| Supervisor Supervisor --> WSHandler WSEndpoint --> WSRelay WSRelay <-->|"messages"| WSHandler MemStore --> TokenStore Policy -->|attached to token| TokenStore AuthEndpoint -->|lookup API key| DB WSHandler -->|subscribe| Ticker Ticker -->|broadcast| WSHandler WSRelay <-->|"ws frames"| Browser Ticker -->|fetch prices| CryptoAPI

Flujo de Seguridad

  1. Intercambio de API Key: El cliente hace POST de API key a /auth/token. El handler valida contra base de datos, crea un actor con la user_policy, y emite un token firmado HMAC.

  2. Autenticación de Token: Las conexiones WebSocket pasan por middleware token_auth que valida el Bearer token y restaura el contexto de seguridad (actor + políticas).

  3. Generación de Procesos: El endpoint WebSocket genera un proceso handler. Como el token incluye la user_policy, el spawn está autorizado.

  4. Routing de Mensajes: El middleware websocket_relay enruta frames WebSocket al proceso handler como mensajes.

Configuración

_index.yaml completo:

version: "1.0"
namespace: app

entries:
  # Base de datos para API keys
  - name: db
    kind: db.sql.sqlite
    file: "./data/auth.db"
    lifecycle:
      auto_start: true

  # Almacén respaldo de tokens
  - name: token_data
    kind: store.memory
    lifecycle:
      auto_start: true

  # Almacén de tokens con firma HMAC
  - name: tokens
    kind: security.token_store
    store: app:token_data
    token_length: 32
    default_expiration: "1h"
    token_key: "demo-secret-key-change-in-production"

  # Política de seguridad para usuarios autenticados
  - name: user_policy
    kind: security.policy
    policy:
      actions: "*"
      resources: "*"
      effect: allow
    groups:
      - user

  # Process host
  - name: processes
    kind: process.host
    lifecycle:
      auto_start: true

  # Migración de base de datos
  - name: migrate
    kind: process.lua
    source: file://migrate.lua
    method: main
    modules: [sql, logger, crypto]

  - name: migrate-service
    kind: process.service
    process: app:migrate
    host: app:processes
    lifecycle:
      auto_start: true

  # Broadcaster del ticker
  - name: ticker
    kind: process.lua
    source: file://ticker.lua
    method: main
    modules: [logger, time, json, crypto]

  - name: ticker-service
    kind: process.service
    process: app:ticker
    host: app:processes
    lifecycle:
      auto_start: true

  # Handler WebSocket (generado por conexión)
  - name: ws_handler
    kind: process.lua
    source: file://ws_handler.lua
    method: main
    modules: [logger, json]

  # Servidor HTTP
  - name: gateway
    kind: http.service
    addr: ":8081"
    lifecycle:
      auto_start: true

  # Router público (sin auth)
  - name: public_router
    kind: http.router
    meta:
      server: app:gateway
    middleware:
      - cors
    options:
      cors.allow.origins: "*"

  # Router WebSocket (con auth)
  - name: ws_router
    kind: http.router
    meta:
      server: app:gateway
    prefix: /ws
    middleware:
      - cors
      - token_auth
    options:
      cors.allow.origins: "*"
      token_auth.store: "app:tokens"
    post_middleware:
      - websocket_relay
    post_options:
      wsrelay.allowed.origins: "*"

  # Archivos estáticos
  - name: public_fs
    kind: fs.directory
    directory: ./public

  - name: static
    kind: http.static
    meta:
      server: app:gateway
    path: /
    fs: app:public_fs
    static_options:
      spa: true
      index: index.html

  # Intercambio de token de auth
  - name: auth_token
    kind: function.lua
    source: file://auth_token.lua
    method: handler
    modules: [http, sql, crypto, security, json]

  - name: auth_token.endpoint
    kind: http.endpoint
    meta:
      router: app:public_router
    method: POST
    path: /auth/token
    func: app:auth_token

  # Endpoint WebSocket ticker
  - name: ws_ticker
    kind: function.lua
    source: file://ws_ticker.lua
    method: handler
    modules: [http, json, security, logger]

  - name: ws_ticker.endpoint
    kind: http.endpoint
    meta:
      router: app:ws_router
    method: GET
    path: /ticker
    func: app:ws_ticker

Para producción, use token_key_env para leer la clave HMAC desde una variable de entorno en lugar de hardcodearla. Ver Sistema de Entorno.

Intercambio de Token

auth_token.lua - valida API keys y emite tokens firmados HMAC:

local http = require("http")
local sql = require("sql")
local security = require("security")

local function handler()
    local req = http.request()
    local res = http.response()

    local body, parse_err = req:body_json()
    if parse_err then
        res:set_status(http.STATUS.BAD_REQUEST)
        res:write_json({error = "JSON inválido"})
        return
    end

    local api_key = body.api_key
    if not api_key or #api_key == 0 then
        res:set_status(http.STATUS.BAD_REQUEST)
        res:write_json({error = "api_key requerido"})
        return
    end

    local db, db_err = sql.get("app:db")
    if db_err then
        res:set_status(http.STATUS.INTERNAL_ERROR)
        res:write_json({error = "base de datos no disponible"})
        return
    end

    local rows, query_err = db:query(
        "SELECT user_id, role FROM api_keys WHERE api_key = ?",
        {api_key}
    )
    db:release()

    if query_err or #rows == 0 then
        res:set_status(http.STATUS.UNAUTHORIZED)
        res:write_json({error = "API key inválido"})
        return
    end

    local user = rows[1]

    -- Crear actor con identidad de usuario
    local actor = security.new_actor("user:" .. user.user_id, {
        role = user.role,
        user_id = user.user_id
    })

    -- Adjuntar user_policy al scope
    local policy, _ = security.policy("app:user_policy")
    local scope = policy and security.new_scope({policy}) or security.new_scope()

    -- Emitir token firmado HMAC
    local store, store_err = security.token_store("app:tokens")
    if store_err then
        res:set_status(http.STATUS.INTERNAL_ERROR)
        res:write_json({error = "almacén de tokens no disponible"})
        return
    end

    local token, token_err = store:create(actor, scope, {
        expiration = "1h",
        meta = {ip = req:remote_addr()}
    })
    store:close()

    if token_err then
        res:set_status(http.STATUS.INTERNAL_ERROR)
        res:write_json({error = "creación de token falló"})
        return
    end

    res:write_json({
        token = token,
        user_id = user.user_id,
        role = user.role,
        expires_in = 3600
    })
end

return { handler = handler }

Endpoint WebSocket

ws_ticker.lua - genera un proceso handler para cada conexión autenticada:

local http = require("http")
local json = require("json")
local security = require("security")
local logger = require("logger")

local function handler()
    local req = http.request()
    local res = http.response()

    if req:method() ~= http.METHOD.GET then
        res:set_status(http.STATUS.METHOD_NOT_ALLOWED)
        res:write_json({error = "método no permitido"})
        return
    end

    -- Actor es establecido por middleware token_auth
    local actor = security.actor()
    if not actor then
        res:set_status(http.STATUS.UNAUTHORIZED)
        res:write_json({error = "autenticación requerida"})
        return
    end

    local user_id = actor:id()

    -- Generar proceso handler (autorizado por user_policy en token)
    local pid, err = process.spawn("app:ws_handler", "app:processes", user_id)
    if err then
        logger:error("spawn failed", {error = tostring(err)})
        res:set_status(http.STATUS.INTERNAL_ERROR)
        res:write_json({error = "falló al crear handler"})
        return
    end

    -- Configurar websocket_relay para enrutar mensajes al handler
    res:set_header("X-WS-Relay", json.encode({
        target_pid = tostring(pid),
        metadata = {user_id = user_id, auth_time = os.time()}
    }))
end

return { handler = handler }

Handler de Conexión

El middleware websocket_relay envía automáticamente mensajes de ciclo de vida al proceso handler:

  • ws.join - Conexión establecida, incluye client_pid para enviar respuestas
  • ws.message - Cliente envió un mensaje
  • ws.leave - Conexión cerrada (enviado automáticamente al desconectar)

ws_handler.lua - maneja estos mensajes de ciclo de vida:

local logger = require("logger")
local json = require("json")

local function main(user_id)
    local inbox = process.inbox()
    local client_pid = nil
    local subscribed = false

    logger:info("handler started", {user_id = user_id})

    while true do
        local msg, ok = inbox:receive()
        if not ok then break end

        local topic = msg:topic()
        local data = msg:payload():data()

        if topic == "ws.join" then
            client_pid = data.client_pid

            -- Suscribir con nuestro PID para monitoreo de crash
            process.send("ticker", "subscribe", {
                client_pid = client_pid,
                handler_pid = process.pid()
            })
            subscribed = true

            -- Enviar bienvenida
            process.send(client_pid, "ws.send", {
                type = "text",
                data = json.encode({type = "welcome", user_id = user_id})
            })

            logger:info("client joined", {user_id = user_id, client_pid = client_pid})

        elseif topic == "ws.message" then
            local content = json.decode(data.data)
            if content and content.type == "ping" then
                process.send(client_pid, "ws.send", {
                    type = "text",
                    data = json.encode({type = "pong"})
                })
            end

        elseif topic == "ws.leave" then
            -- Relay envía esto automáticamente al desconectar
            logger:info("client left", {user_id = user_id, reason = data.reason})
            if subscribed then
                process.send("ticker", "unsubscribe", {handler_pid = process.pid()})
            end
            break
        end
    end

    return 0
end

return { main = main }

Broadcasting

ticker.lua - mantiene suscripciones y hace broadcast de actualizaciones de precios:

local logger = require("logger")
local time = require("time")
local json = require("json")
local crypto = require("crypto")

-- mapping handler_pid -> client_pid
local subscriptions = {}

local prices = {
    ["BTC-USD"] = 42000.00,
    ["ETH-USD"] = 2500.00,
    ["SOL-USD"] = 95.00
}

local function broadcast(message)
    local data = json.encode(message)
    for _, client_pid in pairs(subscriptions) do
        process.send(client_pid, "ws.send", {type = "text", data = data})
    end
end

local function update_prices()
    for symbol, price in pairs(prices) do
        local bytes = crypto.random.bytes(2)
        local rand = (bytes:byte(1) * 256 + bytes:byte(2)) / 65535.0
        local factor = (rand - 0.5) * 0.002
        prices[symbol] = price * (1 + factor)
        prices[symbol] = tonumber(string.format("%.2f", prices[symbol]))
    end
end

local function get_updates()
    local updates = {}
    for symbol, price in pairs(prices) do
        table.insert(updates, {symbol = symbol, price = price, timestamp = os.time()})
    end
    return updates
end

local function main()
    local inbox = process.inbox()
    local events = process.events()

    local ticker, ticker_err = time.ticker("10ms")
    if ticker_err then
        logger:error("failed to create ticker", {error = tostring(ticker_err)})
        return 1
    end
    local tick_ch = ticker:response()

    process.registry.register("ticker")
    logger:info("ticker started", {pid = process.pid()})

    while true do
        local r = channel.select {
            inbox:case_receive(),
            events:case_receive(),
            tick_ch:case_receive()
        }

        if r.channel == tick_ch then
            update_prices()
            if next(subscriptions) then
                broadcast({type = "ticker", data = get_updates()})
            end

        elseif r.channel == events then
            local event = r.value
            if event.kind == process.event.EXIT then
                -- Handler terminó, remover suscripción
                if subscriptions[event.from] then
                    logger:info("handler exited", {handler_pid = event.from})
                    subscriptions[event.from] = nil
                end
            end

        else
            local msg = r.value
            local topic = msg:topic()
            local data = msg:payload():data()

            if topic == "subscribe" then
                local handler_pid = data.handler_pid
                local client_pid = data.client_pid

                subscriptions[handler_pid] = client_pid
                process.monitor(handler_pid)

                logger:info("subscribed", {handler_pid = handler_pid, client_pid = client_pid})

                process.send(client_pid, "ws.send", {
                    type = "text",
                    data = json.encode({type = "ticker", data = get_updates()})
                })

            elseif topic == "unsubscribe" then
                subscriptions[data.handler_pid] = nil
                logger:info("unsubscribed", {handler_pid = data.handler_pid})
            end
        end
    end
end

return { main = main }

Ejecución

wippy init
wippy run

Abra http://localhost:8081 e ingrese el API key demo mostrado en los logs.

Puntos Clave

Concepto Implementación
Firma de token security.token_store con clave HMAC
Validación de token Middleware token_auth en router
Autorización security.policy adjunta al scope del token
Ciclo de vida WebSocket websocket_relay envía ws.join/ws.leave automáticamente
Limpieza de handler process.monitor(handler_pid) detecta crashes
Mapa de suscripciones subscriptions[handler_pid] = client_pid

Ver También