/
server.go
104 lines (88 loc) · 2.64 KB
/
server.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
package core
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"go.uber.org/zap"
"github.com/gin-gonic/gin"
"github.com/nooncall/owls/go/global"
"github.com/nooncall/owls/go/initialize"
"github.com/nooncall/owls/go/service/system"
"github.com/nooncall/owls/go/utils/logger"
)
type server interface {
ListenAndServe() error
}
func RunWindowsServer() {
// 从db加载jwt数据
if global.GVA_DB != nil {
system.LoadAll()
}
Router := gin.Default()
initialize.Routers(Router)
Router.Static("/form-generator", "./resource/page")
frontRouter(Router)
docRouter(Router)
address := fmt.Sprintf(":%d", global.GVA_CONFIG.System.Addr)
s := initServer(address, Router)
// 保证文本顺序输出
// In order to ensure that the text order output can be deleted
time.Sleep(10 * time.Microsecond)
global.GVA_LOG.Info("server run success on ", zap.String("address", address))
fmt.Printf(`
欢迎使用 github.com/nooncall/owls/go
当前版本:V0.0.1
加群方式:微信号:xxx QQ群:xxxx
默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
默认前端文件运行地址:http://127.0.0.1:8080
如果项目让您获得了收益,那就帮忙宣传一下吧!
`, address)
global.GVA_LOG.Error(s.ListenAndServe().Error())
}
func docRouter(r *gin.Engine) {
currentDir := getCurrentDirectory()
// currentDir = "/Users/mingbai/openS/vue/database-manager/bin"
logger.Info("doc router current dir is: ", currentDir)
r.Static("/docs", filepath.Join(currentDir, "./docs-static"))
r.GET("/doc", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "docs/")
})
r.NoRoute(func(c *gin.Context) {
if !strings.Contains(c.Request.RequestURI, "/api") {
path := strings.Split(c.Request.URL.Path, "/")
if len(path) > 1 {
c.File(filepath.Join(currentDir, "./docs-static") + "/index.html")
return
}
}
})
}
func frontRouter(r *gin.Engine) {
currentDir := getCurrentDirectory()
// currentDir = "/Users/mingbai/openS/vue/database-manager/bin"
logger.Info("current dir is: ", currentDir)
r.Static("/owls", filepath.Join(currentDir, "./static"))
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "owls/")
})
r.NoRoute(func(c *gin.Context) {
if !strings.Contains(c.Request.RequestURI, "/api") {
path := strings.Split(c.Request.URL.Path, "/")
if len(path) > 1 {
c.File(filepath.Join(currentDir, "./static") + "/index.html")
return
}
}
})
}
func getCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
logger.Errorf("get current dir err: %s", err.Error())
return ""
}
return strings.Replace(dir, "\\", "/", -1)
}