Code Patterns

Common patterns shared across all UNS functions.

Valkey Config Loading

// Go — cached 30 seconds
key := "fnkit:config:" + os.Getenv("FUNCTION_TARGET")
val, _ := rdb.Get(ctx, key).Result()
json.Unmarshal([]byte(val), &config)

// Node.js
const raw = await redis.get(`fnkit:config:${process.env.FUNCTION_TARGET}`);
const config = JSON.parse(raw);

Auto-Table Creation

CREATE TABLE IF NOT EXISTS uns_state ( ... );

Runs once per container lifetime. No migration scripts.

UNS Topic Path Parsing

v1.0 / enterprise / site1 / area1 / cnc-01 / status
 [0]      [1]        [2]     [3]     [4]      [5]

parts := strings.Split(topic, "/")
enterprise, site, area, machine := parts[1], parts[2], parts[3], parts[4]

In-Memory State Tracking

uns-state and uns-productivity maintain in-memory trackers. State lost on restart but re-initialises from cache on next poll.

Lazy Connections

DB and cache connections created on first request, not startup.

JSON Response Pattern

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)