Skip to content

Commit 95b464b

Browse files
committed
feat(v1.3.0): add BBolt support and refactor database layer
- Add BBolt implementations for cache, KV, and queue systems - Introduce factory pattern for cache, KV, and queue initialization - Add memory-based queue implementation for lightweight deployments - Refactor database drivers with improved SQL support - Update authentication and project controllers - Enhance GraphQL subscription handling - Remove deprecated executor Makefiles and docker resolver - Update dependencies and configuration models - Add BBolt migration documentation - Improve path utility functions
1 parent a7c6b0b commit 95b464b

42 files changed

Lines changed: 1891 additions & 737 deletions

Some content is hidden

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

const/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323

2424
// EmbeddedDB Embedded Database
2525
CoreDB string = "coreDB"
26+
BoltDB string = "bbolt"
2627

2728
// RedisDriver KeyValue database
2829
RedisDriver string = "redis"

controller/auth_controller.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"strconv"
78
"time"
89

910
"github.com/apito-io/engine/database/project/driver/mongo"
@@ -21,7 +22,6 @@ import (
2122
"golang.org/x/oauth2/google"
2223
)
2324

24-
2525
type authCtrl struct {
2626
Cfg *models.Config
2727
graphQLServer *resolver.GraphQLServer
@@ -150,15 +150,16 @@ func (a *authCtrl) Journey(c echo.Context) error {
150150
}
151151

152152
type DatabaseRequest struct {
153+
File string `json:"file"`
153154
Type string `json:"type"`
154155
Host string `json:"host"`
155-
Port string `json:"port"`
156+
Port int `json:"port"`
156157
Database string `json:"database"`
157158
Username string `json:"username"`
158159
Password string `json:"password"`
159160
}
160161

161-
func (a *authCtrl) DatabaseTest(c echo.Context) error {
162+
func (a *authCtrl) DatabaseCheck(c echo.Context) error {
162163

163164
var req DatabaseRequest
164165
if err := c.Bind(&req); err != nil {
@@ -173,7 +174,7 @@ func (a *authCtrl) DatabaseTest(c echo.Context) error {
173174
driver, err := mongo.GetMongoDriver(&models.DriverCredentials{
174175
Engine: req.Type,
175176
Host: req.Host,
176-
Port: req.Port,
177+
Port: strconv.Itoa(req.Port),
177178
Database: req.Database,
178179
User: req.Username,
179180
Password: req.Password,
@@ -198,9 +199,10 @@ func (a *authCtrl) DatabaseTest(c echo.Context) error {
198199

199200
case "postgresql", "mysql", "sqlite", "sqlServer", "mariadb":
200201
driver, err := sql.GetSQLDriver(&models.DriverCredentials{
202+
File: req.File,
201203
Engine: req.Type,
202204
Host: req.Host,
203-
Port: req.Port,
205+
Port: strconv.Itoa(req.Port),
204206
Database: req.Database,
205207
User: req.Username,
206208
Password: req.Password,
@@ -246,12 +248,11 @@ func (a *authCtrl) errorHandler(router echo.Context, response *models.HttpRespon
246248
router.JSON(int(response.Code), response)
247249
}
248250

249-
250251
func (a *authCtrl) ProjectSwitchV2(c echo.Context) error {
251252
var req *models.ProjectCreateRequest
252253
if err := c.Bind(&req); err != nil {
253254
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
254-
Message: "Bad boy, Jason ...",
255+
Message: err.Error(),
255256
Code: http.StatusBadRequest,
256257
})
257258
}
@@ -281,6 +282,13 @@ func (a *authCtrl) ProjectSwitchV2(c echo.Context) error {
281282
})
282283
}
283284

285+
if resp.User == nil {
286+
return c.JSON(http.StatusForbidden, &models.HttpResponse{
287+
Message: "user is missing in the project with roles",
288+
Code: http.StatusForbidden,
289+
})
290+
}
291+
284292
// project switched so empty the cache
285293
//a.gqlServer.Param.ProjectId = project.ID
286294

@@ -332,7 +340,6 @@ func (a *authCtrl) ProjectSwitchV2(c echo.Context) error {
332340
//time.Sleep(time.Second * 2)
333341

334342
_project.Schema = nil
335-
_project.MicroServicePort = ""
336343

337344
return c.JSON(http.StatusOK, &models.HttpResponse{
338345
Message: "Project Switched",

controller/project_controller.go

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"os"
99
"strings"
1010
"time"
11-
12-
_const "github.com/apito-io/engine/const"
1311
"github.com/apito-io/engine/models"
1412
"github.com/apito-io/engine/utility"
1513
"github.com/google/uuid"
@@ -70,13 +68,6 @@ func (a *authCtrl) ProjectCreation(c echo.Context) error {
7068
createdProject++
7169
}
7270

73-
if createdProject >= int32(user.ProjectLimit) {
74-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
75-
Message: "You can not create more projects. Please upgrade your plan",
76-
Code: http.StatusBadRequest,
77-
})
78-
}
79-
8071
// prepare the project skeleton
8172
var _projectID string
8273
if val, ok := req["id"]; ok && val != nil {
@@ -113,62 +104,53 @@ func (a *authCtrl) ProjectCreation(c echo.Context) error {
113104
Teams: []*models.Team{user.DefaultTeam},
114105
}
115106

116-
var planType string
117-
if val, ok := req["plan_type"]; ok && val != nil {
118-
planType = val.(string)
107+
// optional
108+
if val, ok := req["description"]; ok && val != nil {
109+
project.Description = val.(string)
110+
}
111+
112+
var databaseType string
113+
if val, ok := req["database_type"]; ok && val != nil {
114+
databaseType = val.(string)
119115
} else {
120116
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
121-
Message: "Plan type is required",
117+
Message: "Database type is required",
122118
Code: http.StatusBadRequest,
123119
})
124120
}
125121

126-
switch planType {
127-
case "dedicated":
128-
129-
project.PlanType = "dedicated"
130-
if val, ok := req["tier_name"]; ok && val != nil {
131-
project.TireType = val.(string)
132-
} else {
133-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
134-
Message: "Tier name is required",
135-
Code: http.StatusBadRequest,
136-
})
137-
}
138-
139-
if project.TireType == "free" && createdProject >= 1 {
140-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
141-
Message: "You can not create more than 1 project with free tier",
142-
Code: http.StatusBadRequest,
143-
})
144-
}
145-
case "byod":
146-
var databaseType string
147-
if val, ok := req["database_type"]; ok && val != nil {
148-
databaseType = val.(string)
149-
} else {
150-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
151-
Message: "Database type is required",
152-
Code: http.StatusBadRequest,
153-
})
154-
}
155-
project.PlanType = "byod"
156-
project.Driver = &models.DriverCredentials{
157-
Engine: databaseType,
158-
}
122+
var dbConfig map[string]interface{}
123+
if val, ok := req["db_config"]; ok && val != nil && len(val.(map[string]interface{})) > 0 {
124+
dbConfig = val.(map[string]interface{})
125+
} else {
126+
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
127+
Message: "Database configuration is required",
128+
Code: http.StatusBadRequest,
129+
})
159130
}
160131

161-
if val, ok := req["description"]; ok && val != nil {
162-
project.Description = val.(string)
132+
project.Driver = &models.DriverCredentials{
133+
Engine: databaseType,
163134
}
164135

165-
/* ust := c.Get("ust")
166-
if ust == nil {
167-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
168-
Message: "user is missing in the token payload",
169-
Code: http.StatusBadRequest,
170-
})
171-
} */
136+
if val, ok := dbConfig["host"]; ok && val != nil {
137+
project.Driver.Host = val.(string)
138+
}
139+
if val, ok := dbConfig["port"]; ok && val != nil {
140+
project.Driver.Port = val.(string)
141+
}
142+
if val, ok := dbConfig["user"]; ok && val != nil {
143+
project.Driver.User = val.(string)
144+
}
145+
if val, ok := dbConfig["password"]; ok && val != nil {
146+
project.Driver.Password = val.(string)
147+
}
148+
if val, ok := dbConfig["database"]; ok && val != nil {
149+
project.Driver.Database = val.(string)
150+
}
151+
if val, ok := dbConfig["file"]; ok && val != nil {
152+
project.Driver.File = val.(string)
153+
}
172154

173155
switch strings.ToLower(req["project_type"].(string)) {
174156
case "regular", "general":
@@ -222,23 +204,21 @@ func (a *authCtrl) ProjectCreation(c echo.Context) error {
222204
}
223205

224206
if project.Driver == nil {
225-
cfg := &models.DriverCredentials{
226-
ProjectID: projectID, // this is must be passed from here
227-
Engine: a.Cfg.DefaultProjectDatabaseEngine,
228-
Host: a.Cfg.DefaultProjectDBHost,
229-
Port: a.Cfg.DefaultProjectDBPort,
230-
User: a.Cfg.DefaultProjectDBUser,
231-
Database: a.Cfg.DefaultProjectDBName,
232-
Password: a.Cfg.DefaultProjectDBPassword,
233-
}
234-
// only save the engine
235207
project.Driver = &models.DriverCredentials{
236-
Engine: _const.ApitoDB,
208+
ProjectID: projectID, // this is must be passed from here
209+
Engine: a.Cfg.DefaultProjectDatabaseEngine,
210+
Host: a.Cfg.DefaultProjectDBHost,
211+
Port: a.Cfg.DefaultProjectDBPort,
212+
User: a.Cfg.DefaultProjectDBUser,
213+
Database: a.Cfg.DefaultProjectDBName,
214+
Password: a.Cfg.DefaultProjectDBPassword,
237215
}
238-
// inject driver credential to connection manager
239-
a.graphQLServer.GraphQLExecutor.SetProjectDriverCredential(ctx, cfg)
240216
}
241217

218+
// inject driver credential to connection manager
219+
project.Driver.ProjectID = projectID // this is a must for connection manager
220+
a.graphQLServer.GraphQLExecutor.SetProjectDriverCredential(ctx, project.Driver)
221+
242222
if project.ProjectType != models.ProjectType_SaaS {
243223
//inject project_id via context
244224
ctx = context.WithValue(ctx, "project_id", projectID)

controller/token_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (a *authCtrl) SyncProject(c echo.Context) error {
179179
var req SyncProjectRequest
180180
if err := c.Bind(&req); err != nil {
181181
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
182-
Message: "Bad boy, Jason ...",
182+
Message: err.Error(),
183183
Code: http.StatusBadRequest,
184184
})
185185
}

0 commit comments

Comments
 (0)