{"id":26685438,"url":"https://github.com/goapt/envconf","last_synced_at":"2025-03-26T10:16:04.280Z","repository":{"id":57519313,"uuid":"248117998","full_name":"goapt/envconf","owner":"goapt","description":"golang config from .env file","archived":false,"fork":false,"pushed_at":"2023-02-25T09:02:45.000Z","size":97,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-07-27T14:42:17.319Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goapt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-18T02:15:56.000Z","updated_at":"2021-06-18T07:14:15.000Z","dependencies_parsed_at":"2022-09-06T03:42:35.881Z","dependency_job_id":null,"html_url":"https://github.com/goapt/envconf","commit_stats":null,"previous_names":[],"tags_count":4,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Fenvconf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Fenvconf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Fenvconf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goapt%2Fenvconf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goapt","download_url":"https://codeload.github.com/goapt/envconf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245632400,"owners_count":20647194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-26T10:16:03.529Z","updated_at":"2025-03-26T10:16:04.261Z","avatar_url":"https://github.com/goapt.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EnvConf 基于viper + env的golang配置库\n\u003ca href=\"https://github.com/goapt/envconf/actions\"\u003e\u003cimg src=\"https://github.com/goapt/envconf/workflows/build/badge.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\n为了解决配置集中化方案，基于viper格式和env配置自动替换的解决方案\n\n## 理念\n在应用程序配置文件中，我们只需要使用toml或者yml,ini等viper支持的配置文件写出一个配置文件骨架，里面不包含任何的敏感数据（如用户名和密码），然后在.env.local文件中写入敏感信息，程序启动的时候会自动将数据覆盖替换，如下\n\n1、创建 app.toml 骨架配置文件\n```toml\nenv = \"local\"\n# debug  off close, on open\ndebug = \"on\"\n\ntime_after = 0.5\n\n[databases]\n\n  # You can indent as you please. Tabs or spaces. TOML don't care.\n  [databases.alpha]\n  host = \"\"\n  username = \"\"\n  password = \"\"\n  maxidle = 10\n\n  [databases.beta]\n  host = \"\"\n  username = \"\"\n  password = \"\"\n  maxidle = 10\n\n[log]\n    log_mode = \"std\"\n    log_level = \"debug\"\n    log_max_files = 15\n\n```\n2、创建敏感信息替换文件 `.env.local`\n```\ndebug=off\ntime_after=0.125\n# server config\ndatabases.alpha.host=127.0.0.1\ndatabases.alpha.username=root\ndatabases.alpha.password=123456\ndatabases.alpha.maxidle=100\ndatabases.beta.host=127.0.0.2\ndatabases.beta.username=root\ndatabases.beta.password=123456\n# use variables\ndatabases.beta.maxidle=${databases.alpha.maxidle}\n\n# log config\nlog.log_max_files=10\n```\n\nenv文件遵循 [JSON-Path](https://github.com/pelletier/go-toml/tree/master/query) 规范\n\n此处我们注意到两点\n\n1、env文件支持注释，使用 `#` 开头的则为注释行\n\n2、支持变量应用，比如 `databases.beta.maxidle` 就是对 `databases.alpha.maxidle` 值的引用，变量应用使用 `${KEY}` 格式，当ENV变量不在当前配置项中，则会从系统环境变量中获取\n\n\u003e 因此我们可以仅在本地创建.env.local，而在系统发布的时候由发布系统将ACM配置中心的env线上配置数据写入到项目目录的.env.prod文件，从而实现线上的安全隔离\n\n3、载入配置文件，以及覆盖数据\n```go\nimport \"github.com/goapt/envconf\"\n\ntype Database struct {\n\tHost     string\n\tUsername string\n\tPassword string\n\tMaxidle  int\n}\n\ntype LogConf struct {\n\tLogModel    string `toml:\"log_mode\"`\n\tLogLevel    string `toml:\"log_level\"`\n\tLogMaxFiles int    `toml:\"log_max_files\"`\n}\n\ntype App struct {\n\tEnv       string\n\tDebug     string\n\tTimeAfter float32 `toml:\"time_after\"`\n\tDatabases map[string]Database\n\tLog       LogConf\n}\n\nconf, err := envconf.New(\"./testdata/app.toml\")\n\nif err != nil {\n    return err\n}\n\nerr = conf.Env(\"./testdata/.env.local\")\n\nif err != nil {\n    return err\n}\n\napp := \u0026App{}\n\nerr = conf.Unmarshal(app)\nif err != nil {\n    return err\n}\n```\n\n4、最后得到数据如下\n```\nenv = \"local\"\n# debug  off close, on open\ndebug = \"off\"\n\ntime_after = 0.125\n\n[databases]\n\n  # You can indent as you please. Tabs or spaces. TOML don't care.\n  [databases.alpha]\n  host = \"127.0.0.1\"\n  username = \"root\"\n  password = \"123456\"\n  maxidle = 100\n\n  [databases.beta]\n  host = \"127.0.0.2\"\n  username = \"root\"\n  password = \"123456\"\n  maxidle = 100\n\n[log]\n    log_mode = \"std\"\n    log_level = \"debug\"\n    log_max_files = 10\n```\n\n\u003e Enjoy\n\n## Thanks\n\nhttps://github.com/joho/godotenv\n\nhttps://github.com/spf13/viper\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoapt%2Fenvconf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoapt%2Fenvconf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoapt%2Fenvconf/lists"}