More stuff

This commit is contained in:
Melody Becker 2024-09-15 15:18:05 +02:00
parent 71beed7eb3
commit 9a7e420c83
7 changed files with 63 additions and 33 deletions

View file

@ -48,12 +48,21 @@ type ConfigAdmin struct {
}
type ConfigStorage struct {
// Url to the postgres database. Must contain credentials and stuff
DatabaseUrl string `toml:"database_url"`
Host string `toml:"host"`
Username string `toml:"username"`
Password string `toml:"password"`
DbName string `toml:"db_name"`
Port int `toml:"port"`
SslMode *string `toml:"ssl_mode"`
TimeZone *string `toml:"time_zone"`
// Url to redis server. If empty, no redis is used
RedisUrl *string `toml:"redis_url,omitempty"`
// The maximum size of the in-memory cache in bytes
MaxInMemoryCacheSize int64 `toml:"max_in_memory_cache_size"`
// The time to live for in app in memory cache items, in seconds
MaxInMemoryCacheTTL int
// The time to live for items in redis, in seconds
MaxRedisCacheTTL *int
}
type ConfigMail struct {
@ -102,9 +111,17 @@ var defaultConfig Config = Config{
HashingSecret: "some super secure secret that should never be changed or else password storage breaks",
},
Storage: ConfigStorage{
DatabaseUrl: "db.sqlite",
Host: "localhost",
Username: "linstrom",
Password: "linstrom",
DbName: "linstrom",
Port: 5432,
SslMode: other.IntoPointer("disable"),
TimeZone: other.IntoPointer("Europe/Berlin"),
RedisUrl: nil,
MaxInMemoryCacheSize: 1e6, // 1 Megabyte
MaxInMemoryCacheTTL: 5,
MaxRedisCacheTTL: nil,
},
}
@ -125,6 +142,25 @@ func (gc *ConfigGeneral) GetFullPublicUrl() string {
return str
}
func (sc *ConfigStorage) BuildPostgresDSN() string {
dsn := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%d",
sc.Host,
sc.Username,
sc.Password,
sc.DbName,
sc.Port,
)
if sc.SslMode != nil {
dsn += fmt.Sprintf(" sslmode=%s", *sc.SslMode)
}
if sc.TimeZone != nil {
dsn += fmt.Sprintf(" TimeZone=%s", *sc.TimeZone)
}
return dsn
}
func WriteDefaultConfig(toFile string) error {
log.Trace().Caller().Send()
log.Info().Str("config-file", toFile).Msg("Writing default config to file")

View file

@ -0,0 +1 @@
services: name

14
main.go
View file

@ -35,21 +35,15 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("Failed to start cache")
}
var store *storage.Storage
if config.GlobalConfig.Storage.DbIsPostgres != nil &&
*config.GlobalConfig.Storage.DbIsPostgres {
store, err = storage.NewStoragePostgres(
config.GlobalConfig.Storage.DatabaseUrl,
storageCache,
)
} else {
store, err = storage.NewStorageSqlite(config.GlobalConfig.Storage.DatabaseUrl, storageCache)
}
store, err := storage.NewStorage(config.GlobalConfig.Storage.BuildPostgresDSN(), storageCache)
if err != nil {
log.Fatal().Err(err).Msg("Failed to setup storage")
}
_ = store
// TODO: Set up media server
// TODO: Set up queues
// TODO: Set up http server
// TODO: Set up plugins
// TODO: Start everything

View file

@ -12,6 +12,7 @@ import (
ristretto_store "github.com/eko/gocache/store/ristretto/v4"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/config"
)
type Cache struct {
@ -37,7 +38,9 @@ func NewCache(maxSize int64, redisUrl *string) (*Cache, error) {
}
ristrettoStore := ristretto_store.NewRistretto(
ristrettoCache,
store.WithExpiration(time.Second*10),
store.WithExpiration(
time.Second*time.Duration(config.GlobalConfig.Storage.MaxInMemoryCacheSize),
),
)
var cacheManager *cache.ChainCache[[]byte]
@ -47,7 +50,12 @@ func NewCache(maxSize int64, redisUrl *string) (*Cache, error) {
return nil, err
}
redisClient := redis.NewClient(opts)
redisStore := redis_store.NewRedis(redisClient, store.WithExpiration(time.Minute))
redisStore := redis_store.NewRedis(
redisClient,
store.WithExpiration(
time.Second*time.Duration(*config.GlobalConfig.Storage.MaxRedisCacheTTL),
),
)
cacheManager = cache.NewChain(
cache.New[[]byte](ristrettoStore),
cache.New[[]byte](redisStore),

View file

@ -4,3 +4,8 @@ package storage
// Things like true deletion of soft deleted data after some time
// Or removing inactive access tokens
// All of this will be handled by goroutines
// TODO: Delete everything soft deleted and older than a month
// TODO: Delete old tokens not in active use anymore
// TODO: Start jobs where the last check-in was more than an hour ago
//

View file

@ -107,7 +107,9 @@ func (s *Storage) UpdateNote(note *Note) error {
}
err = s.cache.Set(cacheNoteIdToNotePrefix+note.ID, note)
if err != nil {
log.Warn().Err(err).Msg("Failed to update note into cache. Cache and db might be out of sync, a force sync is recommended")
log.Warn().
Err(err).
Msg("Failed to update note into cache. Cache and db might be out of sync, a force sync is recommended")
}
return nil
}
@ -121,13 +123,3 @@ func (s *Storage) DeleteNote(id string) {
s.cache.Delete(cacheNoteIdToNotePrefix + id)
s.db.Delete(Note{ID: id})
}
// Try and find a note with a given ID
func (store *Storage) FindNoteById(id string) (*Note, error) {
note := Note{}
res := store.db.First(&note, id)
if res.Error != nil {
return nil, res.Error
}
return &note, nil
}

View file

@ -7,7 +7,6 @@ package storage
import (
"errors"
"fmt"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/storage/cache"
@ -47,8 +46,3 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
return &Storage{db, cache}, nil
}
// TODO: Placeholder. Update to proper implementation later. Including signature
func (s *Storage) FindLocalAccount(handle string) (string, error) {
return handle, nil
}