mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-23 19:02:19 +00:00
feat: Implement basic DiskBackend struct with constructor (for now it isn't implementing Backend interface)
This commit is contained in:
parent
e5b4e156e9
commit
929239cc5b
70
internal/backends/disk/backend.go
Normal file
70
internal/backends/disk/backend.go
Normal file
@ -0,0 +1,70 @@
|
||||
package disk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sync"
|
||||
|
||||
"github.com/HouzuoGuo/tiedot/db"
|
||||
)
|
||||
|
||||
const (
|
||||
DatabasePath = "signaller.db" // TODO: make changing the path of database
|
||||
UsersColName = "users"
|
||||
RoomsColName = "rooms"
|
||||
EventsColName = "events"
|
||||
RoomAliasesColName = "roomAliases"
|
||||
)
|
||||
|
||||
func createCollections(database *db.DB) {
|
||||
if err := database.Create(UsersColName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := database.Create(RoomsColName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := database.Create(EventsColName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := database.Create(RoomAliasesColName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func defaultValidationUsernameFunc(userName string) error {
|
||||
const re = `^\w{5,}$`
|
||||
|
||||
if !regexp.MustCompile(re).MatchString(userName) {
|
||||
return fmt.Errorf("username does not match %s", re)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type DiskBackend struct {
|
||||
currentDatabase *db.DB
|
||||
users *db.Col
|
||||
rooms *db.Col
|
||||
events *db.Col
|
||||
roomAliases *db.Col
|
||||
hostname string
|
||||
validateUsernameFunc func(string) error // TODO: create ability to redefine validation func
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewDiskBackend(hostname string) *DiskBackend {
|
||||
database, err := db.OpenDB(DatabasePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
createCollections(database)
|
||||
|
||||
return &DiskBackend{
|
||||
currentDatabase: database,
|
||||
users: database.Use(UsersColName),
|
||||
rooms: database.Use(RoomsColName),
|
||||
events: database.Use(EventsColName),
|
||||
roomAliases: database.Use(RoomAliasesColName),
|
||||
hostname: hostname,
|
||||
validateUsernameFunc: defaultValidationUsernameFunc}
|
||||
}
|
Loading…
Reference in New Issue
Block a user