-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
64 lines (57 loc) · 2.23 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package config contains the configuration of the application.
package config
import (
"fmt"
"github.com/kelseyhightower/envconfig"
"github.com/daniel-orlov/quotes-server/internal/transport/middleware/ratelimiter"
)
// Config is the main configuration of the application.
// It is populated from environment variables and defaults.
type Config struct {
// Logging is the logging configuration.
Logging struct {
// LogLevel is the log level to use.
Level string `envconfig:"LOG_LEVEL" default:"debug"`
// LogFormat is the log format to use.
Format string `envconfig:"LOG_FORMAT" default:"console"`
}
// Server is the http server configuration.
Server struct {
// GinMode is the mode to run the server in.
GinMode string `envconfig:"GIN_MODE" default:"release"`
// Port is the port to listen on.
Port int `envconfig:"SERVER_PORT" default:"8080"`
// Meddlewares is the configuration for the middlewares.
Middlewares struct {
// Ratelimiter is the configuration for the ratelimiter middleware.
Ratelimiter struct {
// Rate is the rate at which requests are allowed.
Rate ratelimiter.Rate `envconfig:"RATELIMITER_RATE" default:"second"`
// Limit is the maximum number of requests allowed.
Limit uint `envconfig:"RATELIMITER_LIMIT" default:"5"`
// Key is the key to use for the ratelimiter.
Key ratelimiter.Key `envconfig:"RATELIMITER_KEY" default:"client_ip"`
}
// Proofer is the configuration for the proofer middleware.
Proofer struct {
// ChallengeDifficulty is the difficulty of the proof of work challenge.
ChallengeDifficulty int `envconfig:"CHALLENGE_DIFFICULTY" default:"20"`
// SaltLength is the length of the salt.
SaltLength int `envconfig:"SALT_LENGTH" default:"8"`
}
}
}
}
// NewConfig returns a new Config instance, populated with environment variables and defaults.
func NewConfig() (*Config, error) {
// Create a new Config instance
cfg := &Config{}
// Populate the Config instance with environment variables
err := envconfig.Process("", cfg)
// Return an error if the environment variables could not be processed
if err != nil {
return nil, fmt.Errorf("processing environment variables: %w", err)
}
// Return the Config instance and no error
return cfg, nil
}