Repository navigation
Expand file tree
/
Copy pathcore.go
More file actions
70 lines (58 loc) · 1.76 KB
/
Copy pathcore.go
File metadata and controls
70 lines (58 loc) · 1.76 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
//
// Copyright © Manetu Inc. All rights reserved.
//
package serve
import (
"context"
"os"
"os/signal"
"github.com/manetu/policyengine/cmd/mpe/common"
"github.com/manetu/policyengine/internal/logging"
"github.com/manetu/policyengine/pkg/core/auxdata"
"github.com/manetu/policyengine/pkg/core/config"
"github.com/manetu/policyengine/pkg/decisionpoint"
"github.com/manetu/policyengine/pkg/decisionpoint/envoy"
"github.com/manetu/policyengine/pkg/decisionpoint/generic"
"github.com/urfave/cli/v3"
)
var logger = logging.GetLogger("policyengine")
const agent string = "serve"
// Execute runs the serve command, starting a decision point server based on the configured protocol.
// It supports both "generic" and "envoy" protocols and gracefully shuts down on interrupt signals.
func Execute(ctx context.Context, cmd *cli.Command) error {
port := cmd.Int("port")
pe, err := common.NewCliPolicyEngine(cmd, os.Stdout)
if err != nil {
return err
}
// Load auxiliary data from configured path or CLI flag
auxPath := config.VConfig.GetString(config.AuxDataPath)
if p := cmd.String("auxdata"); p != "" {
auxPath = p
}
aux, err := auxdata.LoadAuxData(auxPath)
if err != nil {
return err
}
var server decisionpoint.Server
switch cmd.String("protocol") {
case "generic":
server, err = generic.CreateServer(pe, port)
case "envoy":
server, err = envoy.CreateServer(pe, port, cmd.String("name"), aux)
}
if err != nil {
return err
}
// Wait for interrupt signal to gracefully shutdown the server
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
logger.Info(agent, "shutdown", "Shutting down server...")
err = server.Stop(ctx)
if err != nil {
return err
}
logger.Info(agent, "shutdown", "Server exited gracefully.")
return nil
}