Move backends to separate folder

This commit is contained in:
nxshock 2019-07-20 21:33:02 +05:00
parent a99393f7a6
commit 25bc8f82fd
3 changed files with 32 additions and 30 deletions

View File

@ -3,7 +3,8 @@ package main
import ( import (
"log" "log"
internal "github.com/nxshock/signaller/internal" "github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/backends/memory"
) )
var ( var (
@ -13,7 +14,7 @@ var (
func init() { func init() {
server = internal.New() server = internal.New()
server.Address = "localhost" server.Address = "localhost"
server.Backend = internal.NewMemoryBackend() server.Backend = memory.NewBackend()
server.Backend.Register("andrew", "1", "") server.Backend.Register("andrew", "1", "")
} }

View File

@ -1,4 +1,4 @@
package internal package memory
import ( import (
"encoding/json" "encoding/json"
@ -6,6 +6,7 @@ import (
"os" "os"
"sync" "sync"
"github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/models" "github.com/nxshock/signaller/internal/models"
"github.com/nxshock/signaller/internal/models/common" "github.com/nxshock/signaller/internal/models/common"
mSync "github.com/nxshock/signaller/internal/models/sync" mSync "github.com/nxshock/signaller/internal/models/sync"
@ -13,7 +14,7 @@ import (
var first bool var first bool
type MemoryBackend struct { type Backend struct {
data map[string]*User data map[string]*User
mutex sync.Mutex // TODO: replace with RW mutex mutex sync.Mutex // TODO: replace with RW mutex
} }
@ -27,21 +28,21 @@ type Token struct {
Device string Device string
} }
func NewMemoryBackend() *MemoryBackend { func NewBackend() *Backend {
return &MemoryBackend{data: make(map[string]*User)} return &Backend{data: make(map[string]*User)}
} }
func (memoryBackend MemoryBackend) Register(username, password, device string) (token string, err *models.ApiError) { func (backend Backend) Register(username, password, device string) (token string, err *models.ApiError) {
memoryBackend.mutex.Lock() backend.mutex.Lock()
defer memoryBackend.mutex.Unlock() defer backend.mutex.Unlock()
if _, ok := memoryBackend.data[username]; ok { if _, ok := backend.data[username]; ok {
return "", NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken") return "", internal.NewError(models.M_USER_IN_USE, "trying to register a user ID which has been taken")
} }
token = newToken(DefaultTokenSize) token = internal.NewToken(internal.DefaultTokenSize)
memoryBackend.data[username] = &User{ backend.data[username] = &User{
Password: password, Password: password,
Tokens: map[string]Token{ Tokens: map[string]Token{
token: { token: {
@ -50,31 +51,31 @@ func (memoryBackend MemoryBackend) Register(username, password, device string) (
return token, nil return token, nil
} }
func (memoryBackend MemoryBackend) Login(username, password, device string) (token string, err *models.ApiError) { func (backend Backend) Login(username, password, device string) (token string, err *models.ApiError) {
memoryBackend.mutex.Lock() backend.mutex.Lock()
defer memoryBackend.mutex.Unlock() defer backend.mutex.Unlock()
user, ok := memoryBackend.data[username] user, ok := backend.data[username]
if !ok { if !ok {
return "", NewError(models.M_FORBIDDEN, "wrong username") return "", internal.NewError(models.M_FORBIDDEN, "wrong username")
} }
if user.Password != password { if user.Password != password {
return "", NewError(models.M_FORBIDDEN, "wrong password") return "", internal.NewError(models.M_FORBIDDEN, "wrong password")
} }
token = newToken(DefaultTokenSize) token = internal.NewToken(internal.DefaultTokenSize)
memoryBackend.data[username].Tokens[token] = Token{Device: device} backend.data[username].Tokens[token] = Token{Device: device}
return token, nil return token, nil
} }
func (memoryBackend MemoryBackend) Logout(token string) *models.ApiError { func (backend Backend) Logout(token string) *models.ApiError {
memoryBackend.mutex.Lock() backend.mutex.Lock()
defer memoryBackend.mutex.Unlock() defer backend.mutex.Unlock()
for _, user := range memoryBackend.data { for _, user := range backend.data {
for userToken, _ := range user.Tokens { for userToken, _ := range user.Tokens {
if userToken == token { if userToken == token {
delete(user.Tokens, token) delete(user.Tokens, token)
@ -83,12 +84,12 @@ func (memoryBackend MemoryBackend) Logout(token string) *models.ApiError {
} }
} }
return NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct return internal.NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct
} }
func (memoryBackend MemoryBackend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) { func (backend Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) {
memoryBackend.mutex.Lock() backend.mutex.Lock()
defer memoryBackend.mutex.Unlock() defer backend.mutex.Unlock()
log.Println(request) log.Println(request)

View File

@ -8,7 +8,7 @@ import (
) )
// newToken returns new generated token with specified length // newToken returns new generated token with specified length
func newToken(size int) string { func NewToken(size int) string {
b := make([]byte, size) b := make([]byte, size)
rand.Read(b) rand.Read(b)