package process_queue
import (
b64 "encoding/base64"
"encoding/json"
"strings"
"github.com/google/uuid"
"github.com/limanmys/render-engine/app/models"
"github.com/limanmys/render-engine/internal/liman"
"github.com/limanmys/render-engine/internal/sandbox"
"github.com/limanmys/render-engine/internal/user_token"
"github.com/limanmys/render-engine/pkg/helpers"
"github.com/limanmys/render-engine/pkg/linux"
"gorm.io/gorm"
)
type CreateReport struct {
Queue *models .Queue
DB *gorm .DB
}
func (c CreateReport ) Process () error {
c .Queue .UpdateStatus (models .StatusProcessing )
extension , err := liman .GetExtension (&models .Extension {
ID : c .Queue .Data ["extension_id" ].(string ),
})
if err != nil {
c .Queue .UpdateError (err .Error())
return err
}
if extension .Status == "0" {
c .Queue .UpdateError ("extension is unavailable" )
return err
}
credentials := &models .Credentials {}
if extension .RequireKey == "true" {
credentials , err = liman .GetCredentials (
&models .User {
ID : c .Queue .Data ["user_id" ].(string ),
},
&models .Server {
ID : c .Queue .Data ["server_id" ].(string ),
},
)
if err != nil || len (credentials .Username ) < 1 {
c .Queue .UpdateError ("you need a key to use this extension" )
return err
}
}
formValues := make (map [string ]string )
formValues ["data" ] = b64 .StdEncoding .EncodeToString ([]byte (c .Queue .Data ["payload" ].(string )))
token , err := user_token .Create (c .Queue .Data ["user_id" ].(string ))
if err != nil {
c .Queue .UpdateError (err .Error())
return err
}
log_id := uuid .New ()
command , err := sandbox .GenerateCommand (
extension ,
credentials ,
&models .CommandParams {
TargetFunction : c .Queue .Data ["target" ].(string ),
Locale : helpers .Env ("APP_LANG" , "tr" ),
Extension : c .Queue .Data ["extension_id" ].(string ),
Server : c .Queue .Data ["server_id" ].(string ),
User : c .Queue .Data ["user_id" ].(string ),
LogID : log_id .String (),
RequestData : formValues ,
BaseURL : "https://127.0.0.1" ,
Token : token ,
},
)
if err != nil {
c .Queue .UpdateError (err .Error())
return err
}
type limanResponse struct {
Message string `json:"message"`
Status int `json:"status"`
}
var response limanResponse
output := linux .Execute (command )
if err := json .Unmarshal ([]byte (output ), &response ); err != nil {
c .Queue .UpdateError ("error when unmarshalling json, output: " + output )
return err
}
if response .Status != 200 {
c .Queue .UpdateError (response .Message )
} else {
c .Queue .UpdateAsDone (strings .TrimSpace (strings .ReplaceAll (response .Message , "\"" , "" )))
}
return nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .