-
Notifications
You must be signed in to change notification settings - Fork 657
/
server.go
162 lines (131 loc) · 5.06 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
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/api/middleware"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub"
handler2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/projects"
"context"
"fmt"
"net"
"net/http"
"runtime"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/gorilla/websocket"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/generated"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/config"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/handlers"
pb "github.com/litmuschaos/litmus/chaoscenter/graphql/server/protos"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetReportCaller(true)
log.Printf("go version: %s", runtime.Version())
log.Printf("go os/arch: %s/%s", runtime.GOOS, runtime.GOARCH)
err := envconfig.Process("", &utils.Config)
if err != nil {
log.Fatal(err)
}
}
func validateVersion() error {
currentVersion := utils.Config.Version
dbVersion, err := config.GetConfig(context.Background(), "version")
if err != nil {
return fmt.Errorf("failed to get version from db, error = %w", err)
}
if dbVersion == nil {
err := config.CreateConfig(
context.Background(),
&config.ServerConfig{Key: "version", Value: currentVersion},
)
if err != nil {
return fmt.Errorf("failed to insert current version in db, error = %w", err)
}
return nil
}
if dbVersion.Value.(string) != currentVersion {
return fmt.Errorf("control plane needs to be upgraded from version %v to %v", dbVersion.Value.(string), currentVersion)
}
return nil
}
func setupGin() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(middleware.DefaultStructuredLogger())
router.Use(gin.Recovery())
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowHeaders: []string{"*"},
AllowCredentials: true,
}))
return router
}
func main() {
router := setupGin()
var err error
mongodb.MgoClient, err = mongodb.MongoConnection()
if err != nil {
log.Fatal(err)
}
mongoClient := mongodb.Client.Initialize(mongodb.MgoClient)
var mongodbOperator mongodb.MongoOperator = mongodb.NewMongoOperations(mongoClient)
mongodb.Operator = mongodbOperator
if err := validateVersion(); err != nil {
log.Fatal(err)
}
go startGRPCServer(utils.Config.RpcPort, mongodbOperator) // start GRPC serve
srv := handler.New(generated.NewExecutableSchema(graph.NewConfig(mongodbOperator)))
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
})
// to be removed in production
srv.Use(extension.Introspection{})
// go routine for syncing chaos hubs
go chaoshub.NewService(dbSchemaChaosHub.NewChaosHubOperator(mongodbOperator)).RecurringHubSync()
go chaoshub.NewService(dbSchemaChaosHub.NewChaosHubOperator(mongodbOperator)).SyncDefaultChaosHubs()
// routers
router.GET("/", handlers.PlaygroundHandler())
router.Any("/query", authorization.Middleware(srv, mongodb.MgoClient))
router.Any("/file/:key", handlers.FileHandler(mongodbOperator))
//chaos hub routers
router.GET("/icon/:projectId/:hubName/:chartName/:iconName", handler2.ChaosHubIconHandler())
router.GET("/icon/default/:hubName/:chartName/:iconName", handler2.DefaultChaosHubIconHandler())
//general routers
router.GET("/status", handlers.StatusHandler())
router.GET("/readiness", handlers.ReadinessHandler())
projectEventChannel := make(chan string)
go projects.ProjectEvents(projectEventChannel, mongodb.MgoClient, mongodbOperator)
log.Infof("chaos manager running at http://localhost:%s", utils.Config.HttpPort)
log.Fatal(http.ListenAndServe(":"+utils.Config.HttpPort, router))
}
// startGRPCServer initializes, registers services to and starts the gRPC server for RPC calls
func startGRPCServer(port string, mongodbOperator mongodb.MongoOperator) {
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal("failed to listen: %w", err)
}
grpcServer := grpc.NewServer()
// Register services
pb.RegisterProjectServer(grpcServer, &projects.ProjectServer{Operator: mongodbOperator})
log.Infof("GRPC server listening on %v", lis.Addr())
log.Fatal(grpcServer.Serve(lis))
}