Skip to content
Snippets Groups Projects
Commit 058a322c authored by Michael Zigldrum's avatar Michael Zigldrum
Browse files

Merge branch 'main' into 'main'

Add Core Lib

See merge request eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service!6
parents f382d074 65cdd738
No related branches found
No related tags found
No related merge requests found
......@@ -5,19 +5,19 @@ go 1.21.4
require (
github.com/cloudevents/sdk-go/v2 v2.14.0
github.com/gin-gonic/gin v1.9.1
github.com/jackc/pgx/v5 v5.5.2
github.com/sethvargo/go-retry v0.2.4
github.com/jackc/pgx/v5 v5.5.3
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
gitlab.eclipse.org/eclipse/xfsc/libraries/messaging/cloudeventprovider v0.1.0
gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core v0.0.0-20240212140057-ddf9d6a6a28d
)
require (
github.com/Azure/go-amqp v0.17.0 // indirect
github.com/Shopify/sarama v1.38.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cloudevents/sdk-go/protocol/amqp/v2 v2.14.0 // indirect
github.com/cloudevents/sdk-go/protocol/kafka_sarama/v2 v2.14.0 // indirect
github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20240112101907-998919d6876d // indirect
......@@ -29,15 +29,18 @@ require (
github.com/eapache/queue v1.1.0 // indirect
github.com/eclipse/paho.golang v0.12.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-migrate/migrate/v4 v4.17.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
......@@ -51,41 +54,43 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nats-io/nats.go v1.32.0 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/arch v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
This diff is collapsed.
package config
import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"strings"
"gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/config"
pgPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/db/postgres"
)
type statusListConfiguration struct {
LogLevel string `mapstructure:"logLevel"`
Port int `mapstructure:"servingPort"`
Database struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Db string `mapstructure:"db"`
} `mapstructure:"database"`
CreationTopic string `mapstructure:"creationTopic"`
BlockSizeInBytes int `mapstructure:"blockSizeInBytes"`
config.BaseConfig `mapstructure:",squash"`
Database pgPkg.Config `mapstructure:"database"`
CreationTopic string `mapstructure:"creationTopic"`
BlockSizeInBytes int `mapstructure:"blockSizeInBytes"`
}
var CurrentStatusListConfig statusListConfiguration
func LoadConfig() error {
setDefaults()
readConfig()
if err := viper.Unmarshal(&CurrentStatusListConfig); err != nil {
return err
}
setLogLevel()
return nil
}
func readConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.SetEnvPrefix("STATUSLIST")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := viper.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
log.Printf("Configuration not found but environment variables will be taken into account.")
}
}
viper.AutomaticEnv()
}
func setDefaults() {
viper.SetDefault("logLevel", "info")
viper.SetDefault("servingPort", 8080)
viper.SetDefault("databaseUrl", "postgres://root:pass@postgres_db:5432/0")
viper.SetDefault("creationTopic", "status")
func Load() error {
return config.LoadConfig("CONFIGURATIONSERVICE", &CurrentStatusListConfig, getDefaults())
}
func setLogLevel() {
var logLevel log.Level
switch strings.ToLower(CurrentStatusListConfig.LogLevel) {
case "trace":
logLevel = log.TraceLevel
case "debug":
logLevel = log.DebugLevel
case "info":
logLevel = log.InfoLevel
case "error":
logLevel = log.ErrorLevel
case "fatal":
logLevel = log.FatalLevel
case "panic":
logLevel = log.PanicLevel
default:
logLevel = log.WarnLevel
func getDefaults() map[string]any {
return map[string]any{
"isDev": false,
"creationTopic": "status",
}
log.SetLevel(logLevel)
log.Infof("loglevel set to %s", logLevel.String())
}
......@@ -2,6 +2,7 @@ package database
import (
"context"
pgPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/db/postgres"
"gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/entity"
)
......@@ -19,7 +20,7 @@ type Database struct {
DbConnection
}
func New(username string, password string, host string, port int, db string, blockSizeInBytes int) (*Database, error) {
dbConnection, err := newPostgresConnection(username, password, host, port, db, blockSizeInBytes)
func New(ctx context.Context, config pgPkg.Config, blockSizeInBytes int) (*Database, error) {
dbConnection, err := newPostgresConnection(config, ctx, blockSizeInBytes)
return &Database{DbConnection: dbConnection}, err
}
......@@ -6,9 +6,11 @@ import (
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/sethvargo/go-retry"
log "github.com/sirupsen/logrus"
entity2 "gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/entity"
ctxPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/ctx"
pgPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/db/postgres"
errPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/err"
"gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/entity"
"os"
"regexp"
"time"
)
......@@ -20,28 +22,15 @@ type postgresConnection struct {
blockSizeInBytes int
}
func newPostgresConnection(username string, password string, host string, port int, db string, blockSizeInBytes int) (DbConnection, error) {
connUrl := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", username, password, host, port, db)
conn, err := pgxpool.New(context.Background(), connUrl)
if err != nil {
return nil, err
}
bf := retry.NewFibonacci(time.Millisecond * 500)
bf = retry.WithCappedDuration(time.Second*30, bf)
bf = retry.WithJitter(time.Millisecond*50, bf)
bf = retry.WithMaxDuration(time.Minute*2, bf)
func newPostgresConnection(database pgPkg.Config, ctx context.Context, blockSizeInBytes int) (DbConnection, error) {
logger := ctxPkg.GetLogger(ctx)
if err = retry.Do(context.Background(), bf, func(_ context.Context) error {
if err = conn.Ping(context.Background()); err != nil {
err = fmt.Errorf("connection check failed: %w", err)
log.Error(err)
err = retry.RetryableError(err)
return err
}
return nil
}); err != nil {
return nil, err
errChan := make(chan error)
go errPkg.LogChan(logger, errChan)
conn, err := pgPkg.ConnectRetry(ctx, database, time.Minute, errChan)
if err != nil {
logger.Error(err, "failed to connect to postgres")
os.Exit(1)
}
return &postgresConnection{
......@@ -50,7 +39,7 @@ func newPostgresConnection(username string, password string, host string, port i
}, nil
}
func (pc *postgresConnection) AllocateIndexInCurrentBlock(ctx context.Context, tenantId string) (*entity2.StatusData, error) {
func (pc *postgresConnection) AllocateIndexInCurrentBlock(ctx context.Context, tenantId string) (*entity.StatusData, error) {
tx, err := pc.conn.BeginTx(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadCommitted,
AccessMode: pgx.ReadWrite,
......@@ -76,14 +65,14 @@ func (pc *postgresConnection) AllocateIndexInCurrentBlock(ctx context.Context, t
return nil, fmt.Errorf("error while select current block from the database: %w", err)
}
// not optimized for performance cause of reflection
databaseRows, err := pgx.CollectRows(rows, pgx.RowToStructByName[entity2.Block])
databaseRows, err := pgx.CollectRows(rows, pgx.RowToStructByName[entity.Block])
if err != nil {
return nil, fmt.Errorf("error while collecting current block from rows: %w", err)
}
if len(databaseRows) == 0 {
// no current block -> create new one and allocate index
newBlock := entity2.NewBlock(pc.blockSizeInBytes)
newBlock := entity.NewBlock(pc.blockSizeInBytes)
index, err := newBlock.AllocateNextFreeIndex()
if err != nil {
......@@ -102,7 +91,7 @@ func (pc *postgresConnection) AllocateIndexInCurrentBlock(ctx context.Context, t
return nil, fmt.Errorf("error commiting transaction: %w", err)
}
return entity2.NewStatusData(index, blockId), nil
return entity.NewStatusData(index, blockId), nil
}
// allocate index in current block
......@@ -122,7 +111,7 @@ func (pc *postgresConnection) AllocateIndexInCurrentBlock(ctx context.Context, t
return nil, fmt.Errorf("error commiting transaction: %w", err)
}
return entity2.NewStatusData(index, currentBlock.BlockId), nil
return entity.NewStatusData(index, currentBlock.BlockId), nil
}
func (pc *postgresConnection) RevokeCredentialInSpecifiedBlock(ctx context.Context, tenantId string, blockId int, index int) error {
......@@ -147,7 +136,7 @@ func (pc *postgresConnection) RevokeCredentialInSpecifiedBlock(ctx context.Conte
if err != nil {
return fmt.Errorf("error while select specified block from the database: %w", err)
}
databaseRows, err := pgx.CollectRows(rows, pgx.RowToStructByName[entity2.Block])
databaseRows, err := pgx.CollectRows(rows, pgx.RowToStructByName[entity.Block])
if err != nil {
return fmt.Errorf("error while getting specified block from rows: %w", err)
}
......@@ -214,7 +203,7 @@ func (pc *postgresConnection) CreateTableForTenantIdIfNotExists(ctx context.Cont
return fmt.Errorf("could not create new table for tenantID: %w", err)
}
newBlock := entity2.NewBlock(pc.blockSizeInBytes)
newBlock := entity.NewBlock(pc.blockSizeInBytes)
insertQuery := fmt.Sprintf("INSERT INTO %s (block, free) VALUES ($1, $2)", tableName)
_, err = tx.Exec(ctx, insertQuery, newBlock.Block, newBlock.Free)
......
package main
import (
"context"
log "github.com/sirupsen/logrus"
ctxPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/ctx"
logPkg "gitlab.eclipse.org/eclipse/xfsc/libraries/microservice/core/pkg/logr"
"gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/api"
"gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/config"
"gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/database"
)
func main() {
if err := config.LoadConfig(); err != nil {
log.Error(err)
ctx := context.Background()
if err := config.Load(); err != nil {
log.Fatalf("failed to load config: %v", err)
}
currentConf := &config.CurrentStatusListConfig
dbConf := &currentConf.Database
db, err := database.New(
dbConf.Username,
dbConf.Password,
dbConf.Host,
dbConf.Port,
dbConf.Db,
currentConf.BlockSizeInBytes,
)
logger, err := logPkg.New(currentConf.LogLevel, currentConf.IsDev, nil)
if err != nil {
log.Error(err)
log.Fatalf("failed to init logger: %v", err)
}
defer db.Close()
ctx = ctxPkg.WithLogger(ctx, *logger)
dbConf := &currentConf.Database
db, err := database.New(ctx, *dbConf, currentConf.BlockSizeInBytes)
api.Listen(db, currentConf.Port, currentConf.CreationTopic)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment