-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.application.go
114 lines (89 loc) · 2.59 KB
/
sample.application.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"log"
"github.com/markdicksonjr/nibbler/user"
"github.com/markdicksonjr/nibbler"
"github.com/markdicksonjr/nibbler/database/sql"
"github.com/markdicksonjr/nibbler/database/elasticsearch"
NibUserElastic "github.com/markdicksonjr/nibbler/user/database/elastic"
NibUserSql "github.com/markdicksonjr/nibbler/user/database/sql"
)
type UserAndDbExtensions struct {
UserExtension *user.Extension
UserPersistenceExtension user.PersistenceExtension
DbExtension nibbler.Extension
}
func allocateSqlExtensions() UserAndDbExtensions {
// prepare models for initialization
var models []interface{}
models = append(models, user.User{})
// allocate an SQL extension, providing the models for auto-migration
sqlExtension := sql.Extension{ Models: models }
sqlUserExtension := NibUserSql.Extension{
SqlExtension: &sqlExtension,
}
return UserAndDbExtensions{
DbExtension: &sqlExtension,
UserExtension: &user.Extension{
PersistenceExtension: &sqlUserExtension,
},
UserPersistenceExtension: &sqlUserExtension,
}
}
func allocateEsExtensions() UserAndDbExtensions {
elasticExtension := elasticsearch.Extension{}
elasticUserExtension := NibUserElastic.Extension{
ElasticExtension: &elasticExtension,
}
return UserAndDbExtensions{
DbExtension: &elasticExtension,
UserPersistenceExtension: &elasticUserExtension,
UserExtension: &user.Extension{
PersistenceExtension: &elasticUserExtension,
},
}
}
func main() {
// allocate logger and configuration
var logger nibbler.Logger = nibbler.DefaultLogger{}
config, err := nibbler.LoadConfiguration(nil)
sqlExtensions := allocateSqlExtensions()
// prepare extension(s) for initialization
extensions := []nibbler.Extension{
sqlExtensions.DbExtension,
sqlExtensions.UserPersistenceExtension,
sqlExtensions.UserExtension,
}
// initialize the application context
appContext := nibbler.Application{}
err = appContext.Init(config, &logger, &extensions)
if err != nil {
log.Fatal(err.Error())
}
// create a test user
emailVal := "test@example.com"
_, errCreate := sqlExtensions.UserExtension.Create(&user.User{
Email: &emailVal,
})
if errCreate != nil {
log.Fatal(errCreate.Error())
}
uV, errFind := sqlExtensions.UserExtension.GetUserByEmail(emailVal)
if errFind != nil {
log.Fatal(errFind.Error())
}
log.Println(uV)
firstName := "testfirst"
lastName := "testlast"
uV.FirstName = &firstName
uV.LastName = &lastName
err = sqlExtensions.UserExtension.Update(uV)
if err != nil {
log.Fatal(err.Error())
}
// start the app
err = appContext.Run()
if err != nil {
log.Fatal(err.Error())
}
}