@@ -116,27 +116,34 @@ pub const App = struct {
116116
117117 const CONFIG_FILE = "config.json" ;
118118
119- pub fn initConfig ( target : * std . json . Parsed ( Config ), home : * Home , ct : * tk .Container ) ! void {
120- target .* = try ct . injector . call ( readConfig , .{});
121- target . value . db . dir = target . value . db . dir orelse home . path ;
119+ pub fn configure ( bundle : * tk.Bundle ) void {
120+ // We can't use `routes: [] = ...` field default because we need to reference api again for swagger
121+ bundle . addInstance ([] const tk . Route , . value ( routes )) ;
122122
123- try ct .register (& target .value );
124- inline for (std .meta .fields (Config )) | f | try ct .register (&@field (target .value , f .name ));
123+ // TODO: Maybe the default for fields should be .anyhow?
124+ bundle .addOverride (std .json .Parsed (Config ), .factory (readConfig ));
125+
126+ // Make all config.xxx fields available for injection
127+ bundle .addFieldRef (std .json .Parsed (Config ), "value" );
128+ inline for (std .meta .fields (Config )) | f | bundle .addFieldRef (Config , f .name );
129+
130+ bundle .addInitHook (setDefaultDbPath );
131+ bundle .addInitHook (migrateDb );
132+ bundle .addInitHook (loadCerts );
125133 }
126134
127- pub fn initServer (target : * tk.Server , allocator : std.mem.Allocator , cfg : @FieldType (Config , "server" ), injector : * tk .Injector ) ! void {
128- target .* = try tk .Server .init (allocator , routes , .{
129- .listen = cfg ,
130- .injector = injector ,
131- });
135+ fn setDefaultDbPath (db_opts : * fr.SQLite3.Options , home : * Home ) ! void {
136+ db_opts .dir = db_opts .dir orelse home .path ;
132137 }
133138
134- pub fn afterAppInit (allocator : std.mem.Allocator , db_pool : * fr .Pool (fr.SQLite3 ), client : * std . http . Client ) ! void {
139+ fn migrateDb (allocator : std.mem.Allocator , db_pool : * fr .Pool (fr.SQLite3 )) ! void {
135140 var db = try db_pool .getSession (allocator );
136141 defer db .deinit ();
137142
138- try fr .migrate (db , @embedFile ("db_schema.sql" ));
143+ try fr .migrate (& db , @embedFile ("db_schema.sql" ));
144+ }
139145
146+ fn loadCerts (allocator : std.mem.Allocator , client : * std.http.Client ) ! void {
140147 if (comptime builtin .target .os .tag == .windows ) {
141148 try client .ca_bundle .rescan (allocator );
142149 const start = client .ca_bundle .bytes .items .len ;
0 commit comments