Skip to content

Commit 7e1bd29

Browse files
committed
Merge branch 'config-from-db' into 'ee-v0.9.0'
Config from db See merge request space-up-technologies/space-cloud-ee!1
2 parents 9e4c8fe + 0b250bc commit 7e1bd29

16 files changed

+776
-327
lines changed

Diff for: config/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ type FuncRules map[string]map[string]*Rule // service -> function -> rule
8585

8686
// Realtime holds the config for the realtime module
8787
type Realtime struct {
88-
Enabled bool `json:"enabled" yaml:"enabled"`
89-
Broker string `json:"broker" yaml:"broker"`
90-
Conn string `json:"Conn" yaml:"Conn"`
88+
Enabled bool `json:"enabled" yaml:"enabled"`
89+
Broker utils.Broker `json:"broker" yaml:"broker"`
90+
Conn string `json:"Conn" yaml:"Conn"`
9191
}
9292

9393
// FileStore holds the config for the file store module

Diff for: grpc_server.go

+173-45
Large diffs are not rendered by default.

Diff for: main.go

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67
"os"
@@ -13,7 +14,7 @@ import (
1314
func main() {
1415
app := cli.NewApp()
1516
app.Version = buildVersion
16-
app.Name = "space-cloud"
17+
app.Name = "space-cloud-ee"
1718
app.Usage = "core binary to run space cloud"
1819

1920
app.Commands = []cli.Command{
@@ -33,9 +34,22 @@ func main() {
3334
Usage: "Start grpc on port `GRPC_PORT`",
3435
},
3536
cli.StringFlag{
36-
Name: "config",
37-
Value: "none",
38-
Usage: "Load space cloud config from `FILE`",
37+
Name: "db",
38+
Value: "mongo",
39+
Usage: "Load space cloud config from `DB`",
40+
EnvVar: "DB",
41+
},
42+
cli.StringFlag{
43+
Name: "conn",
44+
Value: "mongodb://localhost:27017",
45+
Usage: "The connection string to connect to config db",
46+
EnvVar: "CONN",
47+
},
48+
cli.StringFlag{
49+
Name: "account",
50+
Value: "none",
51+
Usage: "Start space-cloud with `ACCOUNT`",
52+
EnvVar: "ACCOUNT",
3953
},
4054
cli.BoolFlag{
4155
Name: "prod",
@@ -71,11 +85,17 @@ func actionRun(c *cli.Context) error {
7185
// Load cli flags
7286
port := c.String("port")
7387
grpcPort := c.String("grpc-port")
74-
configPath := c.String("config")
88+
conn := c.String("conn")
89+
db := c.String("db")
90+
account := c.String("account")
7591
isProd := c.Bool("prod")
7692
disableMetrics := c.Bool("disable-metrics")
7793
disableNats := c.Bool("disable-nats")
7894

95+
if account == "none" {
96+
return errors.New("Cannot start space-cloud with no account")
97+
}
98+
7999
// Project and env cannot be changed once space cloud has started
80100
s := initServer(isProd)
81101

@@ -85,15 +105,9 @@ func actionRun(c *cli.Context) error {
85105
fmt.Println("Started nats server on port ", defaultNatsOptions.Port)
86106
}
87107

88-
if configPath != "none" {
89-
conf, err := config.LoadConfigFromFile(configPath)
90-
if err != nil {
91-
return err
92-
}
93-
err = s.loadConfig(conf)
94-
if err != nil {
95-
return err
96-
}
108+
err := s.projects.LoadConfigFromDB(account, db, conn)
109+
if err != nil {
110+
return err
97111
}
98112

99113
// Anonymously collect usage metrics if not explicitly disabled

Diff for: metrics.go

+74-78
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"encoding/json"
7-
"fmt"
8-
"log"
9-
"runtime"
10-
"time"
117

12-
uuid "github.com/satori/go.uuid"
138
"google.golang.org/grpc"
149
"google.golang.org/grpc/credentials"
1510

@@ -85,84 +80,85 @@ func newTransport(host, port string, sslEnabled bool) (*transport, error) {
8580
}
8681

8782
func (s *server) routineMetrics() {
88-
ticker := time.NewTicker(time.Minute * 5)
89-
defer ticker.Stop()
90-
91-
id := uuid.NewV1().String()
92-
col := "metrics"
93-
project := "crm"
94-
m := &proto.Meta{Col: col, DbType: "mongo", Project: project}
95-
96-
// Create the find and update clauses
97-
find := map[string]interface{}{"_id": id}
98-
update := map[string]interface{}{
99-
"$currentDate": map[string]interface{}{
100-
"lastUpdated": map[string]interface{}{"$type": "date"},
101-
"startTime": map[string]interface{}{"$type": "date"},
102-
},
103-
}
104-
set := map[string]interface{}{
105-
"os": runtime.GOOS,
106-
"isProd": s.isProd,
107-
"version": buildVersion,
108-
}
109-
110-
// Connect to metrics server
111-
trans, err := newTransport("spaceuptech.com", "11001", true)
112-
if err != nil {
113-
fmt.Println("Metrics Error -", err)
114-
return
115-
}
116-
117-
s.lock.Lock()
118-
if s.config != nil && s.config.Modules != nil {
119-
set["project"] = getProjectInfo(s.config.Modules)
120-
set["projectId"] = s.config.ID
121-
set["sslEnabled"] = s.config.SSL != nil
122-
}
123-
s.lock.Unlock()
124-
125-
update["$set"] = set
126-
status, err := trans.update(context.TODO(), m, "upsert", find, update)
127-
if err != nil {
128-
fmt.Println("Metrics Error -", err)
129-
return
130-
}
131-
132-
if status != 200 {
133-
fmt.Println("Metrics Error - Upsert failed: Invalid status code ", status)
134-
return
135-
}
136-
137-
for range ticker.C {
138-
update := map[string]interface{}{
139-
"$currentDate": map[string]interface{}{"lastUpdated": map[string]interface{}{"$type": "date"}},
140-
}
141-
142-
s.lock.Lock()
143-
if s.config != nil && s.config.Modules != nil {
144-
set["project"] = getProjectInfo(s.config.Modules)
145-
set["projectId"] = s.config.ID
146-
set["sslEnabled"] = s.config.SSL != nil
147-
}
148-
s.lock.Unlock()
149-
150-
update["$set"] = set
151-
status, err := trans.update(context.TODO(), m, "one", find, update)
152-
if err != nil {
153-
log.Println("Metrics Error -", err)
154-
}
155-
156-
if status != 200 {
157-
log.Println("Metrics Error - Invalid status code ", status)
158-
}
159-
}
83+
// TODO
84+
// ticker := time.NewTicker(time.Minute * 5)
85+
// defer ticker.Stop()
86+
87+
// id := uuid.NewV1().String()
88+
// col := "metrics"
89+
// project := "crm"
90+
// m := &proto.Meta{Col: col, DbType: "mongo", Project: project}
91+
92+
// // Create the find and update clauses
93+
// find := map[string]interface{}{"_id": id}
94+
// update := map[string]interface{}{
95+
// "$currentDate": map[string]interface{}{
96+
// "lastUpdated": map[string]interface{}{"$type": "date"},
97+
// "startTime": map[string]interface{}{"$type": "date"},
98+
// },
99+
// }
100+
// set := map[string]interface{}{
101+
// "os": runtime.GOOS,
102+
// "isProd": s.isProd,
103+
// "version": buildVersion,
104+
// }
105+
106+
// // Connect to metrics server
107+
// trans, err := newTransport("spaceuptech.com", "11001", true)
108+
// if err != nil {
109+
// fmt.Println("Metrics Error -", err)
110+
// return
111+
// }
112+
113+
// s.lock.Lock()
114+
// if s.config != nil && s.config.Modules != nil {
115+
// set["project"] = getProjectInfo(s.config.Modules)
116+
// set["projectId"] = s.config.ID
117+
// set["sslEnabled"] = s.config.SSL != nil
118+
// }
119+
// s.lock.Unlock()
120+
121+
// update["$set"] = set
122+
// status, err := trans.update(context.TODO(), m, "upsert", find, update)
123+
// if err != nil {
124+
// fmt.Println("Metrics Error -", err)
125+
// return
126+
// }
127+
128+
// if status != 200 {
129+
// fmt.Println("Metrics Error - Upsert failed: Invalid status code ", status)
130+
// return
131+
// }
132+
133+
// for range ticker.C {
134+
// update := map[string]interface{}{
135+
// "$currentDate": map[string]interface{}{"lastUpdated": map[string]interface{}{"$type": "date"}},
136+
// }
137+
138+
// s.lock.Lock()
139+
// if s.config != nil && s.config.Modules != nil {
140+
// set["project"] = getProjectInfo(s.config.Modules)
141+
// set["projectId"] = s.config.ID
142+
// set["sslEnabled"] = s.config.SSL != nil
143+
// }
144+
// s.lock.Unlock()
145+
146+
// update["$set"] = set
147+
// status, err := trans.update(context.TODO(), m, "one", find, update)
148+
// if err != nil {
149+
// log.Println("Metrics Error -", err)
150+
// }
151+
152+
// if status != 200 {
153+
// log.Println("Metrics Error - Invalid status code ", status)
154+
// }
155+
// }
160156
}
161157

162158
func getProjectInfo(config *config.Modules) map[string]interface{} {
163159
project := map[string]interface{}{
164160
"crud": []string{},
165-
"functions": map[string]interface{}{"enabled": false},
161+
"functions": map[string]interface{}{"enabled": false},
166162
"realtime": map[string]interface{}{"enabled": false},
167163
"fileStore": map[string]interface{}{"enabled": false},
168164
"auth": []string{},

Diff for: modules/realtime/operations.go

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func (m *Module) Subscribe(ctx context.Context, clientID string, auth *auth.Modu
3131
return nil, err
3232
}
3333

34+
return m.DoRealtimeSubscribe(ctx, clientID, crud, data, sendFeed)
35+
}
36+
37+
// DoRealtimeSubscribe makes the realtime query
38+
func (m *Module) DoRealtimeSubscribe(ctx context.Context, clientID string, crud *crud.Module, data *model.RealtimeRequest, sendFeed SendFeed) ([]*model.FeedData, error) {
3439
readReq := model.ReadRequest{Find: data.Where, Operation: utils.All}
3540
result, err := crud.Read(ctx, data.DBType, data.Project, data.Group, &readReq)
3641
if err != nil {

Diff for: routes.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
package main
22

33
import (
4-
"github.com/spaceuptech/space-cloud/config"
54
"github.com/spaceuptech/space-cloud/utils/handlers"
65
)
76

87
func (s *server) routes() {
98
// Initialize the routes for config management
10-
s.router.Methods("POST").Path("/v1/api/config").HandlerFunc(config.HandleConfig(s.isProd, s.loadConfig))
9+
//s.router.Methods("POST").Path("/v1/api/config").HandlerFunc(config.HandleConfig(s.isProd, s.loadConfig))
1110

1211
// Initialize the route for websocket
1312
s.router.HandleFunc("/v1/api/socket/json", s.handleWebsocket())
1413

1514
// Initialize the routes for functions service
16-
s.router.Methods("POST").Path("/v1/api/{project}/functions/{service}/{func}").HandlerFunc(handlers.HandleFunctionCall(s.functions, s.auth))
15+
s.router.Methods("POST").Path("/v1/api/{project}/functions/{service}/{func}").HandlerFunc(handlers.HandleFunctionCall(s.projects))
1716

1817
// Initialize the routes for the crud operations
19-
s.router.Methods("POST").Path("/v1/api/{project}/crud/{dbType}/batch").HandlerFunc(handlers.HandleCrudBatch(s.isProd, s.auth, s.crud, s.realtime))
18+
s.router.Methods("POST").Path("/v1/api/{project}/crud/{dbType}/batch").HandlerFunc(handlers.HandleCrudBatch(s.isProd, s.projects))
2019

2120
crudRouter := s.router.Methods("POST").PathPrefix("/v1/api/{project}/crud/{dbType}/{col}").Subrouter()
22-
crudRouter.HandleFunc("/create", handlers.HandleCrudCreate(s.isProd, s.auth, s.crud, s.realtime))
23-
crudRouter.HandleFunc("/read", handlers.HandleCrudRead(s.auth, s.crud))
24-
crudRouter.HandleFunc("/update", handlers.HandleCrudUpdate(s.isProd, s.auth, s.crud, s.realtime))
25-
crudRouter.HandleFunc("/delete", handlers.HandleCrudDelete(s.isProd, s.auth, s.crud, s.realtime))
26-
crudRouter.HandleFunc("/aggr", handlers.HandleCrudAggregate(s.auth, s.crud))
21+
crudRouter.HandleFunc("/create", handlers.HandleCrudCreate(s.isProd, s.projects))
22+
crudRouter.HandleFunc("/read", handlers.HandleCrudRead(s.projects))
23+
crudRouter.HandleFunc("/update", handlers.HandleCrudUpdate(s.isProd, s.projects))
24+
crudRouter.HandleFunc("/delete", handlers.HandleCrudDelete(s.isProd, s.projects))
25+
crudRouter.HandleFunc("/aggr", handlers.HandleCrudAggregate(s.projects))
2726

2827
// Initialize the routes for the user management operations
2928
userRouter := s.router.PathPrefix("/v1/api/{project}/auth/{dbType}").Subrouter()
30-
userRouter.Methods("POST").Path("/email/signin").HandlerFunc(handlers.HandleEmailSignIn(s.user))
31-
userRouter.Methods("POST").Path("/email/signup").HandlerFunc(handlers.HandleEmailSignUp(s.user))
32-
userRouter.Methods("GET").Path("/profile/{id}").HandlerFunc(handlers.HandleProfile(s.user))
33-
userRouter.Methods("GET").Path("/profiles").HandlerFunc(handlers.HandleProfiles(s.user))
34-
userRouter.Methods("GET").Path("/edit_profile/{id}").HandlerFunc(handlers.HandleEmailEditProfile(s.user))
29+
userRouter.Methods("POST").Path("/email/signin").HandlerFunc(handlers.HandleEmailSignIn(s.projects))
30+
userRouter.Methods("POST").Path("/email/signup").HandlerFunc(handlers.HandleEmailSignUp(s.projects))
31+
userRouter.Methods("GET").Path("/profile/{id}").HandlerFunc(handlers.HandleProfile(s.projects))
32+
userRouter.Methods("GET").Path("/profiles").HandlerFunc(handlers.HandleProfiles(s.projects))
33+
userRouter.Methods("GET").Path("/edit_profile/{id}").HandlerFunc(handlers.HandleEmailEditProfile(s.projects))
3534

3635
// Initialize the routes for the file management operations
37-
s.router.Methods("POST").Path("/v1/api/{project}/files").HandlerFunc(handlers.HandleCreateFile(s.auth, s.file))
38-
s.router.Methods("GET").PathPrefix("/v1/api/{project}/files").HandlerFunc(handlers.HandleRead(s.auth, s.file))
39-
s.router.Methods("DELETE").PathPrefix("/v1/api/{project}/files").HandlerFunc(handlers.HandleDelete(s.auth, s.file))
36+
s.router.Methods("POST").Path("/v1/api/{project}/files").HandlerFunc(handlers.HandleCreateFile(s.projects))
37+
s.router.Methods("GET").PathPrefix("/v1/api/{project}/files").HandlerFunc(handlers.HandleRead(s.projects))
38+
s.router.Methods("DELETE").PathPrefix("/v1/api/{project}/files").HandlerFunc(handlers.HandleDelete(s.projects))
4039

4140
// Initialize the route for handling static files
4241
s.router.PathPrefix("/").HandlerFunc(handlers.HandleStaticRequest(s.static))

0 commit comments

Comments
 (0)