Adding queues and storage is now postgres only

This commit is contained in:
Melody Becker 2024-09-15 12:24:36 +02:00
parent 7ec0ce5a45
commit 1c2472cc2c
11 changed files with 368 additions and 31 deletions

64
storage/gormLogger.go Normal file
View file

@ -0,0 +1,64 @@
package storage
import (
"context"
"time"
"github.com/rs/zerolog"
"gorm.io/gorm/logger"
)
type gormLogger struct {
logger zerolog.Logger
}
func newGormLogger(zerologger zerolog.Logger) *gormLogger {
return &gormLogger{zerologger}
}
func (g *gormLogger) LogMode(newLevel logger.LogLevel) logger.Interface {
switch newLevel {
case logger.Error:
g.logger = g.logger.Level(zerolog.ErrorLevel)
case logger.Warn:
g.logger = g.logger.Level(zerolog.WarnLevel)
case logger.Info:
g.logger = g.logger.Level(zerolog.InfoLevel)
case logger.Silent:
g.logger = g.logger.Level(zerolog.Disabled)
}
return g
}
func (g *gormLogger) Info(ctx context.Context, format string, args ...interface{}) {
g.logger.Info().Ctx(ctx).Msgf(format, args...)
}
func (g *gormLogger) Warn(ctx context.Context, format string, args ...interface{}) {
g.logger.Warn().Ctx(ctx).Msgf(format, args...)
}
func (g *gormLogger) Error(ctx context.Context, format string, args ...interface{}) {
g.logger.Error().Ctx(ctx).Msgf(format, args...)
}
func (g *gormLogger) Trace(
ctx context.Context,
begin time.Time,
fc func() (sql string, rowsAffected int64),
err error,
) {
sql, rowsAffected := fc()
g.logger.Trace().
Ctx(ctx).
Time("gorm-begin", begin).
Err(err).
Str("gorm-query", sql).
Int64("gorm-rows-affected", rowsAffected).
Send()
}
func (g *gormLogger) OverwriteLoggingLevel(new zerolog.Level) {
g.logger = g.logger.Level(new)
}
func (g *gormLogger) OverwriteLogger(new zerolog.Logger) {
g.logger = new
}

36
storage/inboundJobs.go Normal file
View file

@ -0,0 +1,36 @@
package storage
import (
"time"
)
// Auto-generate string names for the various constants
//go:generate stringer -type InboundJobSource
type InboundJobSource uint8
// TODO: Adjust and expand these constants later, depending on sources
const (
InJobSourceAccInbox InboundJobSource = iota
InJobSourceServerInbox
InJobSourceApiMasto
InJobSourceApiLinstrom
)
// Store inbound jobs from api and ap in the db until they finished processing
// Ensures data consistency in case the server is forced to restart unexpectedly
// No DeletedAt field since don't want completed jobs to linger in the db for any longer than necessary
type InboundJob struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
// Raw data, could be json or gob data, check source for how to interpret
RawData []byte
// Where this job is coming from. Important for figuring out how to decode the raw data and what to do with it
Source InboundJobSource
// Section: Various data
// TODO: Expand based on needs
// If from an inbox, include the owner id here
InboxOwner *string
}

13
storage/outboundJobs.go Normal file
View file

@ -0,0 +1,13 @@
package storage
import (
"gorm.io/gorm"
)
type OutboundJob struct {
gorm.Model // Include full model. Gives ID, created and updated at timestamps as well as soft deletes
// Read (and create) only values to ensure consistency
TargetServer string `gorm:"->;<-:create"` // The url of the target server
TargetPath string `gorm:"->;<-:create"` // The full path of api endpoint targeted
Data []byte `gorm:"->;<-:create"` // The raw data to send
}

View file

@ -3,7 +3,7 @@ package storage
import (
"errors"
"github.com/glebarez/sqlite"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/storage/cache"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@ -18,41 +18,26 @@ type Storage struct {
var ErrInvalidData = errors.New("invalid data")
// Build a new storage using sqlite as database backend
func NewStorageSqlite(filePath string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(sqlite.Open(filePath))
func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{
Logger: newGormLogger(log.Logger),
})
if err != nil {
return nil, err
}
return storageFromEmptyDb(db, cache)
}
func NewStoragePostgres(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl))
if err != nil {
return nil, err
}
return storageFromEmptyDb(db, cache)
}
func storageFromEmptyDb(db *gorm.DB, cache *cache.Cache) (*Storage, error) {
// AutoMigrate ensures the db is in a state where all the structs given here
// have their own tables and relations setup. It also updates tables if necessary
err := db.AutoMigrate(
err = db.AutoMigrate(
MediaMetadata{},
Account{},
RemoteServer{},
Note{},
Role{},
PasskeySession{},
InboundJob{},
OutboundJob{},
)
if err != nil {
return nil, err
}
// And finally, build the actual storage struct
return &Storage{
db: db,
cache: cache,
}, nil
return &Storage{db, cache}, nil
}

58
storage/storage.go.old Normal file
View file

@ -0,0 +1,58 @@
package storage
import (
"errors"
"github.com/glebarez/sqlite"
"gitlab.com/mstarongitlab/linstrom/storage/cache"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// Storage is responsible for all database, cache and media related actions
// and serves as the lowest layer of the cake
type Storage struct {
db *gorm.DB
cache *cache.Cache
}
var ErrInvalidData = errors.New("invalid data")
// Build a new storage using sqlite as database backend
func NewStorageSqlite(filePath string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(sqlite.Open(filePath))
if err != nil {
return nil, err
}
return storageFromEmptyDb(db, cache)
}
func NewStoragePostgres(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl))
if err != nil {
return nil, err
}
return storageFromEmptyDb(db, cache)
}
func storageFromEmptyDb(db *gorm.DB, cache *cache.Cache) (*Storage, error) {
// AutoMigrate ensures the db is in a state where all the structs given here
// have their own tables and relations setup. It also updates tables if necessary
err := db.AutoMigrate(
MediaMetadata{},
Account{},
RemoteServer{},
Note{},
Role{},
PasskeySession{},
)
if err != nil {
return nil, err
}
// And finally, build the actual storage struct
return &Storage{
db: db,
cache: cache,
}, nil
}