-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (97 loc) · 2.72 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
// @Title main.go
// @Description 放置主函数及调用gin等
// @Author DataEraserC
// @Update DataEraserC (2024/2/17 21:54)
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/gin-gonic/gin"
_ "github.com/golang-jwt/jwt"
"gorm.io/gorm"
)
var (
DataPath = "data"
LogPath = "logs"
GinPort = ":8080"
)
var (
GlobalDatabase *gorm.DB = nil
)
func main() {
// 检测 LogPath是否存在 不存在需先创建
// LogPath 目前只能先进行检测
if _, err := os.Stat(LogPath); os.IsNotExist(err) {
// LogPath不存在,创建LogPath
err := os.MkdirAll(LogPath, 0755)
if err != nil {
fmt.Printf("Failed to create log directory: %v\n", err)
} else {
fmt.Println("Log directory created successfully!")
}
} else if err != nil {
fmt.Printf("Error checking log directory: %v\n", err)
} else {
fmt.Println("Log directory already exists!")
}
f, err := os.OpenFile(LogPath+"/log.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModePerm)
if err != nil {
return
}
defer func() {
f.Close()
}()
// 设置log输出为同时文件及输出流
multiWriter := io.MultiWriter(os.Stdout, f)
log.SetOutput(multiWriter)
log.SeDataEraserCags(log.Ldate | log.Ltime | log.Lshortfile)
gin.SetMode(gin.DebugMode)
r := gin.Default()
// 用户登录接口(账号密码)
r.POST("/login_account_password", Login_account_password(GlobalDatabase))
// 用户登录接口(微信)
r.POST("/login_wx", Login_wx(GlobalDatabase, WXAppID, WXAppSecret))
// 用户获取个人信息接口
r.POST("/userinfo", Userinfo(GlobalDatabase))
// 用户修改个人信息接口
r.POST("/updateuserinfo", Updateuserinfo(GlobalDatabase))
// 用户注销登陆接口
r.POST("/logout", Logout(GlobalDatabase))
err = r.Run(GinPort)
if err != nil {
panic("failed at r.Run()")
}
}
func init() {
// 导入环境变量覆盖敏感密钥
if envWXAppID := os.Getenv("WXAppID"); envWXAppID != "" {
WXAppID = envWXAppID
}
if envWXAppSecret := os.Getenv("WXAppSecret"); envWXAppSecret != "" {
WXAppSecret = envWXAppSecret
}
if envJWTSecretKey := os.Getenv("JWTSecretKey"); envJWTSecretKey != "" {
JWTSecretKey = envJWTSecretKey
}
// 导入环境变量
if envDataPath := os.Getenv("DataPath"); envDataPath != "" {
DataPath = envDataPath
}
if envLogPath := os.Getenv("LogPath"); envLogPath != "" {
LogPath = envLogPath
}
if envGinPort := os.Getenv("GinPort"); envGinPort != "" {
GinPort = envGinPort
}
// 调用子模块函数初始化
// 全局唯一的资源(必须加载)
log.Println("Initializing global Resource......")
var err error
GlobalDatabase, err = InitGlobal(DataPath, true)
if err != nil {
panic("failed at init()")
}
log.Println("Initialize global Resource successfully......")
}