-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (95 loc) · 3.14 KB
/
Copy pathmain.go
File metadata and controls
109 lines (95 loc) · 3.14 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
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/hyperledger/fabric-lib-go/common/flogging"
"github.com/hyperledger/fabric-x-committer/utils/serve"
"github.com/hyperledger/fabric-x-common/common/viperutil"
"github.com/hyperledger/fabric-x-samples/custom-endorser/config"
"github.com/hyperledger/fabric-x-samples/custom-endorser/service"
"github.com/spf13/cobra"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
cmd := &cobra.Command{
Use: "endorser",
Short: "Endorser - Example Fabric or Fabric-X endorser service",
Long: `Endorser exposes the Fabric ProcessProposal endpoint and synchronizes its
world state with a committing peer. It can create endorsed read/write sets for either
Fabric or Fabric-X. This enables an alternative deployment model to chaincode for Fabric,
and make is possible to add chaincode-like functionality to Fabric-X networks.`,
RunE: run,
}
cmd.Flags().StringP("config", "c", "", "Path to configuration file")
cmd.Flags().String("log-level", "INFO", "Log level (DEBUG, INFO, WARNING, ERROR)")
cmd.MarkFlagRequired("config")
if err := cmd.ExecuteContext(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) error {
// Load configuration
parser := viperutil.New()
parser.SetConfigName("endorser")
configFile, _ := cmd.Flags().GetString("config")
f, err := os.Open(configFile)
if err != nil {
return fmt.Errorf("failed to open config: %w", err)
}
if err := parser.ReadConfig(f); err != nil {
f.Close()
return fmt.Errorf("failed to read config: %w", err)
}
f.Close()
// Parse and validate
var cfg config.Config
if err := parser.EnhancedExactUnmarshal(&cfg); err != nil {
return fmt.Errorf("invalid config: %w", err)
}
if err := cfg.Validate(); err != nil {
return fmt.Errorf("invalid config: %w", err)
}
// Setup logging
if cmd.Flags().Changed("log-level") {
logLevel, _ := cmd.Flags().GetString("log-level")
cfg.Logging.LogSpec = logLevel
}
flogging.Init(cfg.Logging)
logger := flogging.MustGetLogger("main")
logger.Infof("starting endorser (config: %v)", parser.ConfigFileUsed())
// Create service
executors := map[string]service.Executor{
"mynamespace": SampleExecutor{},
}
svcCfg := service.ServiceConfig{
ChannelID: cfg.ChannelID,
Protocol: cfg.Protocol,
Committer: cfg.Committer.ToPeerConf(),
DBConnStr: cfg.Database.ConnStr,
}
svc, err := service.New(svcCfg, cfg.Identity.MSPDir, cfg.Identity.MspID, executors, flogging.MustGetLogger("endorser"))
if err != nil {
return fmt.Errorf("failed to create service: %w", err)
}
// Start service
// This handles:
// - gRPC server creation with TLS
// - Network listener binding
// - Service registration
// - Background task execution
// - Graceful shutdown on context cancellation
if err := serve.StartAndServe(cmd.Context(), svc, &serve.Config{GRPC: *cfg.Server}); err != nil {
return fmt.Errorf("service failed: %w", err)
}
logger.Info("service shutdown complete")
return nil
}