discuitnet / discuit

A free and open-source community discussion platform.
https://discuit.net
GNU Affero General Public License v3.0
411 stars 52 forks source link

[Feature Request] Docker: Support Configuration via Environment Variables #45

Closed Codycody31 closed 3 months ago

Codycody31 commented 7 months ago

Summary

Currently, discuit relies on a YAML file for configuration. While this works well in many scenarios, Docker and other containerized environments often prefer using environment variables for configuration due to their simplicity, security, and ease of use with container orchestration tools like Kubernetes and Docker Compose. This feature request proposes an enhancement to allow discuit to be configured using environment variables, in addition to the existing YAML file-based approach.

Motivation

In containerized deployments, managing configuration files can be cumbersome and less secure. Environment variables, on the other hand, can be easily injected into containers at runtime, making them ideal for such environments. Supporting environment variables would make discuit more flexible and easier to deploy in a variety of environments, following the twelve-factor app methodology.

Proposed Solution

Introduce the capability to read configuration settings from environment variables, with a clear precedence order where environment variables override the settings defined in the YAML file. This allows users to provide a base configuration through the YAML file while overriding specific settings with environment variables as needed.

Considerations

Benefits

Issues

Yup I used GPT, There is no way I could make my idea sound this good/well formatted

previnder commented 6 months ago

Sounds good. What we'd need here is a function to go through the env variables and populate config.Config struct, overriding the fields that correspond to env variables found.

Codycody31 commented 6 months ago

That's kind of what I was able to do here. It's still a WIP but it does work. The next bit I was looking at would be adding something like -injectEnv or something similar which could be called via the UI prod build to have discuit return a YAML file with the injected env vars. However, I am unsure if that would work as the webpack.common.js script would need to run it (Mainly, not sure if possible to use os or something so I need to check).

previnder commented 6 months ago

I totally forgot that the config file is also used by node. That makes it a bit tricky, for sure.

Codycody31 commented 6 months ago

Yeah, I started working on it. However, it doesn't seem to be too hard to do. I'll need to test it out in docker to ensure it works before I go through a PR together. Also, what do you think a good flag name for this would be? I threw it together as flag.BoolVar(&f.injectConfig, "inject-config", false, "Inject environment variables to yaml")`, though I am not sure how you would prefer it to be.

// RecreateYaml creates a new yaml file with default values and returns a Config.
func RecreateYaml(c Config) (string, error) {
    yamlData, err := yaml.Marshal(c)
    if err != nil {
        return "", err
    }

    // Return the yaml file content as a string
    return string(yamlData), nil
}