Skip to content

Commit

Permalink
implemented singleUserManager
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Aug 17, 2024
1 parent 8cf633b commit f2a883d
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 57 deletions.
30 changes: 30 additions & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package api

import (
"errors"
)

var (
ErrStopIter = errors.New("stop iteration")
ErrNotFound = errors.New("Item not found")
ErrExist = errors.New("Item is already exist")
)
4 changes: 0 additions & 4 deletions api/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ import (
"github.com/LiterMC/go-openbmclapi/utils"
)

var (
ErrNotFound = errors.New("Item not found")
)

type SubscriptionManager interface {
GetWebPushKey() string

Expand Down
7 changes: 0 additions & 7 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@
package database

import (
"errors"
"time"

"github.com/google/uuid"

"github.com/LiterMC/go-openbmclapi/api"
)

var (
ErrStopIter = errors.New("stop iteration")
ErrNotFound = errors.New("no record was found")
ErrExists = errors.New("record's key was already exists")
)

type DB interface {
// Cleanup will release any release that the database created
// No operation should be executed during or after cleanup
Expand Down
52 changes: 26 additions & 26 deletions database/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m *MemoryDB) ValidJTI(jti string) (bool, error) {

expire, ok := m.tokens[jti]
if !ok {
return false, ErrNotFound
return false, api.ErrNotFound
}
if time.Now().After(expire) {
return false, nil
Expand All @@ -85,7 +85,7 @@ func (m *MemoryDB) AddJTI(jti string, expire time.Time) error {
m.tokenMux.Lock()
defer m.tokenMux.Unlock()
if _, ok := m.tokens[jti]; ok {
return ErrExists
return api.ErrExist
}
m.tokens[jti] = expire
return nil
Expand All @@ -96,13 +96,13 @@ func (m *MemoryDB) RemoveJTI(jti string) error {
_, ok := m.tokens[jti]
m.tokenMux.RUnlock()
if !ok {
return ErrNotFound
return api.ErrNotFound
}

m.tokenMux.Lock()
defer m.tokenMux.Unlock()
if _, ok := m.tokens[jti]; !ok {
return ErrNotFound
return api.ErrNotFound
}
delete(m.tokens, jti)
return nil
Expand All @@ -114,7 +114,7 @@ func (m *MemoryDB) GetFileRecord(path string) (*FileRecord, error) {

record, ok := m.fileRecords[path]
if !ok {
return nil, ErrNotFound
return nil, api.ErrNotFound
}
return record, nil
}
Expand All @@ -136,7 +136,7 @@ func (m *MemoryDB) RemoveFileRecord(path string) error {
defer m.fileRecMux.Unlock()

if _, ok := m.fileRecords[path]; !ok {
return ErrNotFound
return api.ErrNotFound
}
delete(m.fileRecords, path)
return nil
Expand All @@ -148,7 +148,7 @@ func (m *MemoryDB) ForEachFileRecord(cb func(*FileRecord) error) error {

for _, v := range m.fileRecords {
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -163,7 +163,7 @@ func (m *MemoryDB) GetSubscribe(user string, client string) (*api.SubscribeRecor

record, ok := m.subscribeRecords[[2]string{user, client}]
if !ok {
return nil, ErrNotFound
return nil, api.ErrNotFound
}
return record, nil
}
Expand All @@ -176,7 +176,7 @@ func (m *MemoryDB) SetSubscribe(record api.SubscribeRecord) error {
if record.EndPoint == "" {
old, ok := m.subscribeRecords[key]
if !ok {
return ErrNotFound
return api.ErrNotFound
}
record.EndPoint = old.EndPoint
}
Expand All @@ -191,7 +191,7 @@ func (m *MemoryDB) RemoveSubscribe(user string, client string) error {
key := [2]string{user, client}
_, ok := m.subscribeRecords[key]
if !ok {
return ErrNotFound
return api.ErrNotFound
}
delete(m.subscribeRecords, key)
return nil
Expand All @@ -203,7 +203,7 @@ func (m *MemoryDB) ForEachSubscribe(cb func(*api.SubscribeRecord) error) error {

for _, v := range m.subscribeRecords {
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -218,7 +218,7 @@ func (m *MemoryDB) GetEmailSubscription(user string, addr string) (*api.EmailSub

record, ok := m.emailSubRecords[[2]string{user, addr}]
if !ok {
return nil, ErrNotFound
return nil, api.ErrNotFound
}
return record, nil
}
Expand All @@ -229,7 +229,7 @@ func (m *MemoryDB) AddEmailSubscription(record api.EmailSubscriptionRecord) erro

key := [2]string{record.User, record.Addr}
if _, ok := m.emailSubRecords[key]; ok {
return ErrExists
return api.ErrExist
}
m.emailSubRecords[key] = &record
return nil
Expand All @@ -242,7 +242,7 @@ func (m *MemoryDB) UpdateEmailSubscription(record api.EmailSubscriptionRecord) e
key := [2]string{record.User, record.Addr}
old, ok := m.emailSubRecords[key]
if ok {
return ErrNotFound
return api.ErrNotFound
}
_ = old
m.emailSubRecords[key] = &record
Expand All @@ -255,7 +255,7 @@ func (m *MemoryDB) RemoveEmailSubscription(user string, addr string) error {

key := [2]string{user, addr}
if _, ok := m.emailSubRecords[key]; ok {
return ErrNotFound
return api.ErrNotFound
}
delete(m.emailSubRecords, key)
return nil
Expand All @@ -267,7 +267,7 @@ func (m *MemoryDB) ForEachEmailSubscription(cb func(*api.EmailSubscriptionRecord

for _, v := range m.emailSubRecords {
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -285,7 +285,7 @@ func (m *MemoryDB) ForEachUsersEmailSubscription(user string, cb func(*api.Email
continue
}
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -303,7 +303,7 @@ func (m *MemoryDB) ForEachEnabledEmailSubscription(cb func(*api.EmailSubscriptio
continue
}
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -318,7 +318,7 @@ func (m *MemoryDB) GetWebhook(user string, id uuid.UUID) (*api.WebhookRecord, er

record, ok := m.webhookRecords[webhookMemKey{user, id}]
if !ok {
return nil, ErrNotFound
return nil, api.ErrNotFound
}
return record, nil
}
Expand All @@ -338,7 +338,7 @@ func (m *MemoryDB) AddWebhook(record api.WebhookRecord) (err error) {

key := webhookMemKey{record.User, record.Id}
if _, ok := m.webhookRecords[key]; ok {
return ErrExists
return api.ErrExist
}
if record.Auth == nil {
record.Auth = emptyStrPtr
Expand All @@ -357,7 +357,7 @@ func (m *MemoryDB) UpdateWebhook(record api.WebhookRecord) error {
key := webhookMemKey{record.User, record.Id}
old, ok := m.webhookRecords[key]
if ok {
return ErrNotFound
return api.ErrNotFound
}
if record.Auth == nil {
record.Auth = old.Auth
Expand All @@ -376,7 +376,7 @@ func (m *MemoryDB) UpdateEnableWebhook(user string, id uuid.UUID, enabled bool)
key := webhookMemKey{user, id}
old, ok := m.webhookRecords[key]
if ok {
return ErrNotFound
return api.ErrNotFound
}
record := *old
record.Enabled = enabled
Expand All @@ -390,7 +390,7 @@ func (m *MemoryDB) RemoveWebhook(user string, id uuid.UUID) error {

key := webhookMemKey{user, id}
if _, ok := m.webhookRecords[key]; ok {
return ErrNotFound
return api.ErrNotFound
}
delete(m.webhookRecords, key)
return nil
Expand All @@ -402,7 +402,7 @@ func (m *MemoryDB) ForEachWebhook(cb func(*api.WebhookRecord) error) error {

for _, v := range m.webhookRecords {
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -420,7 +420,7 @@ func (m *MemoryDB) ForEachUsersWebhook(user string, cb func(*api.WebhookRecord)
continue
}
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand All @@ -438,7 +438,7 @@ func (m *MemoryDB) ForEachEnabledWebhook(cb func(*api.WebhookRecord) error) erro
continue
}
if err := cb(v); err != nil {
if err == ErrStopIter {
if err == api.ErrStopIter {
break
}
return err
Expand Down
Loading

0 comments on commit f2a883d

Please sign in to comment.