Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit d797718

Browse files
committed
refactor the membership handlers to use the backend package
1 parent 4f719a5 commit d797718

File tree

9 files changed

+313
-356
lines changed

9 files changed

+313
-356
lines changed

account.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import (
2121
)
2222

2323
type accounts struct {
24-
membership *membership
25-
log *logger.Logger
24+
log *logger.Logger
2625
}
2726

2827
func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
@@ -158,7 +157,8 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
158157
pw = "devpw1234"
159158
}
160159

161-
if _, _, err := a.membership.createAccountAndUser(dbName, email, pw, 100); err != nil {
160+
mship := backend.Membership(base)
161+
if _, _, err := mship.CreateAccountAndUser(email, pw, 100); err != nil {
162162
http.Error(w, err.Error(), http.StatusInternalServerError)
163163
return
164164
}

backend/backend.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7+
"math/rand"
78
"strings"
89
"time"
910

@@ -26,31 +27,37 @@ import (
2627
_ "github.com/lib/pq"
2728
)
2829

29-
type Backend struct {
30-
User func(baseID string) User
31-
File func(model.Auth, model.DatabaseConfig) FileStore
32-
}
33-
3430
var (
35-
DB database.Persister
31+
// Config reflect the configuration received on Setup
32+
Config config.AppConfig
3633

37-
// Emailer sends email
34+
// DB initialized Persister data store
35+
DB database.Persister
36+
// Emailer initialized Mailer for sending emails
3837
Emailer email.Mailer
39-
// Filestore store/load/delete file
38+
// Filestore initialized Storer for raw save/delete blob file
4039
Filestore storage.Storer
41-
42-
// Cache exposes the cache / pub-sub functionalities
40+
// Cache initialized Volatilizer for cache and pub/sub
4341
Cache cache.Volatilizer
44-
// Log exposes the configured logger
42+
// Log initialized Logger for all logging
4543
Log *logger.Logger
44+
45+
// Membership exposes Account and User functionalities like register, login, etc
46+
// account and user functionalities.
47+
Membership func(model.DatabaseConfig) User
48+
49+
// Storage exposes file storage functionalities. It wraps the blob
50+
// storage as well as the database storage.
51+
Storage func(model.Auth, model.DatabaseConfig) FileStore
4652
)
4753

48-
// New prepared the core services based on the configuration received.
49-
// It returns a Backend struct that wraps the Persister interface.
50-
func New(cfg config.AppConfig) Backend {
51-
//TODO: this code is an awfuly copy of the server.go init code
52-
// Might be an idea to create some kind of helper in the internal package
53-
// that would return a structure with all the initialized services
54+
func init() {
55+
rand.Seed(time.Now().UnixNano())
56+
}
57+
58+
// Setup initializes the core services based on the configuration received.
59+
func Setup(cfg config.AppConfig) {
60+
Config = cfg
5461

5562
Log = logger.Get(cfg)
5663

@@ -129,10 +136,8 @@ func New(cfg config.AppConfig) Backend {
129136
// start system events subscriber
130137
go sub.Start()
131138

132-
return Backend{
133-
User: newUser,
134-
File: newFile,
135-
}
139+
Membership = newUser
140+
Storage = newFile
136141
}
137142

138143
func openMongoDatabase(dbHost string) (*mongodrv.Client, error) {

backend/backend_test.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
)
1212

1313
var (
14-
bkn backend.Backend
15-
1614
adminEmail string
1715
adminPassword string
1816
adminAuth model.Auth
@@ -34,7 +32,8 @@ func TestMain(t *testing.M) {
3432
LocalStorageURL: "http://localhost:8099",
3533
}
3634

37-
bkn = backend.New(cfg)
35+
// initializes all core services basesd on config
36+
backend.Setup(cfg)
3837

3938
setup()
4039

@@ -77,36 +76,21 @@ func createTenantAndDatabase() error {
7776
}
7877

7978
func createUser() error {
80-
id, err := bkn.User(base.ID).CreateAccount(adminEmail)
81-
if err != nil {
82-
return err
83-
}
84-
85-
userID, err := bkn.User(base.ID).CreateUserToken(id, adminEmail, adminPassword, 100)
79+
mship := backend.Membership(base)
80+
jwt, user, err := mship.CreateAccountAndUser(adminEmail, adminPassword, 100)
8681
if err != nil {
8782
return err
8883
}
8984

90-
tok := model.User{
91-
ID: userID,
92-
AccountID: id,
93-
Email: adminEmail,
94-
Role: 100,
95-
Created: time.Now(),
96-
}
97-
9885
adminAuth = model.Auth{
99-
AccountID: id,
100-
UserID: userID,
86+
AccountID: user.AccountID,
87+
UserID: user.ID,
10188
Email: adminEmail,
10289
Role: 100,
103-
Token: tok.Token,
90+
Token: user.Token,
10491
}
10592

106-
jwtToken, err = bkn.User(base.ID).Authenticate(adminEmail, adminPassword)
107-
if err != nil {
108-
return err
109-
}
93+
jwtToken = string(jwt)
11094

11195
return nil
11296
}

0 commit comments

Comments
 (0)