-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
57 lines (51 loc) · 1.17 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
package controller
import (
"context"
firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
"github.com/shoriwe/message-api/common/random"
"github.com/shoriwe/message-api/common/sqlite"
"github.com/shoriwe/message-api/models"
"github.com/shoriwe/message-api/session"
"google.golang.org/api/option"
"gorm.io/gorm"
)
type Controller struct {
DB *gorm.DB
JWT *session.JWT
App *firebase.App
Client *messaging.Client
}
func (c *Controller) Close() error {
conn, err := c.DB.DB()
if err != nil {
return err
}
return conn.Close()
}
func New(db *gorm.DB, j *session.JWT, app *firebase.App) *Controller {
db.AutoMigrate(
&models.User{}, &models.Device{}, &models.Message{},
&models.MessageResponse{},
)
client, cErr := app.Messaging(context.Background())
if cErr != nil {
panic(cErr)
}
return &Controller{
DB: db,
JWT: j,
App: app,
Client: client,
}
}
func NewMem() *Controller {
config := firebase.Config{
ProjectID: random.String(),
}
app, nErr := firebase.NewApp(context.Background(), &config, option.WithoutAuthentication())
if nErr != nil {
panic(nErr)
}
return New(sqlite.NewMem(), session.NewDefault(), app)
}