@@ -106,7 +106,7 @@ func New() *App {
106
106
107
107
app .subscriptionManager = newSubscriptionManager (app .container )
108
108
109
- // static fileserver
109
+ // static file server
110
110
currentWd , _ := os .Getwd ()
111
111
checkDirectory := filepath .Join (currentWd , defaultPublicStaticDir )
112
112
@@ -275,10 +275,12 @@ func (a *App) add(method, pattern string, h Handler) {
275
275
})
276
276
}
277
277
278
+ // Metrics returns the metrics manager associated with the App.
278
279
func (a * App ) Metrics () metrics.Manager {
279
280
return a .container .Metrics ()
280
281
}
281
282
283
+ // Logger returns the logger instance associated with the App.
282
284
func (a * App ) Logger () logging.Logger {
283
285
return a .container .Logger
284
286
}
@@ -289,6 +291,10 @@ func (a *App) SubCommand(pattern string, handler Handler, options ...Options) {
289
291
a .cmd .addRoute (pattern , handler , options ... )
290
292
}
291
293
294
+ // Migrate applies a set of migrations to the application's database.
295
+ //
296
+ // The migrationsMap argument is a map where the key is the version number of the migration
297
+ // and the value is a migration.Migrate instance that implements the migration logic.
292
298
func (a * App ) Migrate (migrationsMap map [int64 ]migration.Migrate ) {
293
299
// TODO : Move panic recovery at central location which will manage for all the different cases.
294
300
defer func () {
@@ -423,6 +429,10 @@ func (o *otelErrorHandler) Handle(e error) {
423
429
o .logger .Error (e .Error ())
424
430
}
425
431
432
+ // EnableBasicAuth enables basic authentication for the application.
433
+ //
434
+ // It takes a variable number of credentials as alternating username and password strings.
435
+ // An error is logged if an odd number of arguments is provided.
426
436
func (a * App ) EnableBasicAuth (credentials ... string ) {
427
437
if len (credentials )% 2 != 0 {
428
438
a .container .Error ("Invalid number of arguments for EnableBasicAuth" )
@@ -442,11 +452,18 @@ func (a *App) EnableBasicAuthWithFunc(validateFunc func(username, password strin
442
452
a .httpServer .router .Use (middleware .BasicAuthMiddleware (middleware.BasicAuthProvider {ValidateFunc : validateFunc , Container : a .container }))
443
453
}
444
454
455
+ // EnableBasicAuthWithValidator enables basic authentication for the HTTP server with a custom validator.
456
+ //
457
+ // The provided `validateFunc` is invoked for each authentication attempt. It receives a container instance,
458
+ // username, and password. The function should return `true` if the credentials are valid, `false` otherwise.
445
459
func (a * App ) EnableBasicAuthWithValidator (validateFunc func (c * container.Container , username , password string ) bool ) {
446
460
a .httpServer .router .Use (middleware .BasicAuthMiddleware (middleware.BasicAuthProvider {
447
461
ValidateFuncWithDatasources : validateFunc , Container : a .container }))
448
462
}
449
463
464
+ // EnableAPIKeyAuth enables API key authentication for the application.
465
+ //
466
+ // It requires at least one API key to be provided. The provided API keys will be used to authenticate requests.
450
467
func (a * App ) EnableAPIKeyAuth (apiKeys ... string ) {
451
468
a .httpServer .router .Use (middleware .APIKeyAuthMiddleware (middleware.APIKeyAuthProvider {}, apiKeys ... ))
452
469
}
@@ -460,13 +477,24 @@ func (a *App) EnableAPIKeyAuthWithFunc(validateFunc func(apiKey string) bool) {
460
477
}))
461
478
}
462
479
480
+ // EnableAPIKeyAuthWithValidator enables API key authentication for the application with a custom validation function.
481
+ //
482
+ // The provided `validateFunc` is used to determine the validity of an API key. It receives the request container
483
+ // and the API key as arguments and should return `true` if the key is valid, `false` otherwise.
463
484
func (a * App ) EnableAPIKeyAuthWithValidator (validateFunc func (c * container.Container , apiKey string ) bool ) {
464
485
a .httpServer .router .Use (middleware .APIKeyAuthMiddleware (middleware.APIKeyAuthProvider {
465
486
ValidateFuncWithDatasources : validateFunc ,
466
487
Container : a .container ,
467
488
}))
468
489
}
469
490
491
+ // EnableOAuth configures OAuth middleware for the application.
492
+ //
493
+ // It registers a new HTTP service for fetching JWKS and sets up OAuth middleware
494
+ // with the given JWKS endpoint and refresh interval.
495
+ //
496
+ // The JWKS endpoint is used to retrieve JSON Web Key Sets for verifying tokens.
497
+ // The refresh interval specifies how often to refresh the token cache.
470
498
func (a * App ) EnableOAuth (jwksEndpoint string , refreshInterval int ) {
471
499
a .AddHTTPService ("gofr_oauth" , jwksEndpoint )
472
500
@@ -478,6 +506,10 @@ func (a *App) EnableOAuth(jwksEndpoint string, refreshInterval int) {
478
506
a .httpServer .router .Use (middleware .OAuth (middleware .NewOAuth (oauthOption )))
479
507
}
480
508
509
+ // Subscribe registers a handler for the given topic.
510
+ //
511
+ // If the subscriber is not initialized in the container, an error is logged and
512
+ // the subscription is not registered.
481
513
func (a * App ) Subscribe (topic string , handler SubscribeFunc ) {
482
514
if a .container .GetSubscriber () == nil {
483
515
a .container .Logger .Errorf ("subscriber not initialized in the container" )
@@ -506,8 +538,8 @@ func (a *App) UseMiddleware(middlewares ...gofrHTTP.Middleware) {
506
538
a .httpServer .router .UseMiddleware (middlewares ... )
507
539
}
508
540
509
- // AddCronJob registers a cron job to the cron table, the schedule is in * * * * * (6 part) format
510
- // denoting minutes, hours, days, months and day of week respectively.
541
+ // AddCronJob registers a cron job to the cron table, the schedule is in * * * * * (5 part) format
542
+ // denoting minute, hour, day, month and day of week respectively.
511
543
func (a * App ) AddCronJob (schedule , jobName string , job CronFunc ) {
512
544
if a .cron == nil {
513
545
a .cron = NewCron (a .container )
@@ -529,6 +561,12 @@ func contains(elems []string, v string) bool {
529
561
return false
530
562
}
531
563
564
+ // AddStaticFiles registers a static file endpoint for the application.
565
+ //
566
+ // The provided `endpoint` will be used as the prefix for the static file
567
+ // server. The `filePath` specifies the directory containing the static files.
568
+ // If `filePath` starts with "./", it will be interpreted as a relative path
569
+ // to the current working directory.
532
570
func (a * App ) AddStaticFiles (endpoint , filePath string ) {
533
571
a .httpRegistered = true
534
572
0 commit comments