8000 refactor: transientDbSvc, marketplace datafield bug and more by ntorga · Pull Request #246 · goinfinite/os · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: transientDbSvc, marketplace datafield bug and more #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog

```log
0.2.3 - 2025/04/08
refactor: replace BadgerDB with SQLite in-memory
fix(critical): x-bind misplacement on InputClientSide component
fix: remove / from marketplace temp dir
fix: check type of vhost on aliasesHostname ToEntity()
tests: add tests for transientDbSvc

0.2.2 - 2025/04/04
refactor: ssl infra implementation
refactor: ssl watchdog after ssl infra refactor
Expand Down
20 changes: 2 additions & 18 deletions go.mod
10000
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.24.1

require (
github.com/alecthomas/chroma v0.10.0
github.com/dgraph-io/badger/v4 v4.6.0
github.com/glebarez/sqlite v1.11.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.6.0
Expand All @@ -25,21 +24,14 @@ require (
)

require (
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/a-h/templ v0.3.856
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgraph-io/ristretto v1.0.0 // indirect
github.com/dlclark/regexp2 v1.11.5 // indirect
github.com/dustin/go-humanize v1.0.1
github.com/ghodss/yaml v1.0.0 // indirect
Expand All @@ -49,21 +41,15 @@ require (
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/flatbuffers v25.2.10+incompatible // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/samber/lo v1.49.1 // indirect
github.com/samber/slog-common v0.18.1 // indirect
Expand All @@ -74,12 +60,10 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0
golang.org/x/time v0.11.0 // indirect
golang.org/x/tools v0.31.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.61.13 // indirect
modernc.org/mathutil v1.7.1 // indirect
Expand Down
292 changes: 18 additions & 274 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/infra/envs/envs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package infraEnvs

const (
InfiniteOsVersion string = "0.2.2"
InfiniteOsVersion string = "0.2.3"
InfiniteOsMainDir string = "/infinite"
InfiniteOsBinary string = InfiniteOsMainDir + "/os"
InfiniteOsEnvFilePath string = InfiniteOsMainDir + "/.env"
Expand Down
10 changes: 10 additions & 0 deletions src/infra/internalDatabase/model/virtualHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ func (model VirtualHost) ToEntity() (vhost entity.VirtualHost, err error) {

aliasesHostnames := []valueObject.Fqdn{}
for _, alias := range model.Aliases {
if alias.Type != valueObject.VirtualHostTypeAlias.String() {
continue
}
if alias.Hostname == model.Hostname {
continue
}
if model.ParentHostname != nil && alias.Hostname == *model.ParentHostname {
continue
}

aliasHostname, err := valueObject.NewFqdn(alias.Hostname)
if err != nil {
slog.Debug("AliasHostnameError", slog.String("alias", alias.Hostname))
Expand Down
94 changes: 47 additions & 47 deletions src/infra/internalDatabase/transientDatabaseService.go
F438
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,75 @@ package internalDbInfra
import (
"errors"

"github.com/dgraph-io/badger/v4"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)

type TransientDatabaseService struct {
Handler *badger.DB
Handler *gorm.DB
}

func NewTransientDatabaseService() (*TransientDatabaseService, error) {
9E7A dbSvcOptions := badger.DefaultOptions("").
WithInMemory(true).
WithNumVersionsToKeep(1).
WithLoggingLevel(badger.ERROR)
type KeyValueModel struct {
Key string `gorm:"primaryKey"`
Value string
}

dbSvc, err := badger.Open(dbSvcOptions)
func NewTransientDatabaseService() (*TransientDatabaseService, error) {
ormSvc, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
return nil, errors.New("TransientDatabaseConnectionError")
}

return &TransientDatabaseService{
Handler: dbSvc,
}, nil
err = ormSvc.AutoMigrate(&KeyValueModel{})
if err != nil {
return nil, errors.New("TransientDatabaseMigrationError: " + err.Error())
}

return &TransientDatabaseService{Handler: ormSvc}, nil
}

func (dbSvc *TransientDatabaseService) Has(key string) bool {
hasKey := false
err := dbSvc.Handler.View(func(txn *badger.Txn) error {
_, err := txn.Get([]byte(key))
if err == nil {
hasKey = true
}

return nil
})
if err != nil {
var count int64
result := dbSvc.Handler.Model(&KeyValueModel{}).
Where("key = ?", key).Count(&count)
if result.Error != nil {
return false
}

return hasKey
return count > 0
}

func (dbSvc *TransientDatabaseService) Get(key string) (string, error) {
var value string
err := dbSvc.Handler.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}

err = item.Value(func(val []byte) error {
value = string(val)
return nil
})
if err != nil {
return err
}
func (dbSvc *TransientDatabaseService) Read(key string) (string, error) {
var keyValueModel KeyValueModel
result := dbSvc.Handler.Model(&KeyValueModel{}).
Where("key = ?", key).Find(&keyValueModel)
if result.Error != nil {
return "", result.Error
}

return nil
})
if err != nil {
return "", err
if result.RowsAffected == 0 {
return "", errors.New("KeyNotFound")
}

return value, nil
return keyValueModel.Value, nil
}

func (dbSvc *TransientDatabaseService) Set(key string, value string) error {
err := dbSvc.Handler.Update(func(txn *badger.Txn) error {
err := txn.Set([]byte(key), []byte(value))
return err
})
return err
keyValueModel := KeyValueModel{Key: key, Value: value}

result := dbSvc.Handler.Model(&KeyValueModel{}).
Where("key = ?", key).FirstOrCreate(&keyValueModel)
if result.Error != nil {
return result.Error
}

if result.RowsAffected == 0 {
result = dbSvc.Handler.Model(&KeyValueModel{}).
Where("key = ?", key).Update("value", value)
if result.Error != nil {
return result.Error
}
}

return nil
}
60 changes: 60 additions & 0 deletions src/infra/internalDatabase/transientDatabaseService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package internalDbInfra

import (
"testing"
)

func TestTransientDbSvc(t *testing.T) {
dbSvc, err := NewTransientDatabaseService()
if err != nil {
t.Fatalf("TransientDatabaseServiceInitFailed: %v", err)
}

testKey := "test_key"
testValue := "test_value"
nonExistentKey := "non_existent_key"
updatedValue := "updated_value"

t.Run("HasFunctionality", func(t *testing.T) {
if dbSvc.Has(testKey) {
t.Errorf("HasReturnedTrueForNonExistentKey")
}

err := dbSvc.Set(testKey, testValue)
if err != nil {
t.Errorf("SetFailed: %v", err)
}

if !dbSvc.Has(testKey) {
t.Errorf("HasReturnedFalseForExistingKey")
}
})

t.Run("ReadWriteFunctionality", func(t *testing.T) {
value, err := dbSvc.Read(testKey)
if err != nil {
t.Errorf("ReadFailed: %v", err)
}
if value != testValue {
t.Errorf("IncorrectValue: Got %v, Want %v", value, testValue)
}

_, err = dbSvc.Read(nonExistentKey)
if err == nil {
t.Errorf("ExpectedErrorForNonExistentKeyButGotNil")
}

err = dbSvc.Set(testKey, updatedValue)
if err != nil {
t.Errorf("UpdateFailed: %v", err)
}

value, err = dbSvc.Read(testKey)
if err != nil {
t.Errorf("ReadFailedAfterUpdate: %v", err)
}
if value != updatedValue {
t.Errorf("IncorrectValueAfterUpdate: Got %v, Want %v", value, updatedValue)
}
})
}
2 changes: 1 addition & 1 deletion src/infra/marketplace/marketplaceCmdRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/google/uuid"
)

const installTempDirPath = "/app/marketplace-tmp/"
const installTempDirPath = "/app/marketplace-tmp"

type MarketplaceCmdRepo struct {
persistentDbSvc *internalDbInfra.PersistentDatabaseService
Expand Down
2 changes: 1 addition & 1 deletion src/infra/o11y/o11yQueryRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (repo *O11yQueryRepo) ReadServerPublicIpAddress() (
ipAddress valueObject.IpAddress,
err error,
) {
cachedIpAddressStr, err := repo.transientDbSvc.Get(PublicIpTransientKey)
cachedIpAddressStr, err := repo.transientDbSvc.Read(PublicIpTransientKey)
if err == nil {
return valueObject.NewIpAddress(cachedIpAddressStr)
}
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

// @title OsApi
// @version 0.2.2
// @version 0.2.3
// @description Infinite OS API
// @termsOfService https://goinfinite.net/tos/

Expand Down
2 changes: 1 addition & 1 deletion src/presentation/api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4405,7 +4405,7 @@ const docTemplate = `{

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "0.2.2",
Version: "0.2.3",
Host: "localhost:1618",
BasePath: "/api",
Schemes: []string{},
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "Eclipse Public License v2.0",
"url": "https://www.eclipse.org/legal/epl-2.0/"
},
"version": "0.2.2"
"version": "0.2.3"
},
"host": "localhost:1618",
"basePath": "/api",
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/api/docs/swagger.yaml
2851
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ info:
url: https://www.eclipse.org/legal/epl-2.0/
termsOfService: https://goinfinite.net/tos/
title: OsApi
version: 0.2.2
version: 0.2.3
paths:
/v1/account/:
get:
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/ui/component/form/inputField.templ
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ templ InputFieldClientSide(bindInputType, bindLabel, bindValuePath string) {
<input
:type={ bindInputType }
if bindValuePath != "" {
:value={ bindValuePath }
x-model:value={ bindValuePath }
}
autocomplete="off"
:placeholder={ bindLabel }
Expand Down
0