[{"data":1,"prerenderedAt":1011},["ShallowReactive",2],{"$ffD-4qDhyEnfI5q3g0aETPnqFFIFUNbx_uobAFmze7p4":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":993,"editUrl":1004,"prev":1005,"next":1008},"overview\u002Fbootstrap","Bootstrap","Boot your application with","---\ntitle: Bootstrap\ndescription: Boot your application with #[bootstrap], Ferrite::create and the async runtime.\n---\n# Bootstrap\n\nBootstrapping is the moment Ferrite assembles the module graph, instantiates providers, and binds your router. It maps directly to what NestJS calls the \"factory\" phase of application startup.\n\n## `#[bootstrap]`\n\nAnnotate any `async fn main()`:\n\n```rust\nuse ferrite_framework::{bootstrap, Ferrite};\n\n#[bootstrap]\nasync fn main() {\n    let app = Ferrite::create::\u003CAppModule>().await;\n    app.listen(\"0.0.0.0:3000\").await.expect(\"failed to bind 0.0.0.0:3000\");\n}\n```\n\nThe macro rewrites this into a synchronous `main` that:\n\n1. Loads environment files (`load_env_file()`) from `.env*`,\n2. Creates a `tokio::runtime::Runtime`,\n3. Runs your async body on it with `block_on`.\n\nYou never write `#[tokio::main]` yourself.\n\n## `Ferrite::create`\n\n```rust\nimpl Ferrite {\n    pub async fn create\u003CM: Module>() -> App;\n    pub async fn create_with_seed\u003CM: Module, F: FnOnce(&Container)>(seed: F) -> App;\n}\n```\n\n`create::\u003CM>()` finds the module descriptor for `M`, collects every provider and controller registered in the inventory registries, assembles the dependency graph, and instantiates singletons. `create_with_seed` additionally let you seed the container **before** the graph compiles — this is what `ferrite-testing` uses to override providers:\n\n```rust\nFerrite::create_with_seed::\u003CAppModule, _>(|container| {\n    container.seed_singleton(Arc::new(FakeMailer));\n})\n```\n\nIf anything in the graph is missing or cyclic, you get a compiler error — not a runtime failure.\n\n## The `App`\n\n`create` returns the running application:\n\n```rust\npub struct App {\n    pub router: axum::Router,     \u002F\u002F the assembled router (nest extra routes in)\n    pub container: Container,     \u002F\u002F the compiled container\n}\n```\n\n### `listen`\n\n```rust\nimpl App {\n    pub async fn listen(self, addr: &str) -> Result\u003C(), std::io::Error>;\n}\n```\n\n```rust\nlet app = Ferrite::create::\u003CAppModule>().await;\napp.listen(&format!(\"0.0.0.0:{port}\")).await.expect(\"failed to bind port\");\n```\n\n### Access the router for extra nesting\n\nMount third-party Axum routers, or hand the router to a custom server:\n\n```rust\nlet app = Ferrite::create::\u003CAppModule>().await;\n\u002F\u002F for example, WebSockets mount onto the router:\nferrite_ws::mount_on(&mut app.router, app.container.clone(), \"\u002Fws\");\n\napp.listen(\"0.0.0.0:3000\").await?;\n```\n\n### Access the container post-compile\n\nResolve any registered provider after the graph is built:\n\n```rust\nlet app = Ferrite::create::\u003CAppModule>().await;\nlet grpc: Arc\u003CGrpcServer> = app.container.get::\u003CGrpcServer>();\ntokio::spawn(async move { grpc.start(\"0.0.0.0:50051\").await });\n```\n\n## Port configuration\n\nThe scaffold reads `PORT` from the environment (defaults to `3000`):\n\n```rust\nlet port = std::env::var(\"PORT\").unwrap_or_else(|_| \"3000\".into());\napp.listen(&format!(\"0.0.0.0:{port}\")).await\n```\n\n> `[server] port = 3000` in `ferrite.toml` is loaded by `ConfigService` into `SERVER__PORT` — see [Configuration](\u002Fdocs\u002Foverview\u002Fconfiguration) for the layered resolution rules.\n\n## Next steps\n\n- [Lifecycle events](\u002Fdocs\u002Ffundamentals\u002Flifecycle-events) — hooks that run around startup and shutdown.\n- [Configuration](\u002Fdocs\u002Foverview\u002Fconfiguration).",{"frontmatter":9,"meta":10,"nodes":41},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,35,38],{"id":17,"depth":13,"text":18},"bootstrap-1","#[bootstrap]",{"id":20,"depth":13,"text":21},"ferritecreate","Ferrite::create",{"id":23,"depth":13,"text":24,"children":25},"the-app","The App",[26,29,32],{"id":27,"depth":14,"text":28},"the-app-listen","listen",{"id":30,"depth":14,"text":31},"the-app-access-the-router-for-extra-nesting","Access the router for extra nesting",{"id":33,"depth":14,"text":34},"the-app-access-the-container-post-compile","Access the container post-compile",{"id":36,"depth":13,"text":37},"port-configuration","Port configuration",{"id":39,"depth":13,"text":40},"next-steps","Next steps",[42,46,50,56,63,193,200,227,234,238,345,367,431,434,440,445,506,511,578,644,646,649,745,747,750,859,861,872,952,977,979],[43,44,5],"h1",{"id":45},"bootstrap",[47,48,49],"p",{},"Bootstrapping is the moment Ferrite assembles the module graph, instantiates providers, and binds your router. It maps directly to what NestJS calls the \"factory\" phase of application startup.",[51,52,53],"h2",{"id":17},[54,55,18],"code",{},[47,57,58,59,62],{},"Annotate any ",[54,60,61],{},"async fn main()",":",[64,65,68],"pre",{"language":66,"class":67},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[54,69,71,97,98,97,100,97,104,97,118,97,154,97,188],{"class":70},"language-rust",[72,73,76,80,84,87,91,94],"span",{"class":74,"style":75},"line","display: inline",[72,77,79],{"style":78},"color:#D73A49;--shiki-dark:#F97583","use",[72,81,83],{"style":82},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[72,85,86],{"style":78},"::",[72,88,90],{"style":89},"color:#24292E;--shiki-dark:#E1E4E8","{bootstrap, ",[72,92,93],{"style":82},"Ferrite",[72,95,96],{"style":89},"};","\n",[72,99],{"class":74,"style":75},[72,101,102],{"class":74,"style":75},[72,103,18],{"style":89},[72,105,106,109,112,115],{"class":74,"style":75},[72,107,108],{"style":78},"async",[72,110,111],{"style":78}," fn",[72,113,114],{"style":82}," main",[72,116,117],{"style":89},"() {",[72,119,120,123,126,129,132,134,137,139,142,145,148,151],{"class":74,"style":75},[72,121,122],{"style":78},"    let",[72,124,125],{"style":89}," app ",[72,127,128],{"style":78},"=",[72,130,131],{"style":82}," Ferrite",[72,133,86],{"style":78},[72,135,136],{"style":82},"create",[72,138,86],{"style":78},[72,140,141],{"style":89},"\u003C",[72,143,144],{"style":82},"AppModule",[72,146,147],{"style":89},">()",[72,149,150],{"style":78},".await",[72,152,153],{"style":89},";",[72,155,156,159,162,164,167,171,174,177,180,182,185],{"class":74,"style":75},[72,157,158],{"style":89},"    app",[72,160,161],{"style":78},".",[72,163,28],{"style":82},[72,165,166],{"style":89},"(",[72,168,170],{"style":169},"color:#032F62;--shiki-dark:#9ECBFF","\"0.0.0.0:3000\"",[72,172,173],{"style":89},")",[72,175,176],{"style":78},".await.",[72,178,179],{"style":82},"expect",[72,181,166],{"style":89},[72,183,184],{"style":169},"\"failed to bind 0.0.0.0:3000\"",[72,186,187],{"style":89},");",[72,189,190],{"class":74,"style":75},[72,191,192],{"style":89},"}",[47,194,195,196,199],{},"The macro rewrites this into a synchronous ",[54,197,198],{},"main"," that:",[201,202,203,215,221],"ol",{},[204,205,206,207,210,211,214],"li",{},"Loads environment files (",[54,208,209],{},"load_env_file()",") from ",[54,212,213],{},".env*",",",[204,216,217,218,214],{},"Creates a ",[54,219,220],{},"tokio::runtime::Runtime",[204,222,223,224,161],{},"Runs your async body on it with ",[54,225,226],{},"block_on",[47,228,229,230,233],{},"You never write ",[54,231,232],{},"#[tokio::main]"," yourself.",[51,235,236],{"id":20},[54,237,21],{},[64,239,240],{"language":66,"class":67},[54,241,242,97,252,97,286,97,341],{"class":70},[72,243,244,247,249],{"class":74,"style":75},[72,245,246],{"style":78},"impl",[72,248,131],{"style":82},[72,250,251],{"style":89}," {",[72,253,254,257,260,262,265,267,270,272,275,278,281,284],{"class":74,"style":75},[72,255,256],{"style":78},"    pub",[72,258,259],{"style":78}," async",[72,261,111],{"style":78},[72,263,264],{"style":82}," create",[72,266,141],{"style":89},[72,268,269],{"style":82},"M",[72,271,62],{"style":78},[72,273,274],{"style":82}," Module",[72,276,277],{"style":89},">() ",[72,279,280],{"style":78},"->",[72,282,283],{"style":82}," App",[72,285,153],{"style":89},[72,287,288,290,292,294,297,299,301,303,305,308,311,313,316,318,321,324,327,329,332,335,337,339],{"class":74,"style":75},[72,289,256],{"style":78},[72,291,259],{"style":78},[72,293,111],{"style":78},[72,295,296],{"style":82}," create_with_seed",[72,298,141],{"style":89},[72,300,269],{"style":82},[72,302,62],{"style":78},[72,304,274],{"style":82},[72,306,307],{"style":89},", ",[72,309,310],{"style":82},"F",[72,312,62],{"style":78},[72,314,315],{"style":82}," FnOnce",[72,317,166],{"style":89},[72,319,320],{"style":78},"&",[72,322,323],{"style":82},"Container",[72,325,326],{"style":89},")>(seed",[72,328,62],{"style":78},[72,330,331],{"style":82}," F",[72,333,334],{"style":89},") ",[72,336,280],{"style":78},[72,338,283],{"style":82},[72,340,153],{"style":89},[72,342,343],{"class":74,"style":75},[72,344,192],{"style":89},[47,346,347,350,351,353,354,357,358,362,363,366],{},[54,348,349],{},"create::\u003CM>()"," finds the module descriptor for ",[54,352,269],{},", collects every provider and controller registered in the inventory registries, assembles the dependency graph, and instantiates singletons. ",[54,355,356],{},"create_with_seed"," additionally let you seed the container ",[359,360,361],"strong",{},"before"," the graph compiles — this is what ",[54,364,365],{},"ferrite-testing"," uses to override providers:",[64,368,369],{"language":66,"class":67},[54,370,371,97,398,97,426],{"class":70},[72,372,373,375,377,379,381,383,385,388,391,394,396],{"class":74,"style":75},[72,374,93],{"style":82},[72,376,86],{"style":78},[72,378,356],{"style":82},[72,380,86],{"style":78},[72,382,141],{"style":89},[72,384,144],{"style":82},[72,386,387],{"style":89},", _>(",[72,389,390],{"style":78},"|",[72,392,393],{"style":89},"container",[72,395,390],{"style":78},[72,397,251],{"style":89},[72,399,400,403,405,408,410,413,415,418,420,423],{"class":74,"style":75},[72,401,402],{"style":89},"    container",[72,404,161],{"style":78},[72,406,407],{"style":82},"seed_singleton",[72,409,166],{"style":89},[72,411,412],{"style":82},"Arc",[72,414,86],{"style":78},[72,416,417],{"style":82},"new",[72,419,166],{"style":89},[72,421,422],{"style":82},"FakeMailer",[72,424,425],{"style":89},"));",[72,427,428],{"class":74,"style":75},[72,429,430],{"style":89},"})",[47,432,433],{},"If anything in the graph is missing or cyclic, you get a compiler error — not a runtime failure.",[51,435,436,437],{"id":23},"The ",[54,438,439],{},"App",[47,441,442,444],{},[54,443,136],{}," returns the running application:",[64,446,447],{"language":66,"class":67},[54,448,449,97,461,97,485,97,502],{"class":70},[72,450,451,454,457,459],{"class":74,"style":75},[72,452,453],{"style":78},"pub",[72,455,456],{"style":78}," struct",[72,458,283],{"style":82},[72,460,251],{"style":89},[72,462,463,465,468,470,473,475,478,481],{"class":74,"style":75},[72,464,256],{"style":78},[72,466,467],{"style":89}," router",[72,469,62],{"style":78},[72,471,472],{"style":82}," axum",[72,474,86],{"style":78},[72,476,477],{"style":82},"Router",[72,479,480],{"style":89},",     ",[72,482,484],{"style":483},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F the assembled router (nest extra routes in)",[72,486,487,489,492,494,497,499],{"class":74,"style":75},[72,488,256],{"style":78},[72,490,491],{"style":89}," container",[72,493,62],{"style":78},[72,495,496],{"style":82}," Container",[72,498,480],{"style":89},[72,500,501],{"style":483},"\u002F\u002F the compiled container",[72,503,504],{"class":74,"style":75},[72,505,192],{"style":89},[507,508,509],"h3",{"id":27},[54,510,28],{},[64,512,513],{"language":66,"class":67},[54,514,515,97,523,97,574],{"class":70},[72,516,517,519,521],{"class":74,"style":75},[72,518,246],{"style":78},[72,520,283],{"style":82},[72,522,251],{"style":89},[72,524,525,527,529,531,534,536,540,543,545,548,551,553,555,558,561,563,566,568,571],{"class":74,"style":75},[72,526,256],{"style":78},[72,528,259],{"style":78},[72,530,111],{"style":78},[72,532,533],{"style":82}," listen",[72,535,166],{"style":89},[72,537,539],{"style":538},"color:#005CC5;--shiki-dark:#79B8FF","self",[72,541,542],{"style":89},", addr",[72,544,62],{"style":78},[72,546,547],{"style":78}," &",[72,549,550],{"style":82},"str",[72,552,334],{"style":89},[72,554,280],{"style":78},[72,556,557],{"style":82}," Result",[72,559,560],{"style":89},"\u003C(), std",[72,562,86],{"style":78},[72,564,565],{"style":89},"io",[72,567,86],{"style":78},[72,569,570],{"style":82},"Error",[72,572,573],{"style":89},">;",[72,575,576],{"class":74,"style":75},[72,577,192],{"style":89},[64,579,580],{"language":66,"class":67},[54,581,582,97,609],{"class":70},[72,583,584,587,589,591,593,595,597,599,601,603,605,607],{"class":74,"style":75},[72,585,586],{"style":78},"let",[72,588,125],{"style":89},[72,590,128],{"style":78},[72,592,131],{"style":82},[72,594,86],{"style":78},[72,596,136],{"style":82},[72,598,86],{"style":78},[72,600,141],{"style":89},[72,602,144],{"style":82},[72,604,147],{"style":89},[72,606,150],{"style":78},[72,608,153],{"style":89},[72,610,611,614,616,618,620,622,625,627,630,633,635,637,639,642],{"class":74,"style":75},[72,612,613],{"style":89},"app",[72,615,161],{"style":78},[72,617,28],{"style":82},[72,619,166],{"style":89},[72,621,320],{"style":78},[72,623,624],{"style":82},"format!",[72,626,166],{"style":89},[72,628,629],{"style":169},"\"0.0.0.0:{port}\"",[72,631,632],{"style":89},"))",[72,634,176],{"style":78},[72,636,179],{"style":82},[72,638,166],{"style":89},[72,640,641],{"style":169},"\"failed to bind port\"",[72,643,187],{"style":89},[507,645,31],{"id":30},[47,647,648],{},"Mount third-party Axum routers, or hand the router to a custom server:",[64,650,651],{"language":66,"class":67},[54,652,653,97,679,97,684,97,724,97,726],{"class":70},[72,654,655,657,659,661,663,665,667,669,671,673,675,677],{"class":74,"style":75},[72,656,586],{"style":78},[72,658,125],{"style":89},[72,660,128],{"style":78},[72,662,131],{"style":82},[72,664,86],{"style":78},[72,666,136],{"style":82},[72,668,86],{"style":78},[72,670,141],{"style":89},[72,672,144],{"style":82},[72,674,147],{"style":89},[72,676,150],{"style":78},[72,678,153],{"style":89},[72,680,681],{"class":74,"style":75},[72,682,683],{"style":483},"\u002F\u002F for example, WebSockets mount onto the router:",[72,685,686,689,691,694,696,699,702,704,707,709,711,713,716,719,722],{"class":74,"style":75},[72,687,688],{"style":82},"ferrite_ws",[72,690,86],{"style":78},[72,692,693],{"style":82},"mount_on",[72,695,166],{"style":89},[72,697,698],{"style":78},"&mut",[72,700,701],{"style":89}," app",[72,703,161],{"style":78},[72,705,706],{"style":89},"router, app",[72,708,161],{"style":78},[72,710,393],{"style":89},[72,712,161],{"style":78},[72,714,715],{"style":82},"clone",[72,717,718],{"style":89},"(), ",[72,720,721],{"style":169},"\"\u002Fws\"",[72,723,187],{"style":89},[72,725],{"class":74,"style":75},[72,727,728,730,732,734,736,738,740,743],{"class":74,"style":75},[72,729,613],{"style":89},[72,731,161],{"style":78},[72,733,28],{"style":82},[72,735,166],{"style":89},[72,737,170],{"style":169},[72,739,173],{"style":89},[72,741,742],{"style":78},".await?",[72,744,153],{"style":89},[507,746,34],{"id":33},[47,748,749],{},"Resolve any registered provider after the graph is built:",[64,751,752],{"language":66,"class":67},[54,753,754,97,780,97,822],{"class":70},[72,755,756,758,760,762,764,766,768,770,772,774,776,778],{"class":74,"style":75},[72,757,586],{"style":78},[72,759,125],{"style":89},[72,761,128],{"style":78},[72,763,131],{"style":82},[72,765,86],{"style":78},[72,767,136],{"style":82},[72,769,86],{"style":78},[72,771,141],{"style":89},[72,773,144],{"style":82},[72,775,147],{"style":89},[72,777,150],{"style":78},[72,779,153],{"style":89},[72,781,782,784,787,789,792,794,797,800,802,804,806,808,810,813,815,817,819],{"class":74,"style":75},[72,783,586],{"style":78},[72,785,786],{"style":89}," grpc",[72,788,62],{"style":78},[72,790,791],{"style":82}," Arc",[72,793,141],{"style":89},[72,795,796],{"style":82},"GrpcServer",[72,798,799],{"style":89},"> ",[72,801,128],{"style":78},[72,803,701],{"style":89},[72,805,161],{"style":78},[72,807,393],{"style":89},[72,809,161],{"style":78},[72,811,812],{"style":82},"get",[72,814,86],{"style":78},[72,816,141],{"style":89},[72,818,796],{"style":82},[72,820,821],{"style":89},">();",[72,823,824,827,829,832,834,836,839,842,844,847,849,852,854,856],{"class":74,"style":75},[72,825,826],{"style":82},"tokio",[72,828,86],{"style":78},[72,830,831],{"style":82},"spawn",[72,833,166],{"style":89},[72,835,108],{"style":78},[72,837,838],{"style":78}," move",[72,840,841],{"style":89}," { grpc",[72,843,161],{"style":78},[72,845,846],{"style":82},"start",[72,848,166],{"style":89},[72,850,851],{"style":169},"\"0.0.0.0:50051\"",[72,853,173],{"style":89},[72,855,150],{"style":78},[72,857,858],{"style":89}," });",[51,860,37],{"id":36},[47,862,863,864,867,868,871],{},"The scaffold reads ",[54,865,866],{},"PORT"," from the environment (defaults to ",[54,869,870],{},"3000","):",[64,873,874],{"language":66,"class":67},[54,875,876,97,930],{"class":70},[72,877,878,880,883,885,888,890,893,895,898,900,903,905,907,910,912,914,917,919,922,924,927],{"class":74,"style":75},[72,879,586],{"style":78},[72,881,882],{"style":89}," port ",[72,884,128],{"style":78},[72,886,887],{"style":82}," std",[72,889,86],{"style":78},[72,891,892],{"style":82},"env",[72,894,86],{"style":78},[72,896,897],{"style":82},"var",[72,899,166],{"style":89},[72,901,902],{"style":169},"\"PORT\"",[72,904,173],{"style":89},[72,906,161],{"style":78},[72,908,909],{"style":82},"unwrap_or_else",[72,911,166],{"style":89},[72,913,390],{"style":78},[72,915,916],{"style":89},"_",[72,918,390],{"style":78},[72,920,921],{"style":169}," \"3000\"",[72,923,161],{"style":78},[72,925,926],{"style":82},"into",[72,928,929],{"style":89},"());",[72,931,932,934,936,938,940,942,944,946,948,950],{"class":74,"style":75},[72,933,613],{"style":89},[72,935,161],{"style":78},[72,937,28],{"style":82},[72,939,166],{"style":89},[72,941,320],{"style":78},[72,943,624],{"style":82},[72,945,166],{"style":89},[72,947,629],{"style":169},[72,949,632],{"style":89},[72,951,150],{"style":78},[953,954,955,958,959,962,963,966,967,970,971,976],"blockquote",{},[54,956,957],{},"[server] port = 3000"," in ",[54,960,961],{},"ferrite.toml"," is loaded by ",[54,964,965],{},"ConfigService"," into ",[54,968,969],{},"SERVER__PORT"," — see ",[972,973,975],"a",{"href":974},"\u002Fdocs\u002Foverview\u002Fconfiguration","Configuration"," for the layered resolution rules.",[51,978,40],{"id":39},[980,981,982,989],"ul",{},[204,983,984,988],{},[972,985,987],{"href":986},"\u002Fdocs\u002Ffundamentals\u002Flifecycle-events","Lifecycle events"," — hooks that run around startup and shutdown.",[204,990,991,161],{},[972,992,975],{"href":974},{"title":12,"searchDepth":13,"depth":14,"links":994},[995,996,997,1002,1003],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24,"children":998},[999,1000,1001],{"id":27,"depth":14,"text":28},{"id":30,"depth":14,"text":31},{"id":33,"depth":14,"text":34},{"id":36,"depth":13,"text":37},{"id":39,"depth":13,"text":40},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fbootstrap.md",{"title":1006,"slug":1007},"Modules","overview\u002Fmodules",{"title":1009,"slug":1010},"Middleware","overview\u002Fmiddleware",1789565440488]