- Commit database transactions - Listen to all interfaces by default - Use logger in collerd - Add docker-compose file with PostgreSQL Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
01031a24ab
commit
7d7614637b
7 changed files with 45 additions and 6 deletions
|
@ -33,7 +33,7 @@ func NewConfig() *Config {
|
|||
IDLength: 5,
|
||||
PasswordLength: 16,
|
||||
ExpirationInterval: 60, // 1 minute
|
||||
ListenAddress: "127.0.0.1",
|
||||
ListenAddress: "0.0.0.0",
|
||||
ListenPort: 8080,
|
||||
Expirations: []int{
|
||||
300, // 5 minutes
|
||||
|
|
|
@ -93,6 +93,7 @@ func (d *Database) StartExpireThread() {
|
|||
func (d *Database) Get(id string) (*Note, error) {
|
||||
var note Note
|
||||
trx := d.db.Where("id = ?", id).Find(¬e)
|
||||
defer trx.Commit()
|
||||
if trx.Error != nil {
|
||||
d.logger.Warn("could not find note", slog.Any("error", trx.Error))
|
||||
return nil, trx.Error
|
||||
|
@ -134,6 +135,7 @@ func (d *Database) Create(content []byte, password string, encrypted bool, expir
|
|||
note.Encrypted = true
|
||||
}
|
||||
trx := d.db.Create(note)
|
||||
defer trx.Commit()
|
||||
if trx.Error != nil {
|
||||
d.logger.Warn("could not create note", slog.Any("error", trx.Error))
|
||||
return nil, trx.Error
|
||||
|
@ -143,6 +145,7 @@ func (d *Database) Create(content []byte, password string, encrypted bool, expir
|
|||
|
||||
func (d *Database) Delete(id string) error {
|
||||
trx := d.db.Where("id = ?", id).Delete(&Note{})
|
||||
defer trx.Commit()
|
||||
if trx.Error != nil {
|
||||
d.logger.Error("could not delete note", slog.Any("error", trx.Error))
|
||||
return trx.Error
|
||||
|
|
|
@ -21,7 +21,7 @@ type Note struct {
|
|||
}
|
||||
|
||||
// Generate ID and compress content before saving to the database
|
||||
func (n *Note) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
func (n *Note) BeforeCreate(trx *gorm.DB) (err error) {
|
||||
for i := 0; i < ID_MAX_RETRIES; i++ {
|
||||
if n.ID != "" {
|
||||
continue
|
||||
|
@ -30,7 +30,7 @@ func (n *Note) BeforeCreate(tx *gorm.DB) (err error) {
|
|||
id := internal.GenerateChars(idLength)
|
||||
|
||||
var note Note
|
||||
tx.Where("id = ?", id).Find(¬e)
|
||||
trx.Where("id = ?", id).Find(¬e)
|
||||
|
||||
if note.ID == "" {
|
||||
n.ID = id
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue