Skip to content

Commit d1a78df

Browse files
authored
Merge branch 'development' into development
2 parents 3da6893 + be7cf67 commit d1a78df

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

docs/advanced-guide/http-authentication/page.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939

4040
**2. Custom Validation Function**
4141

42-
Use `EnableBasicAuthWithFunc(validationFunc)` to implement your own validation logic for credentials.
42+
Use `EnableBasicAuthWithValidator(validationFunc)` to implement your own validation logic for credentials.
4343
The `validationFunc` takes the username and password as arguments and returns true if valid, false otherwise.
4444

4545
```go
@@ -97,7 +97,7 @@ func main() {
9797
```
9898

9999
**2. Custom Validation Function**
100-
- GoFr allows a custom validator function `apiKeyValidator(apiKey string) bool` for validating APIKeys and pass the func in **_EnableAPIKeyAuthWithFunc(validator)_**
100+
- GoFr allows a custom validator function `apiKeyValidator(apiKey string) bool` for validating APIKeys and pass the func in **_EnableAPIKeyAuthWithValidator(validator)_**
101101

102102
```go
103103
package main

pkg/gofr/external_db.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"gofr.dev/pkg/gofr/container"
55
)
66

7+
// AddMongo sets the Mongo datasource in the app's container.
78
func (a *App) AddMongo(db container.MongoProvider) {
89
db.UseLogger(a.Logger())
910
db.UseMetrics(a.Metrics())
@@ -25,7 +26,7 @@ func (a *App) AddClickhouse(db container.ClickhouseProvider) {
2526
}
2627

2728
// UseMongo sets the Mongo datasource in the app's container.
28-
// Deprecated: Use the NewMongo function AddMongo instead.
29+
// Deprecated: Use the AddMongo method instead.
2930
func (a *App) UseMongo(db container.Mongo) {
3031
a.container.Mongo = db
3132
}

pkg/gofr/gofr.go

+41-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func New() *App {
106106

107107
app.subscriptionManager = newSubscriptionManager(app.container)
108108

109-
// static fileserver
109+
// static file server
110110
currentWd, _ := os.Getwd()
111111
checkDirectory := filepath.Join(currentWd, defaultPublicStaticDir)
112112

@@ -275,10 +275,12 @@ func (a *App) add(method, pattern string, h Handler) {
275275
})
276276
}
277277

278+
// Metrics returns the metrics manager associated with the App.
278279
func (a *App) Metrics() metrics.Manager {
279280
return a.container.Metrics()
280281
}
281282

283+
// Logger returns the logger instance associated with the App.
282284
func (a *App) Logger() logging.Logger {
283285
return a.container.Logger
284286
}
@@ -289,6 +291,10 @@ func (a *App) SubCommand(pattern string, handler Handler, options ...Options) {
289291
a.cmd.addRoute(pattern, handler, options...)
290292
}
291293

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.
292298
func (a *App) Migrate(migrationsMap map[int64]migration.Migrate) {
293299
// TODO : Move panic recovery at central location which will manage for all the different cases.
294300
defer func() {
@@ -423,6 +429,10 @@ func (o *otelErrorHandler) Handle(e error) {
423429
o.logger.Error(e.Error())
424430
}
425431

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.
426436
func (a *App) EnableBasicAuth(credentials ...string) {
427437
if len(credentials)%2 != 0 {
428438
a.container.Error("Invalid number of arguments for EnableBasicAuth")
@@ -442,11 +452,18 @@ func (a *App) EnableBasicAuthWithFunc(validateFunc func(username, password strin
442452
a.httpServer.router.Use(middleware.BasicAuthMiddleware(middleware.BasicAuthProvider{ValidateFunc: validateFunc, Container: a.container}))
443453
}
444454

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.
445459
func (a *App) EnableBasicAuthWithValidator(validateFunc func(c *container.Container, username, password string) bool) {
446460
a.httpServer.router.Use(middleware.BasicAuthMiddleware(middleware.BasicAuthProvider{
447461
ValidateFuncWithDatasources: validateFunc, Container: a.container}))
448462
}
449463

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.
450467
func (a *App) EnableAPIKeyAuth(apiKeys ...string) {
451468
a.httpServer.router.Use(middleware.APIKeyAuthMiddleware(middleware.APIKeyAuthProvider{}, apiKeys...))
452469
}
@@ -460,13 +477,24 @@ func (a *App) EnableAPIKeyAuthWithFunc(validateFunc func(apiKey string) bool) {
460477
}))
461478
}
462479

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.
463484
func (a *App) EnableAPIKeyAuthWithValidator(validateFunc func(c *container.Container, apiKey string) bool) {
464485
a.httpServer.router.Use(middleware.APIKeyAuthMiddleware(middleware.APIKeyAuthProvider{
465486
ValidateFuncWithDatasources: validateFunc,
466487
Container: a.container,
467488
}))
468489
}
469490

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.
470498
func (a *App) EnableOAuth(jwksEndpoint string, refreshInterval int) {
471499
a.AddHTTPService("gofr_oauth", jwksEndpoint)
472500

@@ -478,6 +506,10 @@ func (a *App) EnableOAuth(jwksEndpoint string, refreshInterval int) {
478506
a.httpServer.router.Use(middleware.OAuth(middleware.NewOAuth(oauthOption)))
479507
}
480508

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.
481513
func (a *App) Subscribe(topic string, handler SubscribeFunc) {
482514
if a.container.GetSubscriber() == nil {
483515
a.container.Logger.Errorf("subscriber not initialized in the container")
@@ -506,8 +538,8 @@ func (a *App) UseMiddleware(middlewares ...gofrHTTP.Middleware) {
506538
a.httpServer.router.UseMiddleware(middlewares...)
507539
}
508540

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.
511543
func (a *App) AddCronJob(schedule, jobName string, job CronFunc) {
512544
if a.cron == nil {
513545
a.cron = NewCron(a.container)
@@ -529,6 +561,12 @@ func contains(elems []string, v string) bool {
529561
return false
530562
}
531563

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.
532570
func (a *App) AddStaticFiles(endpoint, filePath string) {
533571
a.httpRegistered = true
534572

pkg/gofr/http_server.go

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ func newHTTPServer(c *container.Container, port int, middlewareConfigs map[strin
3737
}
3838
}
3939

40+
// RegisterProfilingRoutes registers pprof endpoints on the HTTP server.
41+
//
42+
// This method adds the following routes to the server's router:
43+
//
44+
// - /debug/pprof/cmdline
45+
// - /debug/pprof/profile
46+
// - /debug/pprof/symbol
47+
// - /debug/pprof/trace
48+
// - /debug/pprof/ (index)
49+
//
50+
// These endpoints provide various profiling information for the application,
51+
// such as command-line arguments, memory profiles, symbol information, and
52+
// execution traces.
4053
func (s *httpServer) RegisterProfilingRoutes() {
4154
s.router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
4255
s.router.HandleFunc("/debug/pprof/profile", pprof.Profile)

0 commit comments

Comments
 (0)