-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4149f45
commit e8e6051
Showing
11 changed files
with
342 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/limanmys/render-engine/app/models" | ||
"github.com/limanmys/render-engine/internal/database" | ||
"github.com/limanmys/render-engine/internal/liman" | ||
"github.com/limanmys/render-engine/internal/process_queue" | ||
"github.com/limanmys/render-engine/pkg/logger" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type QueueHandler struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewQueueHandler() *QueueHandler { | ||
return &QueueHandler{ | ||
db: database.Connection(), | ||
} | ||
} | ||
|
||
func (h *QueueHandler) Create(c *fiber.Ctx) error { | ||
queue := &models.Queue{} | ||
if err := c.BodyParser(&queue); err != nil { | ||
return err | ||
} | ||
|
||
if queue.Data["server_id"] == nil { | ||
return c.Status(422).JSON(fiber.Map{ | ||
"server_id": "server id is required", | ||
}) | ||
} | ||
|
||
// Check if user is eligible to use this server | ||
user, _ := liman.GetUser(&models.User{ID: c.Locals("user_id").(string)}) | ||
if user.Status == 0 { | ||
var count int64 | ||
h.db.Model(&models.Permission{}).Find(&models.Permission{ | ||
MorphID: c.Locals("user_id").(string), | ||
Type: "server", | ||
Value: queue.Data["server_id"].(string), | ||
}).Count(&count) | ||
|
||
if count < 1 { | ||
return logger.FiberError(fiber.StatusForbidden, "you are not allowed to use this server") | ||
} | ||
} | ||
|
||
// Create queue object | ||
if err := h.db.Create(queue).Error; err != nil { | ||
return err | ||
} | ||
|
||
// Start processing by type | ||
var processor process_queue.ProcessQueue | ||
switch queue.Type { | ||
case models.OperationInstall: | ||
processor = &process_queue.InstallPackage{ | ||
Queue: queue, | ||
DB: h.db, | ||
UserID: c.Locals("user_id").(string), | ||
} | ||
go processor.Process() | ||
} | ||
|
||
return c.JSON(queue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
gormjsonb "github.com/dariubs/gorm-jsonb" | ||
"github.com/google/uuid" | ||
"github.com/limanmys/render-engine/internal/database" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Operation string | ||
type Status string | ||
|
||
const ( | ||
OperationCreate Operation = "create" | ||
OperationUpdate Operation = "update" | ||
OperationInstall Operation = "install" | ||
|
||
StatusPending Status = "pending" | ||
StatusProcessing Status = "processing" | ||
StatusDone Status = "done" | ||
StatusFailed Status = "failed" | ||
) | ||
|
||
// Queue structure of Queue object | ||
type Queue struct { | ||
ID string `json:"id"` | ||
Type Operation `json:"type"` | ||
Status Status `json:"status"` | ||
Data gormjsonb.JSONB `json:"data" gorm:"type:jsonb;index,type:gin"` | ||
Error string `json:"error"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} | ||
|
||
func (Queue) TableName() string { | ||
return "queue" | ||
} | ||
|
||
// Fill ID of Queue object with UUID beforeCreate | ||
func (q *Queue) BeforeCreate(tx *gorm.DB) (err error) { | ||
q.ID = uuid.New().String() | ||
q.Status = StatusPending | ||
q.CreatedAt = time.Now() | ||
q.UpdatedAt = time.Now() | ||
return | ||
} | ||
|
||
func (q *Queue) BeforeUpdate(tx *gorm.DB) (err error) { | ||
q.UpdatedAt = time.Now() | ||
return | ||
} | ||
|
||
func (q *Queue) UpdateStatus(status Status) { | ||
q.Status = status | ||
database.Connection().Model(q).Save(q) | ||
} | ||
|
||
func (q *Queue) UpdateError(err string) { | ||
q.Error = err | ||
q.Status = StatusFailed | ||
database.Connection().Model(q).Save(q) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.