| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 1 | package model |
| 2 | |
| 3 | const ( |
| 4 | databaseDriverPostgres = "postgres" |
| 5 | defaultDataSource = "postgres://user:test@localhost/pcloud_test?sslmode=disable&connect_timeout=10" |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame^] | 6 | |
| 7 | defaultHTTPHost = "0.0.0.0" |
| 8 | defaultHTTPPort = 9086 |
| 9 | defaultHTTPReadTimeout = 5 |
| 10 | defaultHTTPWriteTimeout = 10 |
| 11 | defaultHTTPIdleTimeout = 120 |
| 12 | |
| 13 | defaultGRPCPort = 9087 |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | type Config struct { |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame^] | 17 | SQLSettings SQLSettings |
| 18 | HTTPSettings HTTPSettings |
| 19 | GRPCSettings GRPCSettings |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 20 | } |
| 21 | |
| iomodo | c6abf5b | 2021-02-19 14:48:03 +0400 | [diff] [blame] | 22 | func NewConfig() *Config { |
| 23 | config := &Config{} |
| 24 | config.SetDefaults() |
| 25 | return config |
| 26 | } |
| 27 | |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 28 | func (c *Config) SetDefaults() { |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame^] | 29 | c.SQLSettings.SetDefaults() |
| 30 | c.HTTPSettings.SetDefaults() |
| 31 | c.GRPCSettings.SetDefaults() |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 32 | } |
| 33 | |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame^] | 34 | type SQLSettings struct { |
| 35 | DriverName string |
| 36 | DataSource string |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 37 | } |
| 38 | |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame^] | 39 | func (s *SQLSettings) SetDefaults() { |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 40 | if s.DriverName == "" { |
| 41 | s.DriverName = databaseDriverPostgres |
| 42 | } |
| 43 | |
| 44 | if s.DataSource == "" { |
| 45 | s.DataSource = defaultDataSource |
| 46 | } |
| 47 | } |
| iomodo | b892d07 | 2021-02-22 00:23:16 +0400 | [diff] [blame^] | 48 | |
| 49 | type HTTPSettings struct { |
| 50 | Host string |
| 51 | Port int |
| 52 | ReadTimeout int |
| 53 | WriteTimeout int |
| 54 | IdleTimeout int |
| 55 | } |
| 56 | |
| 57 | func (s *HTTPSettings) SetDefaults() { |
| 58 | if s.Host == "" { |
| 59 | s.Host = defaultHTTPHost |
| 60 | } |
| 61 | |
| 62 | if s.Port == 0 { |
| 63 | s.Port = defaultHTTPPort |
| 64 | } |
| 65 | |
| 66 | if s.ReadTimeout == 0 { |
| 67 | s.ReadTimeout = defaultHTTPReadTimeout |
| 68 | } |
| 69 | |
| 70 | if s.WriteTimeout == 0 { |
| 71 | s.WriteTimeout = defaultHTTPWriteTimeout |
| 72 | } |
| 73 | |
| 74 | if s.IdleTimeout == 0 { |
| 75 | s.IdleTimeout = defaultHTTPIdleTimeout |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | type GRPCSettings struct { |
| 80 | Port int |
| 81 | } |
| 82 | |
| 83 | func (s *GRPCSettings) SetDefaults() { |
| 84 | if s.Port == 0 { |
| 85 | s.Port = defaultGRPCPort |
| 86 | } |
| 87 | } |