/
main.go
93 lines (78 loc) · 2.13 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path"
"github.com/goodplayer/Princess/api/controller"
"github.com/goodplayer/Princess/config"
"github.com/goodplayer/Princess/domain"
"github.com/goodplayer/Princess/framework/app"
"github.com/goodplayer/Princess/framework/ginplugin"
"github.com/goodplayer/Princess/framework/ginsupport"
"github.com/goodplayer/Princess/repository"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
func init() {
ROOT_PATH, _ := os.Getwd()
config.InitConfig(config.Load(path.Join(ROOT_PATH, "princess.conf")))
fmt.Println(config.GlobalConfig())
}
func main() {
ac := app.NewAppContainer()
r := gin.New()
dsn := "sqlserver://sa:P@ssw0rdP@ssw0rd@192.168.31.232:1433?database=princess"
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
ac.Set(repository.NewDb(db))
if err := initProcess(r, ac); err != nil {
panic(err)
}
// start container
ac.StartContainer()
if gin.IsDebugging() {
log.Printf("[GIN-debug] Listening and serving HTTP on %s\n", config.GlobalConfig().HttpConfig.Bind)
}
defer func() {
if err != nil && gin.IsDebugging() {
log.Printf("[GIN-debug] [ERROR] %v\n", err)
}
}()
server := &http.Server{
Addr: config.GlobalConfig().HttpConfig.Bind,
Handler: r,
}
if err = server.ListenAndServe(); err != nil {
panic(err)
}
}
func initProcess(r *gin.Engine, ac *app.ApplicationContainer) error {
if err := domain.Init(ac); err != nil {
return err
}
// init middleware before registering controllers
// including custom function in order for template to use
initMiddleware(r)
if gin.IsDebugging() {
ginsupport.InitHtmlTemplateFromFolderWithDebugReloading(r, "templates", "html")
} else {
ginsupport.InitHtmlTemplateFromFolder(r, "templates", "html")
}
// load controller
if err := controller.InitController(r, ac); err != nil {
return err
}
return nil
}
func initMiddleware(r *gin.Engine) {
r.Use(gin.Recovery(), gin.Logger())
//init session
store := ginplugin.NewMemSessionStore()
s := ginplugin.NewSessionDomainService("Princess", 3600*24*30, store)
r.Use(ginplugin.SessionHandler(s))
}