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