-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
65 lines (51 loc) · 1.38 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
package main
import (
"flag"
"fmt"
"log"
"net"
pb "golang-grpc-sample/proto"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"golang-grpc-sample/keycloak"
"golang-grpc-sample/utils"
)
var logger = utils.NewLogger()
func initConfig() {
var configPath string
flag.StringVar(&configPath, "cf", ".", "Path to the config file")
flag.Parse()
viper.SetConfigFile("config/config.toml")
viper.AddConfigPath(configPath)
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Sprintf("No valid config file is provided: %s", err.Error()))
}
viper.SetDefault("keycloak.uri", "http://localhost:8080")
viper.SetDefault("keycloak.master_realm", "master")
}
func main() {
initConfig()
logger.Info("GRPC GO")
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
pb.RegisterKeycloakProvisionServer(
grpcServer,
keycloak.NewKcProvision(
&keycloak.KcProvisionOpts{
KcConfig: keycloak.KcConfig{
MasterRealm: viper.GetString("keycloak.master_realm"),
AdminUsername: viper.GetString("keycloak.username"),
AdminPassword: viper.GetString("keycloak.password"),
KeycloakURI: viper.GetString("keycloak.uri"),
},
},
))
reflection.Register(grpcServer)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %s", err)
}
}