/
server.go
185 lines (163 loc) · 5.22 KB
/
server.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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"runtime/debug"
"strings"
"google.golang.org/grpc"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
cmdutils "github.com/panyam/onehub/cmd/utils"
ds "github.com/panyam/onehub/datastore"
v1 "github.com/panyam/onehub/gen/go/onehub/v1"
svc "github.com/panyam/onehub/services"
// This is needed to enable the use of the grpc_cli tool
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
const DEFAULT_DB_ENDPOINT = "postgres://postgres:docker@localhost:5432/onehubdb"
var (
addr = flag.String("addr", ":9000", "Address to start the onehub grpc server on.")
gw_addr = flag.String("gw_addr", ":8080", "Address to start the grpc gateway server on.")
db_endpoint = flag.String("db_endpoint", "", fmt.Sprintf("Endpoint of DB where all topics/messages state are persisted. Default value: ONEHUB_DB_ENDPOINT environment variable or %s", DEFAULT_DB_ENDPOINT))
)
func startGRPCServer(addr string, db *ds.OneHubDB) {
// create new gRPC server
server := grpc.NewServer(
grpc.ChainUnaryInterceptor(
ErrorLogger(),
EnsureAuthIsValid,
),
)
v1.RegisterTopicServiceServer(server, svc.NewTopicService(db))
v1.RegisterMessageServiceServer(server, svc.NewMessageService(db))
if l, err := net.Listen("tcp", addr); err != nil {
log.Fatalf("error in listening on port %s: %v", addr, err)
} else {
// the gRPC server
log.Printf("Starting grpc endpoint on %s:", addr)
reflection.Register(server)
if err := server.Serve(l); err != nil {
log.Fatal("unable to start server", err)
}
}
}
func startGatewayServer(gw_addr, grpc_addr string) {
ctx := context.Background()
mux := runtime.NewServeMux(
runtime.WithMetadata(func(ctx context.Context, request *http.Request) metadata.MD {
//
// Step 2 - Extend the context
//
ctx = metadata.AppendToOutgoingContext(ctx)
//
// Step 3 - get the basic auth params
//
username, password, ok := request.BasicAuth()
if !ok {
return nil
}
md := metadata.Pairs()
md.Append("OneHubUsername", username)
md.Append("OneHubPassword", password)
return md
}))
opts := []grpc.DialOption{grpc.WithInsecure()}
err := v1.RegisterTopicServiceHandlerFromEndpoint(ctx, mux, grpc_addr, opts)
if err != nil {
log.Fatal(err)
}
if err := v1.RegisterMessageServiceHandlerFromEndpoint(ctx, mux, grpc_addr, opts); err != nil {
log.Fatal(err)
}
log.Println("Starting grpc gateway server on: ", gw_addr)
http.ListenAndServe(gw_addr, mux)
}
func EnsureAuthExists(ctx context.Context,
method string, // Method to be invoked on the service (eg GetAlbums)
req, // Request payload (eg GetAlbumsRequest)
reply interface{}, // Response payload (eg GetAlbumsResponse)
cc *grpc.ClientConn, // the underlying connection to the service
invoker grpc.UnaryInvoker, // The next handler
opts ...grpc.CallOption) error {
md, ok := metadata.FromOutgoingContext(ctx)
if ok {
usernames := md.Get("OneHubUsername")
passwords := md.Get("OneHubPassword")
log.Println("UP: ", usernames, passwords)
if len(usernames) > 0 && len(passwords) > 0 {
username := strings.TrimSpace(usernames[0])
password := strings.TrimSpace(passwords[0])
if len(username) > 0 && len(password) > 0 {
// All fine - just call the invoker
return invoker(ctx, method, req, reply, cc, opts...)
}
}
}
return status.Error(codes.NotFound, "BasicAuth params not found")
}
func EnsureAuthIsValid(ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
usernames := md.Get("OneHubUsername")
passwords := md.Get("OneHubPassword")
log.Println("UP: ", usernames, passwords)
if len(usernames) > 0 && len(passwords) > 0 {
username := strings.TrimSpace(usernames[0])
password := strings.TrimSpace(passwords[0])
// Make sure you use better passwords than this!
if len(username) > 0 && password == fmt.Sprintf("%s123", username) {
// All fine - just call the invoker
return handler(ctx, req)
}
}
}
return nil, status.Error(codes.NotFound, "Invalid username/password")
}
func ErrorLogger( /* Add configs here */ ) grpc.UnaryServerInterceptor {
return func(ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
onPanic := func() {
if r := recover(); r != nil {
err = status.Errorf(codes.Internal, "panic: %s", r)
errmsg := fmt.Sprintf("[PANIC] %s\n\n%s", r, string(debug.Stack()))
log.Println(errmsg)
}
}
defer onPanic()
resp, err = handler(ctx, req)
errCode := status.Code(err)
if errCode == codes.Unknown || errCode == codes.Internal {
log.Println("Request handler returned an internal error - reporting it")
return
}
return
}
}
func main() {
flag.Parse()
ohdb := OpenOHDB()
go startGRPCServer(*addr, ohdb)
startGatewayServer(*gw_addr, *addr)
}
func OpenOHDB() *ds.OneHubDB {
if *db_endpoint == "" {
*db_endpoint = cmdutils.GetEnvOrDefault("ONEHUB_DB_ENDPOINT", DEFAULT_DB_ENDPOINT)
}
db, err := cmdutils.OpenDB(*db_endpoint)
if err != nil {
log.Fatal(err)
panic(err)
}
return ds.NewOneHubDB(db)
}