generated from shoriwe/base-go-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
69 lines (64 loc) · 1.42 KB
/
controller.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
package models
import (
"github.com/hidromatologia-v2/models/common/cache"
"github.com/hidromatologia-v2/models/session"
"github.com/hidromatologia-v2/models/tables"
"github.com/wneessen/go-mail"
"gorm.io/gorm"
)
type Controller struct {
DB *gorm.DB
EmailCache cache.Cache
PasswordCache cache.Cache
JWT *session.JWT
MailHost string
MailFrom string
MailOptions []mail.Option
}
func (c *Controller) Close() error {
conn, err := c.DB.DB()
if err != nil {
return err
}
return conn.Close()
}
type (
MailOptions struct {
Host string
From string
Options []mail.Option
}
Options struct {
Database *gorm.DB
EmailCache cache.Cache
PasswordCache cache.Cache
JWTSecret []byte
Mail *MailOptions
}
)
func NewController(options *Options) *Controller {
c := &Controller{}
if options.Database != nil {
c.DB = options.Database
}
if options.EmailCache != nil {
c.EmailCache = options.EmailCache
}
if options.PasswordCache != nil {
c.PasswordCache = options.PasswordCache
}
if options.JWTSecret != nil {
c.JWT = session.New(options.JWTSecret)
}
if options.Mail != nil {
c.MailHost = options.Mail.Host
c.MailFrom = options.Mail.From
c.MailOptions = options.Mail.Options
}
c.DB.AutoMigrate(
&tables.User{}, &tables.Station{}, &tables.Sensor{},
&tables.SensorRegistry{}, &tables.Alert{}, &tables.Admin{},
&tables.Message{},
)
return c
}