tikoci / traefik-wasm-grain

⚠️ Traefik WASM plugin using Grain and http-wasm ABI
1 stars 0 forks source link

HttpWasm API should provide better interface into JSON from Traefik #1

Closed mobileskyfi closed 5 months ago

mobileskyfi commented 5 months ago

While the underlying Traefik "middleware" configuration is available to plugin biz logic via in hostConfig as Grain's Json* types, parsing out values should have some helpers.

Something like a Map be better. Perhaps collapsing the JSON in dot.strings to keep key,value of Map – just flatten names of any hierarchy, i.e. "actions.addResponseHeader.X-My-Header" from { "actions": { "addResponseHeader": { "X-My-Header": "some value" }}}

Right now it's way to much code to read one attributes from hostConfig using JsonObject()/match to add that as a header:

// special case, traefik's plugin validater checks "testData" if header is added
registerRequestHandler(req => match (match (hostConfig) {
  JsonObject([(root, JsonObject([(key, JsonString(value))]))]) => 
    Ok((key, value)),
    _ => Err("no config value found"),
  }) {
  Ok((key, value)) => {
    addHeaderValue(ResponseHeader, key, value)
    log(Debug, String.concat("special case, add response header: ", key))
    true
  },
  _ => true,
})

Thinking adding hostJson for the "raw" JsonObject(), and hostConfig be a Map of the JSON.

mobileskyfi commented 5 months ago

Still not ideal, but using a Map with dotted keys works better than the Json object for reading config.