Skip to content

Commit be7cf67

Browse files
authored
Add go docs (gofr-dev#875)
1 parent 31b001b commit be7cf67

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
@@ -103,7 +103,7 @@ func New() *App {
103103

104104
app.subscriptionManager = newSubscriptionManager(app.container)
105105

106-
// static fileserver
106+
// static file server
107107
currentWd, _ := os.Getwd()
108108
checkDirectory := filepath.Join(currentWd, defaultPublicStaticDir)
109109

@@ -272,10 +272,12 @@ func (a *App) add(method, pattern string, h Handler) {
272272
})
273273
}
274274

275+
// Metrics returns the metrics manager associated with the App.
275276
func (a *App) Metrics() metrics.Manager {
276277
return a.container.Metrics()
277278
}
278279

280+
// Logger returns the logger instance associated with the App.
279281
func (a *App) Logger() logging.Logger {
280282
return a.container.Logger
281283
}
@@ -286,6 +288,10 @@ func (a *App) SubCommand(pattern string, handler Handler, options ...Options) {
286288
a.cmd.addRoute(pattern, handler, options...)
287289
}
288290

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.
289295
func (a *App) Migrate(migrationsMap map[int64]migration.Migrate) {
290296
// TODO : Move panic recovery at central location which will manage for all the different cases.
291297
defer func() {
@@ -400,6 +406,10 @@ func (o *otelErrorHandler) Handle(e error) {
400406
o.logger.Error(e.Error())
401407
}
402408

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.
403413
func (a *App) EnableBasicAuth(credentials ...string) {
404414
if len(credentials)%2 != 0 {
405415
a.container.Error("Invalid number of arguments for EnableBasicAuth")
@@ -419,11 +429,18 @@ func (a *App) EnableBasicAuthWithFunc(validateFunc func(username, password strin
419429
a.httpServer.router.Use(middleware.BasicAuthMiddleware(middleware.BasicAuthProvider{ValidateFunc: validateFunc, Container: a.container}))
420430
}
421431

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.
422436
func (a *App) EnableBasicAuthWithValidator(validateFunc func(c *container.Container, username, password string) bool) {
423437
a.httpServer.router.Use(middleware.BasicAuthMiddleware(middleware.BasicAuthProvider{
424438
ValidateFuncWithDatasources: validateFunc, Container: a.container}))
425439
}
426440

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.
427444
func (a *App) EnableAPIKeyAuth(apiKeys ...string) {
428445
a.httpServer.router.Use(middleware.APIKeyAuthMiddleware(middleware.APIKeyAuthProvider{}, apiKeys...))
429446
}
@@ -437,13 +454,24 @@ func (a *App) EnableAPIKeyAuthWithFunc(validateFunc func(apiKey string) bool) {
437454
}))
438455
}
439456

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.
440461
func (a *App) EnableAPIKeyAuthWithValidator(validateFunc func(c *container.Container, apiKey string) bool) {
441462
a.httpServer.router.Use(middleware.APIKeyAuthMiddleware(middleware.APIKeyAuthProvider{
442463
ValidateFuncWithDatasources: validateFunc,
443464
Container: a.container,
444465
}))
445466
}
446467

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

@@ -455,6 +483,10 @@ func (a *App) EnableOAuth(jwksEndpoint string, refreshInterval int) {
455483
a.httpServer.router.Use(middleware.OAuth(middleware.NewOAuth(oauthOption)))
456484
}
457485

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.
458490
func (a *App) Subscribe(topic string, handler SubscribeFunc) {
459491
if a.container.GetSubscriber() == nil {
460492
a.container.Logger.Errorf("subscriber not initialized in the container")
@@ -483,8 +515,8 @@ func (a *App) UseMiddleware(middlewares ...gofrHTTP.Middleware) {
483515
a.httpServer.router.UseMiddleware(middlewares...)
484516
}
485517

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.
488520
func (a *App) AddCronJob(schedule, jobName string, job CronFunc) {
489521
if a.cron == nil {
490522
a.cron = NewCron(a.container)
@@ -506,6 +538,12 @@ func contains(elems []string, v string) bool {
506538
return false
507539
}
508540

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

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)