-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
55 lines (41 loc) · 1.71 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
package main
import (
"errors"
"log/slog"
"github.com/chadweimer/gomp/db"
"github.com/chadweimer/gomp/upload"
)
const defaultSecureKey = "ChangeMe"
// Config represents the application configuration settings
type Config struct {
// Upload contains the upload configuration settings
Upload upload.Config
// Database contains the database configuration settings
Database db.Config
// Port gets the port number under which the site is being hosted.
Port int `env:"PORT" default:"5000"`
// IsDevelopment defines whether to run the application in "development mode".
// Development mode turns on additional features, such as logging, that may
// not be desirable in a production environment.
IsDevelopment bool `env:"IS_DEVELOPMENT" default:"false"`
// BaseAssetsPath gets the base path to the client assets.
BaseAssetsPath string `env:"BASE_ASSETS_PATH" default:"static"`
// SecureKeys is used for session authentication. Recommended to be 32 or 64 ASCII characters.
// Multiple keys can be separated by commas.
SecureKeys []string `env:"SECURE_KEY" default:"ChangeMe"`
}
func (c Config) validate() error {
errs := make([]error, 0)
if c.Port <= 0 {
errs = append(errs, errors.New("port must be a positive integer"))
}
if c.BaseAssetsPath == "" {
errs = append(errs, errors.New("base assets path must be specified"))
}
if len(c.SecureKeys) == 0 {
errs = append(errs, errors.New("secure keys must be specified with 1 or more keys separated by a comma"))
} else if len(c.SecureKeys) == 1 && c.SecureKeys[0] == defaultSecureKey {
slog.Warn("Using default secure key. It is highly recommended that this be changed to something unique.", slog.String("value", defaultSecureKey))
}
return errors.Join(errs...)
}