Skip to content

Commit 2409810

Browse files
committed
Polishing documentation purged binaries
1 parent 5565b35 commit 2409810

File tree

31 files changed

+677
-120
lines changed

31 files changed

+677
-120
lines changed

EXAMPLES/auth/quickstart/README.md

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
## Overview
44

5-
This example demonstrates the quickstart functionality of the ServiceLib auth package.
5+
This example demonstrates how to quickly set up authentication and authorization using the ServiceLib auth package. It shows the basic steps to create an auth instance, use auth middleware, and perform authorization checks.
66

77
## Features
88

9-
- **Feature 1**: Description of feature 1
10-
- **Feature 2**: Description of feature 2
11-
- **Feature 3**: Description of feature 3
9+
- **Auth Configuration**: Set up auth configuration with JWT secret key
10+
- **Auth Middleware**: Protect HTTP endpoints with auth middleware
11+
- **Authorization Checks**: Check if users are authorized to perform operations
12+
- **User Context**: Access user information from the request context
1213

1314
## Running the Example
1415

@@ -20,36 +21,60 @@ go run main.go
2021

2122
## Code Walkthrough
2223

23-
### Key Component 1
24+
### Auth Configuration and Instance Creation
2425

25-
Description of the first key component in this example:
26+
This example starts by creating an auth configuration with a JWT secret key and then creating an auth instance:
2627

27-
```
28-
// Code snippet for key component 1
28+
```go
29+
// Create a configuration
30+
config := auth.DefaultConfig()
31+
config.JWT.SecretKey = "your-secret-key-that-is-at-least-32-characters-long"
32+
33+
// Create an auth instance
34+
authInstance, err := auth.New(ctx, config, logger)
35+
if err != nil {
36+
logger.Fatal("Failed to create auth instance", zap.Error(err))
37+
}
2938
```
3039

31-
### Key Component 2
40+
### Auth Middleware
3241

33-
Description of the second key component in this example:
42+
The example shows how to use the auth middleware to protect an HTTP endpoint:
3443

35-
```
36-
// Code snippet for key component 2
44+
```go
45+
http.Handle("/", authInstance.Middleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46+
// Handler code
47+
})))
3748
```
3849

39-
### Key Component 3
50+
### Authorization Checks
4051

41-
Description of the third key component in this example:
52+
The example demonstrates how to check if a user is authorized to perform an operation:
4253

43-
```
44-
// Code snippet for key component 3
54+
```go
55+
// Check if the user is authorized to perform an operation
56+
authorized, err := authInstance.IsAuthorized(r.Context(), "read:resource")
57+
if err != nil {
58+
http.Error(w, "Authorization error", http.StatusInternalServerError)
59+
return
60+
}
61+
62+
if !authorized {
63+
http.Error(w, "Forbidden", http.StatusForbidden)
64+
return
65+
}
4566
```
4667

4768
## Expected Output
4869

70+
When you run the example and access the HTTP endpoint with a valid JWT token that has the "read:resource" permission, you should see:
71+
4972
```
50-
Expected output of the example when run
73+
Hello, [user-id]
5174
```
5275

76+
Where [user-id] is the ID of the authenticated user. If the token is invalid or the user doesn't have the required permission, you'll see an error message.
77+
5378
## Related Examples
5479

5580

-21.9 MB
Binary file not shown.
-20.1 MB
Binary file not shown.
-19.8 MB
Binary file not shown.
-4.84 MB
Binary file not shown.

EXAMPLES/db/connection/connection

-27.1 MB
Binary file not shown.
-28.4 MB
Binary file not shown.

EXAMPLES/env/config_integration/README.md

Lines changed: 95 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
## Overview
44

5-
This example demonstrates the config_integration functionality of the ServiceLib env package.
5+
This example demonstrates how to integrate environment variables with a configuration structure using the ServiceLib env package. It shows how to create a structured configuration object that loads its values from environment variables, with validation and type conversion.
66

77
## Features
88

9-
- **Feature 1**: Description of feature 1
10-
- **Feature 2**: Description of feature 2
11-
- **Feature 3**: Description of feature 3
9+
- **Configuration Structure**: Create a typed configuration structure to hold application settings
10+
- **Environment Variable Loading**: Load values from environment variables into the configuration structure
11+
- **Validation**: Validate configuration values for correctness
12+
- **Complex Types**: Handle complex types like slices from environment variables
1213

1314
## Running the Example
1415

@@ -20,47 +21,116 @@ go run main.go
2021

2122
## Code Walkthrough
2223

23-
### Key Component 1
24+
### AppConfig Structure
2425

25-
Description of the first key component in this example:
26+
The AppConfig structure centralizes all configuration settings in one place:
2627

28+
```go
29+
type AppConfig struct {
30+
ServerPort string // Port the server will listen on (from SERVER_PORT)
31+
DatabaseURL string // Connection string for the database (from DATABASE_URL)
32+
LogLevel string // Logging level: debug, info, warn, error (from LOG_LEVEL)
33+
APIKey string // API key for external service authentication (from API_KEY)
34+
Environment string // Application environment: development, testing, staging, production (from APP_ENV)
35+
Features []string // Enabled features as a slice of strings (from comma-separated FEATURES)
36+
}
2737
```
28-
// Code snippet for key component 1
29-
```
30-
31-
### Key Component 2
3238

33-
Description of the second key component in this example:
34-
35-
```
36-
// Code snippet for key component 2
39+
### Loading Configuration
40+
41+
The LoadConfig function retrieves values from environment variables and populates the AppConfig struct:
42+
43+
```go
44+
func LoadConfig() AppConfig {
45+
// Load features from a comma-separated environment variable
46+
featuresStr := env.GetEnv("FEATURES", "basic,standard")
47+
features := strings.Split(featuresStr, ",")
48+
49+
return AppConfig{
50+
ServerPort: env.GetEnv("SERVER_PORT", "8080"),
51+
DatabaseURL: env.GetEnv("DATABASE_URL", "postgres://localhost:5432/mydb"),
52+
LogLevel: env.GetEnv("LOG_LEVEL", "info"),
53+
APIKey: env.GetEnv("API_KEY", ""),
54+
Environment: env.GetEnv("APP_ENV", "development"),
55+
Features: features,
56+
}
57+
}
3758
```
3859

39-
### Key Component 3
40-
41-
Description of the third key component in this example:
42-
43-
```
44-
// Code snippet for key component 3
60+
### Configuration Validation
61+
62+
The Validate method checks the configuration values for correctness:
63+
64+
```go
65+
func (c *AppConfig) Validate() []string {
66+
var errors []string
67+
68+
// Check required values
69+
if c.APIKey == "" {
70+
errors = append(errors, "API_KEY environment variable is required")
71+
}
72+
73+
// Validate log level
74+
validLogLevels := map[string]bool{
75+
"debug": true,
76+
"info": true,
77+
"warn": true,
78+
"error": true,
79+
}
80+
if !validLogLevels[strings.ToLower(c.LogLevel)] {
81+
errors = append(errors, fmt.Sprintf("Invalid log level: %s. Must be one of: debug, info, warn, error", c.LogLevel))
82+
}
83+
84+
// Validate environment
85+
validEnvs := map[string]bool{
86+
"development": true,
87+
"testing": true,
88+
"staging": true,
89+
"production": true,
90+
}
91+
if !validEnvs[strings.ToLower(c.Environment)] {
92+
errors = append(errors, fmt.Sprintf("Invalid environment: %s. Must be one of: development, testing, staging, production", c.Environment))
93+
}
94+
95+
return errors
96+
}
4597
```
4698

4799
## Expected Output
48100

49101
```
50-
Expected output of the example when run
102+
Environment Variables Configuration Integration Example
103+
=====================================================
104+
Server Port: 8080
105+
Database URL: postgres://localhost:5432/mydb
106+
Log Level: info
107+
Environment: development
108+
Features: [basic standard]
109+
Warning: API_KEY environment variable is not set
110+
111+
Configuration Errors:
112+
- API_KEY environment variable is required
113+
114+
Try running this example with different environment variables set:
115+
export SERVER_PORT=9090
116+
export DATABASE_URL="postgres://user:password@localhost:5432/mydb"
117+
export LOG_LEVEL="debug"
118+
export API_KEY="your-api-key"
119+
export APP_ENV="production"
120+
export FEATURES="basic,premium,advanced"
121+
go run EXAMPLES/env/config_integration_example.go
51122
```
52123

53124
## Related Examples
54125

55-
56-
- [basic_usage](../basic_usage/README.md) - Related example for basic_usage
126+
- [basic_usage](../basic_usage/README.md) - Shows basic usage of environment variables
57127

58128
## Related Components
59129

60130
- [env Package](../../../env/README.md) - The env package documentation.
131+
- [Config Package](../../../config/README.md) - The config package for more advanced configuration.
132+
- [Validation Package](../../../validation/README.md) - The validation package for input validation.
61133
- [Errors Package](../../../errors/README.md) - The errors package used for error handling.
62-
- [Context Package](../../../context/README.md) - The context package used for context handling.
63-
- [Logging Package](../../../logging/README.md) - The logging package used for logging.
64134

65135
## License
66136

-4.84 MB
Binary file not shown.

EXAMPLES/graphql/auth_configuration/README.md

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
## Overview
44

5-
This example demonstrates the auth_configuration functionality of the ServiceLib graphql package.
5+
This example demonstrates how to configure the authentication service for use with GraphQL in the ServiceLib library. It shows how to set up JWT authentication settings and configure middleware to bypass authentication for specific endpoints.
66

77
## Features
88

9-
- **Feature 1**: Description of feature 1
10-
- **Feature 2**: Description of feature 2
11-
- **Feature 3**: Description of feature 3
9+
- **JWT Configuration**: Configure JWT token settings including secret key, issuer, and token duration
10+
- **Middleware Skip Paths**: Set up paths that bypass authentication middleware
11+
- **Auth Service Initialization**: Initialize the auth service with the configuration
12+
- **GraphQL Integration**: Prepare auth service for use with GraphQL endpoints
1213

1314
## Running the Example
1415

@@ -20,34 +21,74 @@ go run main.go
2021

2122
## Code Walkthrough
2223

23-
### Key Component 1
24+
### Auth Configuration Setup
2425

25-
Description of the first key component in this example:
26+
The example shows how to create and configure the auth service with JWT settings:
2627

27-
```
28-
// Code snippet for key component 1
28+
```go
29+
// Create a configuration for the auth service
30+
authConfig := auth.DefaultConfig()
31+
authConfig.JWT.SecretKey = "your-secret-key"
32+
authConfig.JWT.Issuer = "your-service"
33+
authConfig.JWT.TokenDuration = 24 * time.Hour
34+
authConfig.Middleware.SkipPaths = []string{"/health", "/metrics", "/playground"}
2935
```
3036

31-
### Key Component 2
37+
### Auth Service Initialization
3238

33-
Description of the second key component in this example:
39+
The example demonstrates how to initialize the auth service with the configuration:
3440

35-
```
36-
// Code snippet for key component 2
41+
```go
42+
// Initialize the auth service
43+
authService, err := auth.New(ctx, authConfig, logger)
44+
if err != nil {
45+
fmt.Printf("Error initializing auth service: %v\n", err)
46+
return
47+
}
3748
```
3849

39-
### Key Component 3
50+
### GraphQL Integration Considerations
4051

41-
Description of the third key component in this example:
52+
The example specifically configures the auth service for GraphQL by:
4253

43-
```
44-
// Code snippet for key component 3
45-
```
54+
1. Setting up skip paths for GraphQL-specific endpoints like `/playground`
55+
2. Configuring JWT settings appropriate for GraphQL authentication
56+
3. Preparing the auth service for integration with GraphQL resolvers
4657

4758
## Expected Output
4859

60+
When you run the example, you should see output similar to:
61+
4962
```
50-
Expected output of the example when run
63+
Example: Configuring auth service for GraphQL
64+
65+
Step 1: Configure the auth service
66+
67+
authConfig := auth.DefaultConfig()
68+
authConfig.JWT.SecretKey = cfg.Auth.JWT.SecretKey
69+
authConfig.JWT.Issuer = cfg.Auth.JWT.Issuer
70+
authConfig.JWT.TokenDuration = cfg.Auth.JWT.TokenDuration
71+
authConfig.Middleware.SkipPaths = []string{"/health", "/metrics", "/playground"}
72+
73+
74+
Step 2: Initialize the auth service
75+
76+
authService, err := auth.New(ctx, authConfig, logger)
77+
if err != nil {
78+
return nil, fmt.Errorf("failed to initialize auth service: %w", err)
79+
}
80+
81+
82+
Auth service configured successfully!
83+
JWT Issuer: your-service
84+
Token Duration: 24h0m0s
85+
Skip Paths: [/health /metrics /playground]
86+
87+
The auth service provides:
88+
- JWT token generation and validation
89+
- Middleware for authenticating HTTP requests
90+
- Functions for checking authorization
91+
- Context utilities for working with user information
5192
```
5293

5394
## Related Examples

0 commit comments

Comments
 (0)