-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathserver.go
More file actions
98 lines (82 loc) · 2.45 KB
/
server.go
File metadata and controls
98 lines (82 loc) · 2.45 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-FileCopyrightText: 2021 Open Networking Foundation <info@opennetworking.org>
// Copyright 2019 free5GC.org
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/omec-project/webconsole/backend/factory"
"github.com/omec-project/webconsole/backend/logger"
"github.com/omec-project/webconsole/backend/nfconfig"
"github.com/omec-project/webconsole/backend/webui_service"
"github.com/omec-project/webconsole/dbadapter"
"github.com/urfave/cli/v3"
)
var (
initMongoDB = dbadapter.InitMongoDB
newNFConfigServer = nfconfig.NewNFConfigServer
runServer = runWebUIAndNFConfig
)
func main() {
app := &cli.Command{}
app.Name = "webui"
logger.AppLog.Infoln(app.Name)
app.Usage = "Web UI"
app.UsageText = "webconsole -cfg <webui_config_file.yaml>"
app.Flags = factory.GetCliFlags()
app.Action = action
if err := app.Run(context.Background(), os.Args); err != nil {
logger.AppLog.Fatalf("error args: %v", err)
}
}
func action(ctx context.Context, c *cli.Command) error {
cfgPath := c.String("cfg")
if cfgPath == "" {
return fmt.Errorf("required flag cfg not set")
}
absPath, err := filepath.Abs(cfgPath)
if err != nil {
return fmt.Errorf("failed to resolve config path: %w", err)
}
if err := factory.InitConfigFactory(absPath); err != nil {
return fmt.Errorf("failed to init config: %w", err)
}
config := factory.WebUIConfig
if config == nil {
return fmt.Errorf("configuration not properly initialized")
}
factory.SetLogLevelsFromConfig(config)
return startApplication(config)
}
func startApplication(config *factory.Config) error {
if config == nil || config.Configuration == nil {
return fmt.Errorf("configuration section is nil")
}
if err := initMongoDB(); err != nil {
logger.InitLog.Errorf("failed to initialize MongoDB: %v", err)
return err
}
webui := &webui_service.WEBUI{}
nfConfigServer, err := newNFConfigServer(config)
if err != nil {
return fmt.Errorf("failed to initialize NFConfig: %w", err)
}
return runServer(webui, nfConfigServer)
}
func runWebUIAndNFConfig(webui webui_service.WebUIInterface, nfConf nfconfig.NFConfigInterface) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
syncChan := make(chan struct{}, 1)
go webui.Start(ctx, syncChan)
logger.InitLog.Infoln("WebUI started")
err := nfConf.Start(ctx, syncChan)
if err != nil {
cancel()
return fmt.Errorf("NFConfig failed: %w", err)
}
return nil
}