[{"data":1,"prerenderedAt":812},["ShallowReactive",2],{"$fjUd_Sk_jjqRC0uwLzBnounKPhp9seBIQuXnYN26sCR8":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":794,"editUrl":806,"prev":807,"next":810},"overview\u002Fconfiguration","Configuration","Manage environment configuration with ConfigService, .env files and ferrite.toml.","---\ntitle: Configuration\ndescription: Manage environment configuration with ConfigService, .env files and ferrite.toml.\n---\n\n# Configuration\n\nFerrite reads configuration from four layers, with later layers overriding earlier ones. `ConfigService` is a global singleton — you can inject it anywhere, from services to auth and ORM adapters.\n\n## Sources & precedence\n\n| Priority | Source |\n| --- | --- |\n| 5 (highest) | Real process environment |\n| 4 | `.env.\u003CFERRITE_ENV>` (e.g. `.env.production`) |\n| 3 | `.env` |\n| 2 | `ferrite.toml` (flattened) |\n| 1 (lowest) | Hardcoded defaults (`APP_ENV=development`, `PORT=3000`) |\n\n## Using `ConfigService`\n\n```rust\nuse ferrite_framework::{injectable, inject, ConfigService};\n\n#[injectable]\npub struct AppService {\n    config: ConfigService,\n}\n\nimpl AppService {\n    #[inject]\n    pub fn new(config: ConfigService) -> Self {\n        Self { config }\n    }\n\n    pub fn env(&self) -> String {\n        self.config.get_or(\"APP_ENV\", \"development\")\n    }\n\n    pub fn port(&self) -> u16 {\n        self.config.get_or_parse(\"PORT\", 3000u16)\n    }\n}\n```\n\n### API\n\n| Method | Description |\n| --- | --- |\n| `get(key) -> Option\u003CString>` | Raw lookup |\n| `get_or(key, default) -> String` | Lookup with a fallback |\n| `get_or_parse::\u003CT>(key, default) -> T` | Parse with `FromStr`, fall back on failure |\n| `contains(key) -> bool` | Presence check |\n| `all() -> HashMap\u003CString, String>` | Entire merged map |\n\n### Loading\n\n`ConfigService::load()` merges everything. `load_from(dir)` reads from a specific directory, and `merge_layers(dir, real_env)` gives you full control. The `#[bootstrap]` macro calls `load_env_file()` automatically for `.env*`, without overriding existing process variables.\n\n## `ferrite.toml`\n\nTOML sections are flattened into `SECTION__KEY` variables:\n\n```toml\n[app]\nname = \"my-api\"\n\n[server]\ntransport = \"http\"\nhost = \"0.0.0.0\"\nport = 3000\n```\n\nbecomes:\n\n```text\nAPP__NAME=my-api\nSERVER__TRANSPORT=http\nSERVER__HOST=0.0.0.0\nSERVER__PORT=3000\n```\n\nScaffold one quickly:\n\n```bash\nfr generate config\n```\n\n## `.env` files\n\n`.env.example` is scaffolded for you:\n\n```bash\nfr generate env\n```\n\n```dotenv\nAPP_ENV=development\nPORT=3000\nDATABASE_URL=postgres:\u002F\u002Fuser:pass@localhost:5432\u002Fapp\nJWT_SECRET=change-me\n```\n\nPer-environment files load automatically based on `FERRITE_ENV` \u002F `APP_ENV`:\n\n```bash\nfr up --env production   # additionally loads .env.production\n```\n\n## What uses configuration\n\n- `ferrite-auth-jwt` — `JWT_SECRET`, `JWT_EXPIRES_IN`\n- `ferrite-auth-oauth` — `\u003CPROVIDER>_OAUTH_CLIENT_ID`, `_CLIENT_SECRET`, `_REDIRECT_URI`\n- `ferrite-orm-sqlx` \u002F diesel \u002F sea — `DATABASE_URL`\n- `ferrite-swagger` — `SWAGGER_TITLE`, `SWAGGER_VERSION`, `SWAGGER_DESCRIPTION`, `SWAGGER_SERVER_URL`\n- `ferrite-grpc` — `GRPC_HEALTH_TIMEOUT_MS`\n- `ferrite-cache-redis` — Redis connection settings\n\n## Verification\n\n```bash\nfr doctor\n```\n\nchecks that `.env`, `ferrite.toml`, and `DATABASE_URL` are present and reports on the toolchain.\n\n## Next steps\n\n- [Validation](\u002Fdocs\u002Foverview\u002Fvalidation).\n- [Environment files](\u002Fdocs\u002Foverview\u002Fcli-usage#generate).",{"frontmatter":9,"meta":10,"nodes":44},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,29,32,35,38,41],{"id":17,"depth":13,"text":18},"sources-precedence","Sources & precedence",{"id":20,"depth":13,"text":21,"children":22},"using-configservice","Using ConfigService",[23,26],{"id":24,"depth":14,"text":25},"using-configservice-api","API",{"id":27,"depth":14,"text":28},"using-configservice-loading","Loading",{"id":30,"depth":13,"text":31},"ferritetoml","ferrite.toml",{"id":33,"depth":13,"text":34},"env-files",".env files",{"id":36,"depth":13,"text":37},"what-uses-configuration","What uses configuration",{"id":39,"depth":13,"text":40},"verification","Verification",{"id":42,"depth":13,"text":43},"next-steps","Next steps",[45,49,58,61,139,144,401,404,472,474,500,504,511,570,573,599,602,619,624,630,642,650,660,681,683,753,755,766,777,779],[46,47,5],"h1",{"id":48},"configuration",[50,51,52,53,57],"p",{},"Ferrite reads configuration from four layers, with later layers overriding earlier ones. ",[54,55,56],"code",{},"ConfigService"," is a global singleton — you can inject it anywhere, from services to auth and ORM adapters.",[59,60,18],"h2",{"id":17},[62,63,64,77],"table",{},[65,66,67],"thead",{},[68,69,70,74],"tr",{},[71,72,73],"th",{},"Priority",[71,75,76],{},"Source",[78,79,80,89,104,114,124],"tbody",{},[68,81,82,86],{},[83,84,85],"td",{},"5 (highest)",[83,87,88],{},"Real process environment",[68,90,91,94],{},[83,92,93],{},"4",[83,95,96,99,100,103],{},[54,97,98],{},".env.\u003CFERRITE_ENV>"," (e.g. ",[54,101,102],{},".env.production",")",[68,105,106,109],{},[83,107,108],{},"3",[83,110,111],{},[54,112,113],{},".env",[68,115,116,119],{},[83,117,118],{},"2",[83,120,121,123],{},[54,122,31],{}," (flattened)",[68,125,126,129],{},[83,127,128],{},"1 (lowest)",[83,130,131,132,135,136,103],{},"Hardcoded defaults (",[54,133,134],{},"APP_ENV=development",", ",[54,137,138],{},"PORT=3000",[59,140,141,142],{"id":20},"Using ",[54,143,56],{},[145,146,149],"pre",{"language":147,"class":148},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[54,150,152,177,178,177,180,177,185,177,199,177,213,177,218,177,220,177,229,177,234,177,264,177,272,177,277,177,279,177,306,177,335,177,339,177,341,177,365,177,393,177,397],{"class":151},"language-rust",[153,154,157,161,165,168,172,174],"span",{"class":155,"style":156},"line","display: inline",[153,158,160],{"style":159},"color:#D73A49;--shiki-dark:#F97583","use",[153,162,164],{"style":163},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[153,166,167],{"style":159},"::",[153,169,171],{"style":170},"color:#24292E;--shiki-dark:#E1E4E8","{injectable, inject, ",[153,173,56],{"style":163},[153,175,176],{"style":170},"};","\n",[153,179],{"class":155,"style":156},[153,181,182],{"class":155,"style":156},[153,183,184],{"style":170},"#[injectable]",[153,186,187,190,193,196],{"class":155,"style":156},[153,188,189],{"style":159},"pub",[153,191,192],{"style":159}," struct",[153,194,195],{"style":163}," AppService",[153,197,198],{"style":170}," {",[153,200,201,204,207,210],{"class":155,"style":156},[153,202,203],{"style":170},"    config",[153,205,206],{"style":159},":",[153,208,209],{"style":163}," ConfigService",[153,211,212],{"style":170},",",[153,214,215],{"class":155,"style":156},[153,216,217],{"style":170},"}",[153,219],{"class":155,"style":156},[153,221,222,225,227],{"class":155,"style":156},[153,223,224],{"style":159},"impl",[153,226,195],{"style":163},[153,228,198],{"style":170},[153,230,231],{"class":155,"style":156},[153,232,233],{"style":170},"    #[inject]",[153,235,236,239,242,245,248,250,252,255,258,262],{"class":155,"style":156},[153,237,238],{"style":159},"    pub",[153,240,241],{"style":159}," fn",[153,243,244],{"style":163}," new",[153,246,247],{"style":170},"(config",[153,249,206],{"style":159},[153,251,209],{"style":163},[153,253,254],{"style":170},") ",[153,256,257],{"style":159},"->",[153,259,261],{"style":260},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[153,263,198],{"style":170},[153,265,266,269],{"class":155,"style":156},[153,267,268],{"style":260},"        Self",[153,270,271],{"style":170}," { config }",[153,273,274],{"class":155,"style":156},[153,275,276],{"style":170},"    }",[153,278],{"class":155,"style":156},[153,280,281,283,285,288,291,294,297,299,301,304],{"class":155,"style":156},[153,282,238],{"style":159},[153,284,241],{"style":159},[153,286,287],{"style":163}," env",[153,289,290],{"style":170},"(",[153,292,293],{"style":159},"&",[153,295,296],{"style":260},"self",[153,298,254],{"style":170},[153,300,257],{"style":159},[153,302,303],{"style":163}," String",[153,305,198],{"style":170},[153,307,308,311,314,317,319,322,324,328,330,333],{"class":155,"style":156},[153,309,310],{"style":260},"        self",[153,312,313],{"style":159},".",[153,315,316],{"style":170},"config",[153,318,313],{"style":159},[153,320,321],{"style":163},"get_or",[153,323,290],{"style":170},[153,325,327],{"style":326},"color:#032F62;--shiki-dark:#9ECBFF","\"APP_ENV\"",[153,329,135],{"style":170},[153,331,332],{"style":326},"\"development\"",[153,334,103],{"style":170},[153,336,337],{"class":155,"style":156},[153,338,276],{"style":170},[153,340],{"class":155,"style":156},[153,342,343,345,347,350,352,354,356,358,360,363],{"class":155,"style":156},[153,344,238],{"style":159},[153,346,241],{"style":159},[153,348,349],{"style":163}," port",[153,351,290],{"style":170},[153,353,293],{"style":159},[153,355,296],{"style":260},[153,357,254],{"style":170},[153,359,257],{"style":159},[153,361,362],{"style":163}," u16",[153,364,198],{"style":170},[153,366,367,369,371,373,375,378,380,383,385,388,391],{"class":155,"style":156},[153,368,310],{"style":260},[153,370,313],{"style":159},[153,372,316],{"style":170},[153,374,313],{"style":159},[153,376,377],{"style":163},"get_or_parse",[153,379,290],{"style":170},[153,381,382],{"style":326},"\"PORT\"",[153,384,135],{"style":170},[153,386,387],{"style":260},"3000",[153,389,390],{"style":163},"u16",[153,392,103],{"style":170},[153,394,395],{"class":155,"style":156},[153,396,276],{"style":170},[153,398,399],{"class":155,"style":156},[153,400,217],{"style":170},[402,403,25],"h3",{"id":24},[62,405,406,416],{},[65,407,408],{},[68,409,410,413],{},[71,411,412],{},"Method",[71,414,415],{},"Description",[78,417,418,428,438,452,462],{},[68,419,420,425],{},[83,421,422],{},[54,423,424],{},"get(key) -> Option\u003CString>",[83,426,427],{},"Raw lookup",[68,429,430,435],{},[83,431,432],{},[54,433,434],{},"get_or(key, default) -> String",[83,436,437],{},"Lookup with a fallback",[68,439,440,445],{},[83,441,442],{},[54,443,444],{},"get_or_parse::\u003CT>(key, default) -> T",[83,446,447,448,451],{},"Parse with ",[54,449,450],{},"FromStr",", fall back on failure",[68,453,454,459],{},[83,455,456],{},[54,457,458],{},"contains(key) -> bool",[83,460,461],{},"Presence check",[68,463,464,469],{},[83,465,466],{},[54,467,468],{},"all() -> HashMap\u003CString, String>",[83,470,471],{},"Entire merged map",[402,473,28],{"id":27},[50,475,476,479,480,483,484,487,488,491,492,495,496,499],{},[54,477,478],{},"ConfigService::load()"," merges everything. ",[54,481,482],{},"load_from(dir)"," reads from a specific directory, and ",[54,485,486],{},"merge_layers(dir, real_env)"," gives you full control. The ",[54,489,490],{},"#[bootstrap]"," macro calls ",[54,493,494],{},"load_env_file()"," automatically for ",[54,497,498],{},".env*",", without overriding existing process variables.",[59,501,502],{"id":30},[54,503,31],{},[50,505,506,507,510],{},"TOML sections are flattened into ",[54,508,509],{},"SECTION__KEY"," variables:",[145,512,514],{"language":513,"class":148},"toml",[54,515,517,177,528,177,536,177,538,177,547,177,555,177,563],{"class":516},"language-toml",[153,518,519,522,525],{"class":155,"style":156},[153,520,521],{"style":170},"[",[153,523,524],{"style":163},"app",[153,526,527],{"style":170},"]",[153,529,530,533],{"class":155,"style":156},[153,531,532],{"style":170},"name = ",[153,534,535],{"style":326},"\"my-api\"",[153,537],{"class":155,"style":156},[153,539,540,542,545],{"class":155,"style":156},[153,541,521],{"style":170},[153,543,544],{"style":163},"server",[153,546,527],{"style":170},[153,548,549,552],{"class":155,"style":156},[153,550,551],{"style":170},"transport = ",[153,553,554],{"style":326},"\"http\"",[153,556,557,560],{"class":155,"style":156},[153,558,559],{"style":170},"host = ",[153,561,562],{"style":326},"\"0.0.0.0\"",[153,564,565,568],{"class":155,"style":156},[153,566,567],{"style":170},"port = ",[153,569,387],{"style":260},[50,571,572],{},"becomes:",[145,574,576],{"language":575,"class":148},"text",[54,577,579,177,584,177,589,177,594],{"class":578},"language-text",[153,580,581],{"class":155,"style":156},[153,582,583],{},"APP__NAME=my-api",[153,585,586],{"class":155,"style":156},[153,587,588],{},"SERVER__TRANSPORT=http",[153,590,591],{"class":155,"style":156},[153,592,593],{},"SERVER__HOST=0.0.0.0",[153,595,596],{"class":155,"style":156},[153,597,598],{},"SERVER__PORT=3000",[50,600,601],{},"Scaffold one quickly:",[145,603,605],{"language":604,"class":148},"bash",[54,606,608],{"class":607},"language-bash",[153,609,610,613,616],{"class":155,"style":156},[153,611,612],{"style":163},"fr",[153,614,615],{"style":326}," generate",[153,617,618],{"style":326}," config",[59,620,621,623],{"id":33},[54,622,113],{}," files",[50,625,626,629],{},[54,627,628],{},".env.example"," is scaffolded for you:",[145,631,632],{"language":604,"class":148},[54,633,634],{"class":607},[153,635,636,638,640],{"class":155,"style":156},[153,637,612],{"style":163},[153,639,615],{"style":326},[153,641,287],{"style":326},[145,643,646],{"language":644,"class":645},"dotenv","shiki dark:github-dark",[54,647,649],{"class":648},"language-dotenv","APP_ENV=development\nPORT=3000\nDATABASE_URL=postgres:\u002F\u002Fuser:pass@localhost:5432\u002Fapp\nJWT_SECRET=change-me",[50,651,652,653,656,657,206],{},"Per-environment files load automatically based on ",[54,654,655],{},"FERRITE_ENV"," \u002F ",[54,658,659],{},"APP_ENV",[145,661,662],{"language":604,"class":148},[54,663,664],{"class":607},[153,665,666,668,671,674,677],{"class":155,"style":156},[153,667,612],{"style":163},[153,669,670],{"style":326}," up",[153,672,673],{"style":260}," --env",[153,675,676],{"style":326}," production",[153,678,680],{"style":679},"color:#6A737D;--shiki-dark:#6A737D","   # additionally loads .env.production",[59,682,37],{"id":36},[684,685,686,699,713,722,739,747],"ul",{},[687,688,689,692,693,135,696],"li",{},[54,690,691],{},"ferrite-auth-jwt"," — ",[54,694,695],{},"JWT_SECRET",[54,697,698],{},"JWT_EXPIRES_IN",[687,700,701,692,704,135,707,135,710],{},[54,702,703],{},"ferrite-auth-oauth",[54,705,706],{},"\u003CPROVIDER>_OAUTH_CLIENT_ID",[54,708,709],{},"_CLIENT_SECRET",[54,711,712],{},"_REDIRECT_URI",[687,714,715,718,719],{},[54,716,717],{},"ferrite-orm-sqlx"," \u002F diesel \u002F sea — ",[54,720,721],{},"DATABASE_URL",[687,723,724,692,727,135,730,135,733,135,736],{},[54,725,726],{},"ferrite-swagger",[54,728,729],{},"SWAGGER_TITLE",[54,731,732],{},"SWAGGER_VERSION",[54,734,735],{},"SWAGGER_DESCRIPTION",[54,737,738],{},"SWAGGER_SERVER_URL",[687,740,741,692,744],{},[54,742,743],{},"ferrite-grpc",[54,745,746],{},"GRPC_HEALTH_TIMEOUT_MS",[687,748,749,752],{},[54,750,751],{},"ferrite-cache-redis"," — Redis connection settings",[59,754,40],{"id":39},[145,756,757],{"language":604,"class":148},[54,758,759],{"class":607},[153,760,761,763],{"class":155,"style":156},[153,762,612],{"style":163},[153,764,765],{"style":326}," doctor",[50,767,768,769,135,771,773,774,776],{},"checks that ",[54,770,113],{},[54,772,31],{},", and ",[54,775,721],{}," are present and reports on the toolchain.",[59,778,43],{"id":42},[684,780,781,788],{},[687,782,783,313],{},[784,785,787],"a",{"href":786},"\u002Fdocs\u002Foverview\u002Fvalidation","Validation",[687,789,790,313],{},[784,791,793],{"href":792},"\u002Fdocs\u002Foverview\u002Fcli-usage#generate","Environment files",{"title":12,"searchDepth":13,"depth":14,"links":795},[796,797,801,802,803,804,805],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21,"children":798},[799,800],{"id":24,"depth":14,"text":25},{"id":27,"depth":14,"text":28},{"id":30,"depth":13,"text":31},{"id":33,"depth":13,"text":34},{"id":36,"depth":13,"text":37},{"id":39,"depth":13,"text":40},{"id":42,"depth":13,"text":43},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fconfiguration.md",{"title":808,"slug":809},"Exception filters","overview\u002Fexception-filters",{"title":787,"slug":811},"overview\u002Fvalidation",1789565440161]