-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
70 lines (62 loc) · 1.83 KB
/
app.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
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/ac-kurniawan/mychat/adaptor/gorm"
"github.com/ac-kurniawan/mychat/adaptor/repository"
"github.com/ac-kurniawan/mychat/adaptor/util"
"github.com/ac-kurniawan/mychat/core"
"github.com/ac-kurniawan/mychat/interface/http"
"github.com/ac-kurniawan/mychat/library"
)
type MychatApp struct {
Env string `mapstructure:"env"`
AppName string `mapstructure:"appName"`
HttpServer struct {
Port string `mapstructure:"port"`
} `mapstructure:"httpServer"`
Trace struct {
Enable bool `mapstructure:"enable"`
HostExporter string `mapstructure:"hostExporter"`
ApiKey string `mapstructure:"apiKey"`
} `mapstructure:"trace"`
SQLite struct {
RunMigration bool `mapstructure:"runMigration"`
FilePath string `mapstructure:"filePath"`
} `mapstructure:"sqlite"`
}
func (t MychatApp) Init() {
trace := library.NewAppTrace(context.Background(), t.Trace.Enable, t.Trace.HostExporter, t.Trace.ApiKey, t.AppName, "", t.Env)
log := library.NewAppLog(false)
utilCore := util.NewUtil(util.Util{
AppTrace: trace,
AppLog: log,
})
sqlite := library.NewGormSqliteConnection(t.SQLite.FilePath)
DB := gorm.NewGormDB(gorm.GormDB{
Gorm: sqlite,
Trace: trace,
}, t.SQLite.RunMigration)
repository := repository.NewRepository(repository.Repository{
IChatDB: DB,
})
service := core.NewMychatService(core.MychatService{
Util: utilCore,
Repository: repository,
})
httpServer := http.NewHttpServer(http.HttpServer{
Port: t.HttpServer.Port,
Service: service,
Trace: trace,
})
httpServer.Start()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
// a timeout of 10 seconds to shut down the server
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
httpServer.Stop(ctx)
}