@@ -8,18 +8,21 @@ package factory
88
99import (
1010 "fmt"
11+ "os"
1112 "sync"
1213
1314 "github.com/asaskevich/govalidator"
15+ "github.com/google/uuid"
1416
1517 "github.com/free5gc/webconsole/backend/logger"
1618)
1719
1820const (
19- WebuiDefaultTLSKeyLogPath = "./log/webuisslkey.log"
20- WebuiDefaultCertPemPath = "./cert/webui.pem"
21- WebuiDefaultPrivateKeyPath = "./cert/webui.key"
22- WebuiDefaultConfigPath = "./config/webuicfg.yaml"
21+ WebuiDefaultTLSKeyLogPath = "./log/webuisslkey.log"
22+ WebuiDefaultCertPemPath = "./cert/webui.pem"
23+ WebuiDefaultPrivateKeyPath = "./cert/webui.key"
24+ WebuiDefaultConfigPath = "./config/webuicfg.yaml"
25+ WebuiDefaultNfInstanceIdEnvVar = "WEBUI_NF_INSTANCE_ID"
2326)
2427
2528type Config struct {
@@ -30,6 +33,12 @@ type Config struct {
3033}
3134
3235func (c * Config ) Validate () (bool , error ) {
36+ if configuration := c .Configuration ; configuration != nil {
37+ if result , err := configuration .validate (); err != nil {
38+ return result , err
39+ }
40+ }
41+
3342 result , err := govalidator .ValidateStruct (c )
3443 return result , appendInvalid (err )
3544}
@@ -40,12 +49,22 @@ type Info struct {
4049}
4150
4251type Configuration struct {
52+ NfInstanceId string `yaml:"nfInstanceId,omitempty" valid:"optional,uuidv4"`
4353 WebServer * WebServer `yaml:"webServer,omitempty" valid:"optional"`
4454 Mongodb * Mongodb `yaml:"mongodb" valid:"required"`
4555 NrfUri string `yaml:"nrfUri" valid:"required"`
4656 BillingServer * BillingServer `yaml:"billingServer,omitempty" valid:"required"`
4757}
4858
59+ func (c * Configuration ) validate () (bool , error ) {
60+ if c .NfInstanceId == "" {
61+ c .NfInstanceId = uuid .New ().String ()
62+ }
63+
64+ result , err := govalidator .ValidateStruct (c )
65+ return result , appendInvalid (err )
66+ }
67+
4968type Logger struct {
5069 Enable bool `yaml:"enable" valid:"type(bool)"`
5170 Level string `yaml:"level" valid:"required,in(trace|debug|info|warn|error|fatal|panic)"`
@@ -171,3 +190,28 @@ func (c *Config) GetLogReportCaller() bool {
171190 }
172191 return c .Logger .ReportCaller
173192}
193+
194+ func (c * Config ) GetNfInstanceId () string {
195+ c .RLock ()
196+ defer c .RUnlock ()
197+
198+ var nfInstanceId string
199+
200+ logger .CfgLog .Debugf ("Fetching nfInstanceId from env var \" %s\" " , WebuiDefaultNfInstanceIdEnvVar )
201+
202+ if nfInstanceId = os .Getenv (WebuiDefaultNfInstanceIdEnvVar ); nfInstanceId == "" {
203+ logger .CfgLog .Debugf ("No value found for \" %s\" env, fallback on config nfInstanceId : %s" ,
204+ WebuiDefaultNfInstanceIdEnvVar , c .Configuration .NfInstanceId )
205+ return c .Configuration .NfInstanceId
206+ }
207+
208+ if err := uuid .Validate (nfInstanceId ); err != nil {
209+ logger .CfgLog .Errorf ("Env var \" %s\" is not a valid uuid, " +
210+ "fallback on configuration nfInstanceId : %s" , WebuiDefaultNfInstanceIdEnvVar , c .Configuration .NfInstanceId )
211+ return c .Configuration .NfInstanceId
212+ }
213+
214+ logger .CfgLog .Debugf ("nfInstanceId from %s : %s" , WebuiDefaultNfInstanceIdEnvVar , nfInstanceId )
215+
216+ return nfInstanceId
217+ }
0 commit comments