cloudflare / pingora

A library for building fast, reliable and evolvable network services.
Apache License 2.0
20.25k stars 1.1k forks source link

How to define user configuration. #240

Closed wbtlb closed 3 weeks ago

wbtlb commented 1 month ago

How can I add new user config settings.

drcaramelsyrup commented 1 month ago

Sorry for the delay on responding. What do you mean by user config settings?

If you're asking about a configuration file like nginx's, Pingora's current config files are more limited than nginx's and are limited to environment setup, e.g. see the (as-of-today) YAML formatted ServerConf and the user guide.

wbtlb commented 1 month ago

I mean extand ServerConf with my own configuration parameters.

johnhurt commented 1 month ago

I think your best bet is to use serde's ability to flatten de/serialized structures. Our serverconf is just a struct with a few derives. If you wanted to add some extra configurations to the same file, you could so something like


#[derive(Serialize, Deserialize)]
struct CombinedConf {
    // All pingora configurations come in here
    #[serde(flatten)]
    base_conf: ServerConf,

    // Any of your configurations can go here
    #[serde(flatten)]
    custom_conf: CustomConf,
}

#[derive(Serialize, Deserialize)]
struct CustomConf {
    awesomeness_count: f64
    // ...
}
eaufavor commented 1 month ago

For certain format like yaml and json you can even add your custom field directly into the server_conf.yaml

---
version: 1
threads: 2
awesomeness_count: 3

The yaml above can be directly deserialized to CustomConf. All the fields that are not in your CustomConf are ignored. All the custom fields are ignored by the ServerConf as well.

github-actions[bot] commented 3 weeks ago

This question has been stale for a week. It will be closed in an additional day if not updated.

github-actions[bot] commented 3 weeks ago

This issue has been closed because it has been stalled with no activity.