-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (161 loc) · 4.53 KB
/
main.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/sonr-io/webauthn.io/config"
"github.com/sonr-io/webauthn.io/controller"
db "github.com/sonr-io/webauthn.io/database"
"github.com/sonr-io/webauthn.io/logger"
log "github.com/sonr-io/webauthn.io/logger"
"github.com/sonr-io/webauthn.io/models"
"github.com/sonr-io/webauthn.io/pkg/client"
"github.com/sonr-io/webauthn.io/server"
hw "go.buf.build/grpc/go/sonr-io/highway/v1"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc"
)
const (
// PEM_CERT_FILE is the path to the certificate file.
PEM_CERT_FILE = "cert.pem"
// PEM_KEY_FILE is the file containing the private key.
PEM_KEY_FILE = "key.pem"
)
// Error Definitions
var (
//logger = golog.Default.Child("grpc/highway")
ErrEmptyQueue = errors.New("no items in Transfer Queue")
ErrInvalidQuery = errors.New("no SName or PeerID provided")
ErrMissingParam = errors.New("paramater is missing")
ErrProtocolsNotSet = errors.New("node Protocol has not been initialized")
ErrMethodUnimplemented = errors.New("method is not implemented")
)
func main() {
var ctx context.Context
highwayConfig, err := config.Load()
if err != nil {
log.Fatal(err)
}
authConfig := &config.Config{
HostAddress: highwayConfig.HighwayAddress,
HostPort: highwayConfig.HttpPort,
DBName: highwayConfig.SqlName,
DBPath: highwayConfig.SqlPath,
RelyingParty: highwayConfig.RelyingParty,
RPOrigin: highwayConfig.RPOrigin + highwayConfig.RPPort,
}
err = log.Setup(authConfig)
if err != nil {
log.Fatal(err)
}
// Create the cosmos listener.
l, err := net.Listen(verifyAddress(highwayConfig))
if err != nil {
log.Fatal(err)
}
cosmos, err := client.NewClient(context.Background(), l.Addr().String(), "test", "unimplemented-password")
if err != nil {
log.Fatal(err)
}
// Get TLS config if TLS is enabled
var stub *models.HighwayStub
credentials, err := loadTLSCredentials()
if err != nil {
//logger.Infof("Error loading TLS credentials: ", err)
// If TLS is not enabled, create a new listener.
// Create the RPC Service
stub = &models.HighwayStub{
Host: nil,
Ctx: ctx,
Grpc: grpc.NewServer(),
Cosmos: cosmos.Client,
}
hw.RegisterHighwayServer(stub.Grpc, stub)
//reflection.RegisterReflection(stub.grpc)
//logger.Infof("Starting RPC Service on %s", l.Addr().String())
} else {
// Create the RPC Service
stub = &models.HighwayStub{
Host: nil,
Ctx: ctx,
Grpc: grpc.NewServer(grpc.Creds(credentials)),
Cosmos: cosmos.Client,
}
hw.RegisterHighwayServer(stub.Grpc, stub)
//reflection.RegisterReflection(stub.grpc)
//logger.Infof("Starting RPC Service on %s", l.Addr().String())
}
DB, err := db.Connect(highwayConfig.MongoUri, highwayConfig.MongoCollectionName, highwayConfig.MongoDbName)
if err != nil {
logger.Errorf("database connection failed")
}
ctrl, err := controller.New(DB, highwayConfig, stub)
if err != nil {
log.Fatal(err)
}
server, err := server.NewServer(ctrl, authConfig)
if err != nil {
log.Fatal(err)
}
go server.Start()
// Handle graceful shutdown
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
signal.Notify(c, syscall.SIGINT)
<-c
log.Info("Shutting down...")
server.Shutdown()
}
// verifyAddress verifies the address is valid.
func verifyAddress(cnfg *config.SonrConfig) (string, string) {
// Define Variables
var network string
var address string
var port int
var err error
// Set Network
if cnfg.HighwayNetwork != "tcp" {
network = "tcp"
}
logger.Infof("Network: %s", network)
//set port
port, err = strconv.Atoi(cnfg.GrpcPort)
if err != nil {
return "", err.Error()
}
logger.Infof("Using port %d", port)
// Set Address
if !strings.Contains(cnfg.HighwayAddress, ":") {
address = fmt.Sprintf("%s:%d", cnfg.HighwayAddress, port)
}
logger.Infof("Address: %s", address)
return network, address
}
// Helper function to see if file exists
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func loadTLSCredentials() (credentials.TransportCredentials, error) {
// Load server's certificate and private key
serverCert, err := tls.LoadX509KeyPair(PEM_CERT_FILE, PEM_KEY_FILE)
if err != nil {
return nil, err
}
// Create the credentials and return it
config := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.NoClientCert,
}
return credentials.NewTLS(config), nil
}