forked from donknap/dpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·140 lines (130 loc) · 3.89 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"embed"
_ "embed"
"github.com/donknap/dpanel/app/application"
"github.com/donknap/dpanel/app/common"
"github.com/donknap/dpanel/app/common/logic"
"github.com/donknap/dpanel/common/accessor"
"github.com/donknap/dpanel/common/dao"
"github.com/donknap/dpanel/common/entity"
common2 "github.com/donknap/dpanel/common/middleware"
"github.com/donknap/dpanel/common/service/storage"
"github.com/gin-gonic/gin"
"github.com/we7coreteam/w7-rangine-go-support/src/facade"
app "github.com/we7coreteam/w7-rangine-go/src"
"github.com/we7coreteam/w7-rangine-go/src/http"
"github.com/we7coreteam/w7-rangine-go/src/http/middleware"
"io/fs"
"log/slog"
http2 "net/http"
"os"
"path/filepath"
)
var (
//go:embed config.yaml
ConfigFile []byte
//go:embed asset
Asset embed.FS
)
func main() {
// 兼容没有配置存储目录的情况
if os.Getenv("STORAGE_LOCAL_PATH") == "" {
exePath, _ := os.Executable()
os.Setenv("STORAGE_LOCAL_PATH", filepath.Dir(exePath))
}
app := app.NewApp(
app.Option{
Name: "w7-rangine-go-skeleton",
},
)
slog.Debug("config", "env", facade.GetConfig().GetString("app.env"))
slog.Debug("config", "storage", storage.Local{}.GetStorageLocalPath())
slog.Debug("config", "db", facade.GetConfig().GetString("database.default.db_name"))
// 业务中需要使用 http server,这里需要先实例化
httpServer := new(http.Provider).Register(app.GetConfig(), app.GetConsole(), app.GetServerManager()).Export()
// 注册一些全局中间件,路由或是其它一些全局操作
httpServer.Use(middleware.GetPanicHandlerMiddleware())
// 全局登录判断
httpServer.Use(common2.AuthMiddleware{}.Process)
httpServer.RegisterRouters(
func(engine *gin.Engine) {
subFs, _ := fs.Sub(Asset, "asset/static")
engine.StaticFS("/static", http2.FS(subFs))
engine.StaticFileFS("/favicon.ico", "icon.jpg", http2.FS(subFs))
engine.NoRoute(func(http *gin.Context) {
indexHtml, _ := Asset.ReadFile("asset/static/index.html")
http.Data(http2.StatusOK, "text/html; charset=UTF-8", indexHtml)
return
})
},
)
db, err := facade.GetDbFactory().Channel("default")
if err != nil {
panic(err)
}
dao.SetDefault(db)
if facade.GetConfig().GetString("app.env") != "debug" {
// 同步数据库
db.Migrator().AutoMigrate(
&entity.Event{},
&entity.Image{},
&entity.Notice{},
&entity.Registry{},
&entity.Setting{},
&entity.Site{},
&entity.SiteDomain{},
&entity.Compose{},
&entity.Backup{},
)
// 如果没有管理配置新建一条
founderSetting, _ := dao.Setting.
Where(dao.Setting.GroupName.Eq(logic.SettingUser)).
Where(dao.Setting.Name.Eq(logic.SettingUserFounder)).First()
if founderSetting == nil {
_ = dao.Setting.Create(&entity.Setting{
GroupName: logic.SettingUser,
Name: logic.SettingUserFounder,
Value: &accessor.SettingValueOption{
Password: "f6fdffe48c908deb0f4c3bd36c032e72",
Username: "admin",
},
})
}
registryRow, _ := dao.Registry.Where(dao.Registry.ServerAddress.Eq("docker.io")).First()
if registryRow == nil {
_ = dao.Registry.Create(&entity.Registry{
Title: "Docker Hub",
ServerAddress: "docker.io",
Setting: &accessor.RegistrySettingOption{
Username: "anonymous",
Proxy: []string{},
},
})
}
// 初始化挂载目录
for _, path := range []string{
"nginx/default_host",
"nginx/proxy_host",
"nginx/redirection_host",
"nginx/dead_host",
"nginx/temp",
"cert",
"storage",
"backup",
} {
err = os.MkdirAll(storage.Local{}.GetStorageLocalPath()+"/"+path, os.ModePerm)
if err != nil {
panic(err.Error())
}
}
}
// 注册资源
facade.GetContainer().NamedSingleton("asset", func() embed.FS {
return Asset
})
// 注册业务 provider,此模块中需要使用 http server 和 console
new(common.Provider).Register(httpServer)
new(application.Provider).Register(httpServer)
app.RunConsole()
}