go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/apputil/config.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package apputil 9 10 import ( 11 "context" 12 13 "go.charczuk.com/sdk/configmeta" 14 "go.charczuk.com/sdk/configutil" 15 "go.charczuk.com/sdk/db" 16 "go.charczuk.com/sdk/logutil" 17 "go.charczuk.com/sdk/oauth" 18 "go.charczuk.com/sdk/web" 19 ) 20 21 var ( 22 _ LoggerProvider = (*Config)(nil) 23 _ DBProvider = (*Config)(nil) 24 ) 25 26 // LoggerProvider is a type that provides a logger config. 27 type LoggerProvider interface { 28 GetLogger() logutil.Config 29 } 30 31 // DBProvider is a type that provides a db config. 32 type DBProvider interface { 33 GetDB() db.Config 34 } 35 36 // MetaProvider is a type that provides a db config. 37 type MetaProvider interface { 38 GetMeta() configmeta.Meta 39 } 40 41 type Config struct { 42 configmeta.Meta `yaml:",inline"` 43 44 Logger logutil.Config `yaml:"logger"` 45 OAuth oauth.Config `yaml:"oauth"` 46 Web web.Config `yaml:"web"` 47 DB db.Config `yaml:"db"` 48 } 49 50 // ResolveOAuthRedirectURL resolves the oauth redirect url if it's not set. 51 func (c *Config) ResolveOAuthRedirectURL(ctx context.Context) (*string, error) { 52 redirectURL := "http://localhost/oauth/google" 53 return &redirectURL, nil 54 } 55 56 // GetLogger returns the logger config. 57 // 58 // This is required for `EntrypointConfigProvider` 59 func (c Config) GetLogger() logutil.Config { 60 return c.Logger 61 } 62 63 // GetDB returns the db config. 64 // 65 // This is required for `EntrypointConfigProvider` 66 func (c Config) GetDB() db.Config { 67 return c.DB 68 } 69 70 // GetMeta returns the meta config. 71 // 72 // This is required for `EntrypointConfigProvider` 73 func (c Config) GetMeta() configmeta.Meta { 74 return c.Meta 75 } 76 77 // Resolve resolves the config. 78 func (c *Config) Resolve(ctx context.Context) (err error) { 79 return configutil.Resolve(ctx, 80 (&c.Meta).Resolve, 81 (&c.OAuth).Resolve, 82 (&c.Web).Resolve, 83 (&c.DB).Resolve, 84 configutil.Set(&c.ServiceName, configutil.Env[string]("SERVICE_NAME"), configutil.Lazy(&c.ServiceName), configutil.Const("kana-server")), 85 configutil.Set(&c.Version, configutil.Env[string]("VERSION"), configutil.Lazy(&c.Version), configutil.Lazy(&configmeta.Version)), 86 configutil.Set(&c.GitRef, configutil.Env[string]("GIT_REF"), configutil.Lazy(&c.GitRef), configutil.Lazy(&configmeta.GitRef)), 87 configutil.Set(&c.OAuth.RedirectURL, configutil.Lazy(&c.OAuth.RedirectURL), c.ResolveOAuthRedirectURL), 88 ) 89 }