Skip to content

Commit b2198a2

Browse files
author
Jayesh Choudhary
authored
Merge pull request #236 from spaceuptech/v0.9.0
V0.9.0
2 parents 26d01a0 + d9d4c19 commit b2198a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+9013
-3222
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ css
2222
space-cloud
2323
publish.sh
2424

25+
test_config.yaml
26+
2527
#IDEs
26-
.idea
28+
.idea
29+
.vscode

config/config.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package config
22

3+
import "github.com/spaceuptech/space-cloud/utils"
4+
35
// Config holds the entire configuration
46
type Config struct {
57
Projects map[string]*Project `json:"projects" yaml:"projects"` // The key here is the project id
@@ -56,6 +58,8 @@ type Rule struct {
5658
DB string `json:"db" yaml:"db"`
5759
Col string `json:"col" yaml:"col"`
5860
Find map[string]interface{} `json:"find" yaml:"find"`
61+
Service string `json:"service" yaml:"service"`
62+
Func string `json:"func" yaml:"func"`
5963
}
6064

6165
// Auth holds the mapping of the sign in method
@@ -68,17 +72,22 @@ type AuthStub struct {
6872
Secret string `json:"secret" yaml:"secret"`
6973
}
7074

71-
// Functions holds the config for the Functions module
75+
// Functions holds the config for the functions module
7276
type Functions struct {
73-
Enabled bool `json:"enabled" yaml:"enabled"`
74-
Nats string `json:"nats" yaml:"nats"`
77+
Enabled bool `json:"enabled" yaml:"enabled"`
78+
Broker utils.Broker `json:"broker" yaml:"broker"`
79+
Conn string `json:"conn" yaml:"conn"`
80+
Rules FuncRules `json:"rules" yaml:"rules"`
7581
}
7682

83+
// FuncRules is the rules for the functions module
84+
type FuncRules map[string]map[string]*Rule // service -> function -> rule
85+
7786
// Realtime holds the config for the realtime module
7887
type Realtime struct {
79-
Enabled bool `json:"enabled" yaml:"enabled"`
80-
Broker string `json:"broker" yaml:"broker"`
81-
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"`
8291
}
8392

8493
// FileStore holds the config for the file store module
@@ -97,8 +106,14 @@ type FileRule struct {
97106

98107
// Static holds the config for the static files module
99108
type Static struct {
100-
Enabled bool `json:"enabled" yaml:"enabled"`
109+
Enabled bool `json:"enabled" yaml:"enabled"`
110+
Routes []*StaticRoute `json:"routes" yaml:"routes"`
111+
}
112+
113+
// StaticRoute holds the config for each route
114+
type StaticRoute struct {
101115
Path string `json:"path" yaml:"path"`
102116
URLPrefix string `json:"prefix" yaml:"prefix"`
103-
Gzip bool `json:"gzip" yaml:"gzip"`
117+
Host string `json:"host" yaml:"host"`
118+
Proxy string `json:"proxy" yaml:"proxy"`
104119
}

config/load.go

+24
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@ package config
33
import (
44
"encoding/json"
55
"io/ioutil"
6+
"os"
67
"strings"
78

89
"gopkg.in/yaml.v2"
910
)
1011

12+
func loadEnvironmentVariable(p *Project) {
13+
if strings.HasPrefix(p.Secret, "$") {
14+
tempString := strings.TrimPrefix(p.Secret, "$")
15+
tempEnvVar, present := os.LookupEnv(tempString)
16+
17+
if present {
18+
p.Secret = tempEnvVar
19+
}
20+
}
21+
22+
for _, value := range p.Modules.Crud {
23+
if strings.HasPrefix(value.Conn, "$") {
24+
tempStringC := strings.TrimPrefix(value.Conn, "$")
25+
tempEnvVarC, presentC := os.LookupEnv(tempStringC)
26+
27+
if presentC {
28+
value.Conn = tempEnvVarC
29+
}
30+
}
31+
}
32+
}
33+
1134
// LoadConfigFromFile loads the config from the provided file path
1235
func LoadConfigFromFile(path string) (*Project, error) {
1336
// Load the file in memory
@@ -27,5 +50,6 @@ func LoadConfigFromFile(path string) (*Project, error) {
2750
return nil, err
2851
}
2952

53+
loadEnvironmentVariable(conf)
3054
return conf, nil
3155
}

config/template.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ modules:
2525
enabled: false
2626
functions:
2727
enabled: false
28-
nats: nats://localhost:4222
28+
broker: nats
29+
conn: nats://localhost:4222
30+
rules:
31+
service1:
32+
function1:
33+
rule: allow
2934
realtime:
3035
enabled: false
31-
kafka: localhost
36+
broker: nats
37+
conn: nats://localhost:4222
3238
fileStore:
3339
enabled: false
3440
storeType: local
@@ -45,7 +51,7 @@ modules:
4551
rule: allow
4652
static:
4753
enabled: false
48-
path: ./public
49-
prefix: /
50-
gzip: false
54+
routes:
55+
- prefix: /
56+
path: ./public
5157
`

docs/config.json

+23-16
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
"read",
6666
"Read Data"
6767
],
68+
[
69+
"live-query",
70+
"Listening to real-time updates"
71+
],
6872
[
6973
"update",
7074
"Update Data"
@@ -73,22 +77,16 @@
7377
"delete",
7478
"Delete Data"
7579
],
80+
[
81+
"transactions",
82+
"Transactions and batched mutations"
83+
],
7684
[
7785
"config",
7886
"Configuration"
7987
]
8088
]
8189
},
82-
{
83-
"name": "Realtime",
84-
"url": "realtime",
85-
"pages": [
86-
[
87-
"live-query",
88-
"Listening to real-time updates"
89-
]
90-
]
91-
},
9290
{
9391
"name": "File Storage",
9492
"url": "file-storage",
@@ -116,16 +114,16 @@
116114
]
117115
},
118116
{
119-
"name": "Functions",
117+
"name": "Functions Mesh",
120118
"url": "functions",
121119
"pages": [
122120
[
123-
"engine",
124-
"Writing custom logic"
121+
"service",
122+
"Write functions"
125123
],
126124
[
127125
"client",
128-
"Accessing custom logic"
126+
"Call functions"
129127
]
130128
]
131129
},
@@ -135,14 +133,23 @@
135133
"pages": [
136134
[
137135
"database",
138-
"Database"
136+
"Securing data"
139137
],
140138
[
141139
"file-storage",
142-
"File Storage"
140+
"Securing files"
141+
],
142+
[
143+
"functions",
144+
"Securing functions"
143145
]
144146
]
145147
},
148+
{
149+
"name": "Static hosting",
150+
"url": "static",
151+
"pages": []
152+
},
146153
{
147154
"name": "Deploy",
148155
"url": "deploy",

docs/manual/database/config.md

+37-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ modules:
77
crud:
88
mongo:
99
conn: mongodb://localhost:27017
10-
isPrimary: true
1110
collections:
1211
todos:
1312
isRealtimeEnabled: false
@@ -20,23 +19,48 @@ modules:
2019
rule: allow
2120
delete:
2221
rule: allow
22+
sql-mysql:
23+
conn: user:my-secret-pwd@/project
24+
collections:
25+
users:
26+
isRealtimeEnabled: false
27+
rules:
28+
create:
29+
rule: allow
30+
read:
31+
rule: allow
32+
update:
33+
rule: allow
34+
delete:
35+
rule: allow
2336

2437
# Config for other modules go here
2538
```
2639

27-
As you can see `crud`, in this case has the key `mongo` which stands for the MongoDB database. You can have multiple databases in a single project by simply adding the config of each database under `crud`. The keys for the databases we currently support are `mongo` (for MongoDB), `sql-postgres` (for Postgres) and `sql-mysql` (for MySQL).
40+
The `crud` module in config specifies which databases to connect to and which collections to expose in that database along with the respective security rules. As you can see, you can have multiple databases in a single project by simply adding the config of each database under `crud`. The keys for the databases we currently support are `mongo` (for MongoDB), `sql-postgres` (for Postgres and Postgres compatible databases) and `sql-mysql` (for MySQL and MySQL compatible databases).
41+
42+
# Enable the realtime module
43+
44+
Here's a snippet showing how to **enable the realtime module**. This 4 line snippet will set up all the necessary routines required by the realtime module.
45+
46+
```yaml
47+
modules:
48+
realtime:
49+
enabled: true # Enable the realtime module globally
50+
broker: nats # Broker to be used as pub sub for realtime module
51+
conn: nats://localhost:4222 # Connection string of broker
52+
```
2853
29-
Here's a snippet configuring space cloud to use MongoDB and MySQL. MongoDB will hold the `todos` collection while MySQL has the `users` table.
54+
The realtime feature also needs to be enabled on a collection level for the collections that you want to sync in realtime. Here's a snippet configuring space cloud to use MongoDB and MySQL. MongoDB will hold the `todos` collection which will be synced in realtime while MySQL has the `users` table (not synced in realtime).
3055

3156
```yaml
3257
modules:
3358
crud:
3459
mongo:
3560
conn: mongodb://localhost:27017
36-
isPrimary: false
3761
collections:
3862
todos:
39-
isRealtimeEnabled: false
63+
isRealtimeEnabled: true
4064
rules:
4165
create:
4266
rule: allow
@@ -48,7 +72,6 @@ modules:
4872
rule: allow
4973
sql-mysql:
5074
conn: user:my-secret-pwd@/project
51-
isPrimary: true
5275
collections:
5376
users:
5477
isRealtimeEnabled: false
@@ -61,16 +84,17 @@ modules:
6184
rule: allow
6285
delete:
6386
rule: allow
64-
65-
# Config for other modules go here
87+
realtime:
88+
enabled: true
89+
broker: nats
90+
conn: nats://localhost:4222
6691
```
6792

6893
For each database, you need to specify the following fields:
6994
- **conn:** This is the connection string to connect to the database with.
70-
- **isPrimary:** Specifies if the database is to be used as the primary database. Note, you **cannot have more than one primary database**.
7195
- **collections:** These are the table / collections which need to be exposed via Space Cloud. They contain two sub fields `isRealtimeEnabled` and `rules`. `rules` are nothing but the [security rules](/docs/security/database), to control the database access.
7296

73-
The snippet shown above configures Space Cloud to use `MongoDB` as the primary database present at `mongodb://localhost:27017`. It exposes a single collection `todos`. All types of operations (create, read, update and delete) are allowed on the `todos` collection. This implies that, any anonymous user will be able to perform any operations on the database. To expose more tables / collections, simply add new objects under the `collections` key.
97+
The snippet shown above configures Space Cloud to use `MongoDB` present at `mongodb://localhost:27017`. It exposes a single collection `todos`. All types of operations (create, read, update and delete) are allowed on the `todos` collection. This implies that, any anonymous user will be able to perform any operations on the database. To expose more tables / collections, simply add new objects under the `collections` key.
7498

7599
Here's an example that has two collections `todos` and `users`. Note, updating and deleting users is denied.
76100

@@ -111,13 +135,13 @@ modules:
111135

112136
## Next steps
113137

114-
Now you know the basics of the database module. The next step would be checking out the realtime module to bring realtime updates to your app!
138+
Now you know the basics of the database module. The next step would be checking out the file storage module to bring files to your app!
115139

116140
<div class="btns-wrapper">
117-
<a href="/docs/database/delete" class="waves-effect waves-light btn primary-btn-border btn-small">
141+
<a href="/docs/database/transactions" class="waves-effect waves-light btn primary-btn-border btn-small">
118142
<i class="material-icons btn-with-icon">arrow_back</i>Previous
119143
</a>
120-
<a href="/docs/realtime/overview" class="waves-effect waves-light btn primary-btn-fill btn-small">
144+
<a href="/docs/file-storage/overview" class="waves-effect waves-light btn primary-btn-fill btn-small">
121145
Next<i class="material-icons btn-with-icon">arrow_forward</i>
122146
</a>
123147
</div>

0 commit comments

Comments
 (0)