-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
61 lines (49 loc) · 1.31 KB
/
core.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
package asclepius
import (
"context"
"fmt"
"github.com/facebookincubator/ent/dialect"
"github.com/minskylab/asclepius/config"
"github.com/minskylab/asclepius/ent"
"github.com/minskylab/asclepius/ent/migrate"
"github.com/pkg/errors"
_ "github.com/go-sql-driver/mysql"
)
type Core struct {
client *ent.Client
}
func NewCore(config *config.GlobalConfig) (*Core, error) {
core := new(Core)
if err := core.openClient(config); err != nil {
return nil, err
}
return core, nil
}
func (core *Core) openClient(config *config.GlobalConfig) error {
var err error
user := config.Storage.User
pass := config.Storage.Password
endpoint := config.Storage.Endpoint
database := config.Storage.DatabaseName
// e.g. "root:pass@tcp(localhost:3306)/test"
uri :=fmt.Sprintf("%s:%s@tcp(%s)/%s", user, pass, endpoint, database)
core.client, err = ent.Open(dialect.MySQL, uri )
if err != nil {
return errors.Wrap(err, "failed opening connection to mysql database")
}
if err = core.client.Schema.Create(
context.Background(),
migrate.WithDropIndex(true),
migrate.WithDropColumn(true),
migrate.WithGlobalUniqueID(true),
); err != nil {
return errors.Wrap(err, "error at create schema")
}
return nil
}
func (core *Core) done() error {
return core.client.Close()
}
func (core *Core) Done() error {
return core.done()
}