Skip to content

Commit

Permalink
feature: Added new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Aug 31, 2022
1 parent 9838936 commit a135435
Show file tree
Hide file tree
Showing 27 changed files with 370 additions and 29 deletions.
43 changes: 43 additions & 0 deletions app/handlers/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handlers

import (
"github.com/gofiber/fiber/v2"
"github.com/limanmys/render-engine/internal/liman"
)

func ExtensionRunner(c *fiber.Ctx) error {
/*if len(c.FormValue("extension_id")) < 1 {
return fiber.NewError(fiber.StatusNotFound, "Extension not found")
}
extension, err := liman.GetExtension(&models.Extension{
ID: c.FormValue("extension_id"),
})
if err != nil {
return err
}
if extension.Status == "0" {
return fiber.NewError(fiber.StatusServiceUnavailable, "Extension is unavailable right now, please try again later.")
}
if extension.RequireKey == "true" {
credentials, err := liman.GetCredentials(
&models.User{
ID: c.Locals("user_id").(string),
},
&models.Server{
ID: c.FormValue("server_id"),
},
)
if err != nil || len(credentials.Username) < 1 {
return fiber.NewError(fiber.StatusForbidden, "You need a key to use this extension, please add it through the case.")
}
} */

settings := liman.GetSettings(c.Locals("user_id").(string), c.FormValue("server_id"), c.FormValue("extension_id"))

return c.JSON(settings)
}
8 changes: 4 additions & 4 deletions app/controllers/test.go → app/handlers/test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package controllers
package handlers

import (
"github.com/gofiber/fiber/v2"
"github.com/limanmys/render-engine/app/models"
"github.com/limanmys/render-engine/internal/bridge"
"github.com/limanmys/render-engine/internal/liman"
)

func CredentialTest(c *fiber.Ctx) error {

credentials, err := bridge.GetCredentials(
credentials, err := liman.GetCredentials(
&models.User{
ID: c.Params("user"),
ID: c.Locals("user_id").(string),
},
&models.Server{
ID: c.Params("server"),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions app/models/php_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

type CommandParams struct {
TargetFunction string
User string
Extension string
Server string
RequestData map[string]string
Token string
BaseURL string
}
File renamed without changes.
2 changes: 1 addition & 1 deletion app/models/ServerKey.go → app/models/server_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ServerKey) TableName() string {
return "server_keys"
}

func (d KeyData) DecryptKey(user *User, server *Server) *Credentials {
func (d KeyData) DecryptData(user *User, server *Server) *Credentials {
key := os.Getenv("APP_KEY") + user.ID + server.ID

return &Credentials{
Expand Down
15 changes: 15 additions & 0 deletions app/models/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

type Settings struct {
ID string `json:"id"`
ServerID string `json:"server_id"`
UserID string `json:"user_id"`
Name string `json:"name"`
Value string `json:"value"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}

func (Settings) TableName() string {
return "user_settings"
}
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions app/routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package routes

import (
"github.com/gofiber/fiber/v2"
"github.com/limanmys/render-engine/app/controllers"
"github.com/limanmys/render-engine/app/handlers"
)

func Install(app *fiber.App) {
app.Get("/credentials/:user/:server", controllers.CredentialTest)
app.Get("/credentials/:server", handlers.CredentialTest)
app.Post("/extensionRunner", handlers.ExtensionRunner)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/limanmys/render-engine
go 1.18

require (
github.com/gofiber/fiber/v2 v2.36.0
github.com/gofiber/fiber/v2 v2.37.0
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.4.0
gorm.io/driver/mysql v1.3.6
Expand All @@ -29,7 +29,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.39.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/text v0.3.7 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/gofiber/fiber/v2 v2.31.0 h1:M2rWPQbD5fDVAjcoOLjKRXTIlHesI5Eq7I5FEQPt4
github.com/gofiber/fiber/v2 v2.31.0/go.mod h1:1Ega6O199a3Y7yDGuM9FyXDPYQfv+7/y48wl6WCwUF4=
github.com/gofiber/fiber/v2 v2.36.0 h1:1qLMe5rhXFLPa2SjK10Wz7WFgLwYi4TYg7XrjztJHqA=
github.com/gofiber/fiber/v2 v2.36.0/go.mod h1:tgCr+lierLwLoVHHO/jn3Niannv34WRkQETU8wiL9fQ=
github.com/gofiber/fiber/v2 v2.37.0 h1:KVboSQ7e0wDbSFXNjXKqoigwp9HYUqgWn4uGFaUO1P8=
github.com/gofiber/fiber/v2 v2.37.0/go.mod h1:xm3pDGlfE1xqVKb77iH8weLU0FFoTeWeK3nbiYM2Nh0=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
Expand Down Expand Up @@ -190,6 +192,8 @@ golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
Expand Down Expand Up @@ -220,6 +224,8 @@ golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f h1:rlezHXNlxYWvBCzNses9Dlc7n
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM=
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
2 changes: 1 addition & 1 deletion internal/database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Connection() *gorm.DB {
connection = initialize()
})

return connection
return connection.Debug()
}

func initialize() *gorm.DB {
Expand Down
11 changes: 10 additions & 1 deletion internal/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"fmt"
"log"
"os"

"gorm.io/driver/postgres"
Expand All @@ -21,7 +22,15 @@ func initializePostgres() *gorm.DB {
connection, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

if err != nil {
return nil
log.Fatalln("Cannot connect to Liman database!")
}

db, err := connection.DB()

err = db.Ping()
if err != nil {
log.Fatalln("Cannot connect to Liman database!")
}

return connection
}
32 changes: 32 additions & 0 deletions internal/liman/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package liman

import (
"errors"

"github.com/limanmys/render-engine/app/models"
"github.com/limanmys/render-engine/internal/database"
)

func AuthWithToken(token string) (string, error) {
tokenObj := &models.Token{}

err := database.Connection().First(&tokenObj, "token = ?", token).Error

if err != nil || len(tokenObj.UserID) < 1 {
return "", errors.New("Authorization token is not valid.")
}

return tokenObj.UserID, nil
}

func AuthWithAccessToken(token string) (string, error) {
tokenObj := &models.AccessToken{}

err := database.Connection().First(&tokenObj, "token = ?", token).Error

if err != nil || len(tokenObj.UserID) < 1 {
return "", errors.New("Authorization token is not valid.")
}

return tokenObj.UserID, nil
}
16 changes: 13 additions & 3 deletions internal/bridge/liman.go → internal/liman/credentials.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package bridge
package liman

import (
"encoding/json"

"github.com/gofiber/fiber/v2"
"github.com/limanmys/render-engine/app/models"
"github.com/limanmys/render-engine/internal/database"
)
Expand All @@ -14,6 +15,7 @@ func GetCredentials(user *models.User, server *models.Server) (*models.Credentia

encryptedKey := &models.KeyData{}
encrypterUser := user.ID

if serverKey.Data == "" {
database.Connection().First(&server, "id = ?", server.ID)

Expand All @@ -23,9 +25,17 @@ func GetCredentials(user *models.User, server *models.Server) (*models.Credentia
}
}

json.Unmarshal([]byte(serverKey.Data), encryptedKey)
credentials := encryptedKey.DecryptKey(&models.User{ID: encrypterUser}, server)
json.Unmarshal(
[]byte(serverKey.Data),
encryptedKey,
)

credentials := encryptedKey.DecryptData(&models.User{ID: encrypterUser}, server)
credentials.Type = serverKey.Type

if len(credentials.Username) < 1 {
return nil, fiber.NewError(fiber.StatusNotFound, "Server not found")
}

return credentials, nil
}
51 changes: 51 additions & 0 deletions internal/liman/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package liman

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/limanmys/render-engine/app/models"
"github.com/limanmys/render-engine/internal/database"
)

func GetExtension(extension *models.Extension) (*models.Extension, error) {
result := database.Connection().First(&extension)

if result.Error != nil {
return nil, fiber.NewError(fiber.StatusNotFound, "Cannot found extension with this id")
}

if result.RowsAffected > 0 {
return extension, nil
}

return nil, fiber.NewError(fiber.StatusNotFound, "Cannot found extension with this id")
}

func GetExtensionJSON(extension *models.Extension) (map[string]any, error) {
fileName := ""
if len(extension.Name) > 0 {
fileName = strings.ToLower(extension.Name)
} else {
extObject, err := GetExtension(extension)
fileName = strings.ToLower(extObject.Name)

if err != nil {
return nil, err
}
}

jsonFile, err := ioutil.ReadFile(fmt.Sprintf("/liman/extensions/%s/db.json", fileName))

if err != nil {
return nil, err
}

extJson := make(map[string]any)
json.Unmarshal(jsonFile, &extJson)

return extJson, nil
}
21 changes: 21 additions & 0 deletions internal/liman/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package liman

import (
"github.com/gofiber/fiber/v2"
"github.com/limanmys/render-engine/app/models"
"github.com/limanmys/render-engine/internal/database"
)

func GetServer(server *models.Server) (*models.Server, error) {
result := database.Connection().First(&server)

if result.Error != nil {
return nil, fiber.NewError(fiber.StatusNotFound, "Cannot found server with this id")
}

if result.RowsAffected > 0 {
return server, nil
}

return nil, fiber.NewError(fiber.StatusNotFound, "Cannot found server with this id")
}
Loading

0 comments on commit a135435

Please sign in to comment.