-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
109 lines (99 loc) · 2.79 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/yuedun/zhuque/db"
"github.com/yuedun/zhuque/pkg/permission"
"github.com/yuedun/zhuque/pkg/project"
"github.com/yuedun/zhuque/pkg/role"
"github.com/yuedun/zhuque/pkg/task"
"github.com/yuedun/zhuque/pkg/user"
"github.com/yuedun/zhuque/router"
"github.com/yuedun/zhuque/util"
)
// 全局配置
var Conf *util.Config
func init() {
var err error
Conf, err = util.GetConf("./conf.yaml")
util.Conf = Conf
if err != nil {
log.Println("failed to open conf.yaml")
panic(err)
}
db.DB, err = gorm.Open(Conf.Dialects, Conf.Dbpath)
if err != nil {
log.Println("failed to connect database")
panic(err)
}
db.DB.AutoMigrate(&user.User{})
db.DB.AutoMigrate(&user.UserProject{})
db.DB.AutoMigrate(&project.Project{})
db.DB.AutoMigrate(&task.Task{})
db.DB.AutoMigrate(&permission.Permission{})
db.DB.AutoMigrate(&role.Role{})
db.DB.AutoMigrate(&role.RolePermission{})
db.DB.LogMode(true)
//Db.SingularTable(true) // 如果设置为true,`User`的默认表名为`user`,使用`TableName`设置的表名不受影响
//defer Db.Close()
initData()
}
func initData() {
users := make([]user.User, 0)
if err := db.DB.Model(&user.User{}).Find(&users).Error; err != nil {
log.Println(err)
} else {
log.Println(">>>>>>>>", users)
if len(users) == 0 {
userService := user.NewService(db.DB)
user := user.User{}
user.UserName = "test"
user.Password = util.GetMD5(user.UserName)
user.Status = 1
user.CreatedAt = time.Now()
userService.CreateUser(&user)
}
}
}
func main() {
log.SetFlags(log.Llongfile | log.LstdFlags) //输出行号
r := gin.Default()
r.Static("/fe", "./fe")
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusPermanentRedirect, "/fe")
})
util.SocketEvent(r)
router.Register(r)
port := Conf.Port
if port == "" {
port = "8090"
}
log.Println("环境变量env:", util.Conf.Env, "http://localhost:"+port)
r.Run(":" + port) // listen and serve on 0.0.0.0:8090
}
// 以下代码为go:embed打包方式,将静态文件一同打包到可执行文件中
// //go:embed fe/*
// var f embed.FS
// func main() {
// log.SetFlags(log.Llongfile | log.LstdFlags) //输出行号
// r := gin.Default()
// templ := template.Must(template.New("").ParseFS(f, "fe/index.html"))
// r.SetHTMLTemplate(templ)
// r.StaticFS("/static", http.FS(f))
// r.GET("/", func(c *gin.Context) {
// c.HTML(http.StatusOK, "index.html", gin.H{})
// })
// util.SocketEvent(r)
// router.Register(r)
// port := Conf.Port
// if port == "" {
// port = "8090"
// }
// log.Println("环境变量env:", util.Conf.Env, "http://localhost:"+port)
// r.Run(":" + port) // listen and serve on 0.0.0.0:8090
// }