github.com/resonatecoop/user-api@v1.0.0-13.0.20220915120639-05dc9c04014a/pkg/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"time"
     7  
     8  	yaml "gopkg.in/yaml.v2"
     9  )
    10  
    11  // Load loads the configuration file from the given path
    12  func Load(path string) (*Configuration, error) {
    13  	data, err := ioutil.ReadFile(path)
    14  	if err != nil {
    15  		return nil, fmt.Errorf("error reading config file, %s", err)
    16  	}
    17  
    18  	var cfg Configuration
    19  	if err := yaml.Unmarshal(data, &cfg); err != nil {
    20  		return nil, fmt.Errorf("unable to decode config into struct, %v", err)
    21  	}
    22  
    23  	return &cfg, nil
    24  }
    25  
    26  // Configuration holds application configuration data
    27  type Configuration struct {
    28  	Server       Server       `yaml:"server,omitempty"`
    29  	RefreshToken RefreshToken `yaml:"refreshtoken,omitempty"`
    30  	DB           DatabaseEnv  `yaml:"database,omitempty"`
    31  	Access       Access       `yaml:"access,omitempty"`
    32  	App          Application  `yaml:"application,omitempty"`
    33  	OpenAPI      OpenAPI      `yaml:"openapi,omitempty"`
    34  	Storage      Storage      `yaml:"storage,omitempty"`
    35  }
    36  
    37  // DatabaseEnv holds dev and test database data
    38  type DatabaseEnv struct {
    39  	Dev  Database `yaml:"dev,omitempty"`
    40  	Test Database `yaml:"test,omitempty"`
    41  }
    42  
    43  // Database holds data necessery for database configuration
    44  type Database struct {
    45  	PSN            string `yaml:"psn,omitempty"`
    46  	LogQueries     bool   `yaml:"log_queries,omitempty"`
    47  	TimeoutSeconds int    `yaml:"timeout_seconds,omitempty"`
    48  }
    49  
    50  // Server holds data necessery for server configuration
    51  type Server struct {
    52  	CertName            string `yaml:"cert_name,omitempty"`
    53  	ReadTimeoutSeconds  int    `yaml:"read_timeout_seconds,omitempty"`
    54  	WriteTimeoutSeconds int    `yaml:"write_timeout_seconds,omitempty"`
    55  }
    56  
    57  type RefreshToken struct {
    58  	Lifetime int `yaml:"lifetime_seconds,omitempty"`
    59  }
    60  
    61  // Access holds service access configuration data
    62  type Access struct {
    63  	NoTokenMethods string `yaml:"no_token_methods,omitempty"`
    64  	PublicMethods  string `yaml:"public_methods,omitempty"`
    65  	WriteMethods   string `yaml:"write_methods,omitempty"`
    66  }
    67  
    68  // Application represents application specific configuration
    69  type Application struct {
    70  	MinPasswordStrength int `yaml:"min_password_strength,omitempty"`
    71  }
    72  
    73  // OpenAPI holds username password for viewing api docs
    74  type OpenAPI struct {
    75  	Username string `yaml:"username,omitempty"`
    76  	Password string `yaml:"password,omitempty"`
    77  }
    78  
    79  // Storage holds data necessary for backblaze configuration in track-server-api
    80  type Storage struct {
    81  	AccountId      string        `yaml:"account_id,omitempty"`
    82  	Key            string        `yaml:"key,omitempty"`
    83  	AuthEndpoint   string        `yaml:"auth_endpoint,omitempty"`
    84  	FileEndpoint   string        `yaml:"file_endpoint,omitempty"`
    85  	UploadEndpoint string        `yaml:"upload_endpoint,omitempty"`
    86  	BucketId       string        `yaml:"bucket_id,omitempty"`
    87  	Timeout        time.Duration `yaml:"timeout,omitempty"`
    88  }