Add admin config page and runtime-tunable upload/rate-limit settings
This commit is contained in:
40
internal/config/repository.go
Normal file
40
internal/config/repository.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package config
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) Get(key string) (*ConfigEntry, error) {
|
||||
var e ConfigEntry
|
||||
if err := r.db.First(&e, "key = ?", key).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &e, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Upsert(key, value string) error {
|
||||
// Prefer a simple approach that works across sqlite/postgres/mysql.
|
||||
// Try update first, then insert if nothing updated.
|
||||
res := r.db.Model(&ConfigEntry{}).Where("key = ?", key).Update("value", value)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected > 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.Create(&ConfigEntry{Key: key, Value: value}).Error
|
||||
}
|
||||
|
||||
func (r *Repository) List() ([]ConfigEntry, error) {
|
||||
var entries []ConfigEntry
|
||||
if err := r.db.Order("key asc").Find(&entries).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
Reference in New Issue
Block a user