More stuff
This commit is contained in:
parent
71beed7eb3
commit
9a7e420c83
7 changed files with 63 additions and 33 deletions
|
@ -48,12 +48,21 @@ type ConfigAdmin struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigStorage struct {
|
type ConfigStorage struct {
|
||||||
// Url to the postgres database. Must contain credentials and stuff
|
Host string `toml:"host"`
|
||||||
DatabaseUrl string `toml:"database_url"`
|
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
|
// Url to redis server. If empty, no redis is used
|
||||||
RedisUrl *string `toml:"redis_url,omitempty"`
|
RedisUrl *string `toml:"redis_url,omitempty"`
|
||||||
// The maximum size of the in-memory cache in bytes
|
// The maximum size of the in-memory cache in bytes
|
||||||
MaxInMemoryCacheSize int64 `toml:"max_in_memory_cache_size"`
|
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 {
|
type ConfigMail struct {
|
||||||
|
@ -74,7 +83,7 @@ type Config struct {
|
||||||
Admin ConfigAdmin `toml:"admin"`
|
Admin ConfigAdmin `toml:"admin"`
|
||||||
Webauthn ConfigWebAuthn `toml:"webauthn"`
|
Webauthn ConfigWebAuthn `toml:"webauthn"`
|
||||||
Storage ConfigStorage `toml:"storage"`
|
Storage ConfigStorage `toml:"storage"`
|
||||||
Mail ConfigMail `toml:"mail"`
|
Mail ConfigMail `toml:"mail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var GlobalConfig Config
|
var GlobalConfig Config
|
||||||
|
@ -102,9 +111,17 @@ var defaultConfig Config = Config{
|
||||||
HashingSecret: "some super secure secret that should never be changed or else password storage breaks",
|
HashingSecret: "some super secure secret that should never be changed or else password storage breaks",
|
||||||
},
|
},
|
||||||
Storage: ConfigStorage{
|
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,
|
RedisUrl: nil,
|
||||||
MaxInMemoryCacheSize: 1e6, // 1 Megabyte
|
MaxInMemoryCacheSize: 1e6, // 1 Megabyte
|
||||||
|
MaxInMemoryCacheTTL: 5,
|
||||||
|
MaxRedisCacheTTL: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +142,25 @@ func (gc *ConfigGeneral) GetFullPublicUrl() string {
|
||||||
return str
|
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 {
|
func WriteDefaultConfig(toFile string) error {
|
||||||
log.Trace().Caller().Send()
|
log.Trace().Caller().Send()
|
||||||
log.Info().Str("config-file", toFile).Msg("Writing default config to file")
|
log.Info().Str("config-file", toFile).Msg("Writing default config to file")
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
services: name
|
14
main.go
14
main.go
|
@ -35,21 +35,15 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Failed to start cache")
|
log.Fatal().Err(err).Msg("Failed to start cache")
|
||||||
}
|
}
|
||||||
var store *storage.Storage
|
|
||||||
if config.GlobalConfig.Storage.DbIsPostgres != nil &&
|
store, err := storage.NewStorage(config.GlobalConfig.Storage.BuildPostgresDSN(), storageCache)
|
||||||
*config.GlobalConfig.Storage.DbIsPostgres {
|
|
||||||
store, err = storage.NewStoragePostgres(
|
|
||||||
config.GlobalConfig.Storage.DatabaseUrl,
|
|
||||||
storageCache,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
store, err = storage.NewStorageSqlite(config.GlobalConfig.Storage.DatabaseUrl, storageCache)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Failed to setup storage")
|
log.Fatal().Err(err).Msg("Failed to setup storage")
|
||||||
}
|
}
|
||||||
_ = store
|
_ = store
|
||||||
// TODO: Set up media server
|
// TODO: Set up media server
|
||||||
|
// TODO: Set up queues
|
||||||
// TODO: Set up http server
|
// TODO: Set up http server
|
||||||
// TODO: Set up plugins
|
// TODO: Set up plugins
|
||||||
// TODO: Start everything
|
// TODO: Start everything
|
||||||
|
|
12
storage/cache/cache.go
vendored
12
storage/cache/cache.go
vendored
|
@ -12,6 +12,7 @@ import (
|
||||||
ristretto_store "github.com/eko/gocache/store/ristretto/v4"
|
ristretto_store "github.com/eko/gocache/store/ristretto/v4"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
|
@ -37,7 +38,9 @@ func NewCache(maxSize int64, redisUrl *string) (*Cache, error) {
|
||||||
}
|
}
|
||||||
ristrettoStore := ristretto_store.NewRistretto(
|
ristrettoStore := ristretto_store.NewRistretto(
|
||||||
ristrettoCache,
|
ristrettoCache,
|
||||||
store.WithExpiration(time.Second*10),
|
store.WithExpiration(
|
||||||
|
time.Second*time.Duration(config.GlobalConfig.Storage.MaxInMemoryCacheSize),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
var cacheManager *cache.ChainCache[[]byte]
|
var cacheManager *cache.ChainCache[[]byte]
|
||||||
|
@ -47,7 +50,12 @@ func NewCache(maxSize int64, redisUrl *string) (*Cache, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
redisClient := redis.NewClient(opts)
|
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(
|
cacheManager = cache.NewChain(
|
||||||
cache.New[[]byte](ristrettoStore),
|
cache.New[[]byte](ristrettoStore),
|
||||||
cache.New[[]byte](redisStore),
|
cache.New[[]byte](redisStore),
|
||||||
|
|
|
@ -4,3 +4,8 @@ package storage
|
||||||
// Things like true deletion of soft deleted data after some time
|
// Things like true deletion of soft deleted data after some time
|
||||||
// Or removing inactive access tokens
|
// Or removing inactive access tokens
|
||||||
// All of this will be handled by goroutines
|
// 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
|
||||||
|
//
|
||||||
|
|
|
@ -107,7 +107,9 @@ func (s *Storage) UpdateNote(note *Note) error {
|
||||||
}
|
}
|
||||||
err = s.cache.Set(cacheNoteIdToNotePrefix+note.ID, note)
|
err = s.cache.Set(cacheNoteIdToNotePrefix+note.ID, note)
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -121,13 +123,3 @@ func (s *Storage) DeleteNote(id string) {
|
||||||
s.cache.Delete(cacheNoteIdToNotePrefix + id)
|
s.cache.Delete(cacheNoteIdToNotePrefix + id)
|
||||||
s.db.Delete(Note{ID: 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(¬e, id)
|
|
||||||
if res.Error != nil {
|
|
||||||
return nil, res.Error
|
|
||||||
}
|
|
||||||
return ¬e, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gitlab.com/mstarongitlab/linstrom/storage/cache"
|
"gitlab.com/mstarongitlab/linstrom/storage/cache"
|
||||||
|
@ -47,8 +46,3 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
||||||
|
|
||||||
return &Storage{db, cache}, nil
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue