Skip to content

Commit 3b62833

Browse files
author
bajins
committed
分离路由函数,和添加获取go版本接口
1 parent 4e8f616 commit 3b62833

File tree

5 files changed

+203
-149
lines changed

5 files changed

+203
-149
lines changed

controller.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/gin-gonic/gin"
6+
log "github.com/sirupsen/logrus"
7+
"io"
8+
"key-gin/result"
9+
"key-gin/utils"
10+
"net/http"
11+
"os"
12+
"runtime"
13+
)
14+
15+
/**
16+
* 首页
17+
*
18+
* @Description
19+
* @author claer www.bajins.com
20+
* @date 2019/6/28 11:19
21+
*/
22+
func WebRoot(c *gin.Context) {
23+
// 301重定向
24+
//c.Redirect(http.StatusMovedPermanently, "/static")
25+
// 返回HTML页面
26+
//c.HTML(http.StatusOK, "index.html", nil)
27+
c.HTML(http.StatusOK, "index.html", gin.H{})
28+
}
29+
30+
/**
31+
* 获取系统信息
32+
*
33+
* @return
34+
* @Description
35+
* @author claer www.bajins.com
36+
* @date 2019/7/16 14:49
37+
*/
38+
func SystemInfo(c *gin.Context) {
39+
data := make(map[string]interface{}, 0)
40+
data["Version"] = utils.ToUpper(runtime.Version())
41+
data["cpu"] = runtime.NumCPU()
42+
memStatus := runtime.MemStats{}
43+
// 查看内存申请和分配统计信息
44+
runtime.ReadMemStats(&memStatus)
45+
// 申请的内存
46+
data["Mallocs"] = memStatus.Mallocs
47+
// 释放的内存次数
48+
data["Frees"] = memStatus.Frees
49+
// 获取当前函数或者上层函数的标识号、文件名、调用方法在当前文件中的行号
50+
//pc,file,line,ok := runtime.Caller(0)
51+
// 获取当前进程执行的cgo调用次数
52+
data["NumCgoCall"] = runtime.NumCgoCall()
53+
// 获取当前存在的go协程数
54+
data["NumGoroutine"] = runtime.NumGoroutine()
55+
56+
c.JSON(http.StatusOK, result.Success(200, "获取系统信息成功", data))
57+
}
58+
59+
/**
60+
* 获取key
61+
*
62+
* @author claer www.bajins.com
63+
* @date 2019/6/28 15:04
64+
*/
65+
func GetKey(c *gin.Context) {
66+
// GET 获取参数内容,没有则返回空字符串
67+
//company := c.Query("company")
68+
// POST 获取的所有参数内容的类型都是 string
69+
company := c.PostForm("company")
70+
71+
if utils.IsStringEmpty(company) {
72+
c.JSON(http.StatusOK, result.Error(300, "请选择公司"))
73+
return
74+
}
75+
app := c.PostForm("app")
76+
if utils.IsStringEmpty(app) {
77+
c.JSON(http.StatusOK, result.Error(300, "请选择产品"))
78+
return
79+
}
80+
version := c.PostForm("version")
81+
if utils.IsStringEmpty(version) {
82+
c.JSON(http.StatusOK, result.Error(300, "请选择版本"))
83+
return
84+
}
85+
// 获取当前绝对路径
86+
dir, err := os.Getwd()
87+
if err != nil {
88+
log.Error(err)
89+
c.JSON(http.StatusOK, result.Error(500, "获取key系统错误"))
90+
return
91+
}
92+
path := utils.PathStitching(dir, "pyutils")
93+
if company == "netsarang" {
94+
out, err := utils.ExecutePython(path+"/xshell_key.py", app, version)
95+
if err != nil {
96+
log.Error(err)
97+
fmt.Println(err)
98+
c.JSON(http.StatusOK, result.Error(500, "获取key系统错误"))
99+
return
100+
}
101+
res := make(map[string]string)
102+
res["key"] = out
103+
c.JSON(http.StatusOK, result.Success(200, "获取key成功", res))
104+
105+
} else if company == "mobatek" {
106+
107+
_, err := utils.ExecutePython(path+"/moba_xterm_Keygen.py", utils.OsPath(), version)
108+
if err != nil {
109+
c.JSON(http.StatusOK, result.Error(500, "获取key系统错误"))
110+
return
111+
}
112+
c.Header("Content-Type", "application/octet-stream")
113+
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "Custom.mxtpro"))
114+
//c.Writer.Header().Set("Content-Type", "application/octet-stream")
115+
//c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "Custom.mxtpro"))
116+
c.FileAttachment(utils.OsPath()+"/Custom.mxtpro", "Custom.mxtpro")
117+
118+
} else if company == "torchsoft" {
119+
out, err := utils.ExecutePython(path+"/reg_workshop_keygen.py", version)
120+
if err != nil {
121+
c.JSON(http.StatusOK, result.Error(500, "获取key系统错误"))
122+
return
123+
}
124+
res := make(map[string]string)
125+
res["key"] = out
126+
c.JSON(http.StatusOK, result.Success(200, "获取key成功", res))
127+
}
128+
129+
}
130+
131+
/**
132+
* 文件上传请求
133+
*
134+
* @author claer www.bajins.com
135+
* @date 2019/6/28 11:32
136+
*/
137+
func Upload(c *gin.Context) {
138+
// 拿到上传的文件的信息
139+
file, header, err := c.Request.FormFile("upload")
140+
filename := header.Filename
141+
fmt.Println(header.Filename)
142+
out, err := os.Create("./tmp/" + filename + ".png")
143+
if err != nil {
144+
log.Error(err)
145+
}
146+
defer out.Close()
147+
// 拷贝上传的文件信息到新建的out文件中
148+
_, err = io.Copy(out, file)
149+
if err != nil {
150+
log.Error(err)
151+
}
152+
}
153+
154+
/**
155+
* 文件下载请求
156+
*
157+
* @author claer www.bajins.com
158+
* @date 2019/6/28 11:33
159+
*/
160+
func Dowload(c *gin.Context) {
161+
response, err := http.Get(c.Request.Host + "/static/public/favicon.ico")
162+
if err != nil || response.StatusCode != http.StatusOK {
163+
c.Status(http.StatusServiceUnavailable)
164+
return
165+
}
166+
167+
extraHeaders := map[string]string{
168+
"Content-Disposition": `attachment; filename="favicon.ico"`,
169+
}
170+
171+
c.DataFromReader(http.StatusOK, response.ContentLength, response.Header.Get("Content-Type"), response.Body, extraHeaders)
172+
}

key-gin.go

Lines changed: 2 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package main
22

33
import (
44
"flag"
5-
"fmt"
65
"github.com/gin-gonic/gin"
76
log "github.com/sirupsen/logrus"
8-
"io"
9-
"key-gin/common"
107
"key-gin/utils"
118
"net/http"
129
"os"
@@ -80,136 +77,6 @@ func Cors() gin.HandlerFunc {
8077
}
8178
}
8279

83-
/**
84-
* 获取key
85-
*
86-
* @author claer www.bajins.com
87-
* @date 2019/6/28 15:04
88-
*/
89-
func getKey(c *gin.Context) {
90-
// GET 获取参数内容,没有则返回空字符串
91-
//company := c.Query("company")
92-
// POST 获取的所有参数内容的类型都是 string
93-
company := c.PostForm("company")
94-
95-
if utils.IsStringEmpty(company) {
96-
c.JSON(http.StatusOK, common.Error(300, "请选择公司"))
97-
return
98-
}
99-
app := c.PostForm("app")
100-
if utils.IsStringEmpty(app) {
101-
c.JSON(http.StatusOK, common.Error(300, "请选择产品"))
102-
return
103-
}
104-
version := c.PostForm("version")
105-
if utils.IsStringEmpty(version) {
106-
c.JSON(http.StatusOK, common.Error(300, "请选择版本"))
107-
return
108-
}
109-
// 获取当前绝对路径
110-
dir, err := os.Getwd()
111-
if err != nil {
112-
log.Error(err)
113-
c.JSON(http.StatusOK, common.Error(500, "获取key系统错误"))
114-
return
115-
}
116-
path := utils.PathStitching(dir, "pyutils")
117-
if company == "netsarang" {
118-
result, err := utils.ExecutePython(path+"/xshell_key.py", app, version)
119-
if err != nil {
120-
log.Error(err)
121-
fmt.Println(err)
122-
c.JSON(http.StatusOK, common.Error(500, "获取key系统错误"))
123-
return
124-
}
125-
res := make(map[string]string)
126-
res["key"] = result
127-
c.JSON(http.StatusOK, common.Success(200, "获取key成功", res))
128-
129-
} else if company == "mobatek" {
130-
131-
_, err := utils.ExecutePython(path+"/moba_xterm_Keygen.py", utils.OsPath(), version)
132-
if err != nil {
133-
c.JSON(http.StatusOK, common.Error(500, "获取key系统错误"))
134-
return
135-
}
136-
c.Header("Content-Type", "application/octet-stream")
137-
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "Custom.mxtpro"))
138-
//c.Writer.Header().Set("Content-Type", "application/octet-stream")
139-
//c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "Custom.mxtpro"))
140-
c.FileAttachment(utils.OsPath()+"/Custom.mxtpro", "Custom.mxtpro")
141-
142-
} else if company == "torchsoft" {
143-
result, err := utils.ExecutePython(path+"/reg_workshop_keygen.py", version)
144-
if err != nil {
145-
c.JSON(http.StatusOK, common.Error(500, "获取key系统错误"))
146-
return
147-
}
148-
res := make(map[string]string)
149-
res["key"] = result
150-
c.JSON(http.StatusOK, common.Success(200, "获取key成功", res))
151-
}
152-
153-
}
154-
155-
/**
156-
* 文件上传请求
157-
*
158-
* @author claer www.bajins.com
159-
* @date 2019/6/28 11:32
160-
*/
161-
func upload(c *gin.Context) {
162-
// 拿到上传的文件的信息
163-
file, header, err := c.Request.FormFile("upload")
164-
filename := header.Filename
165-
fmt.Println(header.Filename)
166-
out, err := os.Create("./tmp/" + filename + ".png")
167-
if err != nil {
168-
log.Error(err)
169-
}
170-
defer out.Close()
171-
// 拷贝上传的文件信息到新建的out文件中
172-
_, err = io.Copy(out, file)
173-
if err != nil {
174-
log.Error(err)
175-
}
176-
}
177-
178-
/**
179-
* 文件下载请求
180-
*
181-
* @author claer www.bajins.com
182-
* @date 2019/6/28 11:33
183-
*/
184-
func dowload(c *gin.Context) {
185-
response, err := http.Get(c.Request.Host + "/static/public/favicon.ico")
186-
if err != nil || response.StatusCode != http.StatusOK {
187-
c.Status(http.StatusServiceUnavailable)
188-
return
189-
}
190-
191-
extraHeaders := map[string]string{
192-
"Content-Disposition": `attachment; filename="favicon.ico"`,
193-
}
194-
195-
c.DataFromReader(http.StatusOK, response.ContentLength, response.Header.Get("Content-Type"), response.Body, extraHeaders)
196-
}
197-
198-
/**
199-
* 首页
200-
*
201-
* @Description
202-
* @author claer www.bajins.com
203-
* @date 2019/6/28 11:19
204-
*/
205-
func WebRoot(c *gin.Context) {
206-
// 301重定向
207-
//c.Redirect(http.StatusMovedPermanently, "/static")
208-
// 返回HTML页面
209-
//c.HTML(http.StatusOK, "index.html", nil)
210-
c.HTML(http.StatusOK, "index.html", gin.H{})
211-
}
212-
21380
/**
21481
* 获取传入参数的端口,如果没传默认值为8000
21582
*
@@ -248,7 +115,8 @@ func main() {
248115
//router.Use(Authorize())
249116

250117
// 注册接口
251-
router.POST("/getKey", getKey)
118+
router.POST("/getKey", GetKey)
119+
router.POST("/SystemInfo", SystemInfo)
252120
router.Any("/", WebRoot)
253121

254122
// 注册一个目录,gin 会把该目录当成一个静态的资源目录

static/css/index.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ a.pure-button-primary {
14171417
.legal-links,
14181418
.legal-links li {
14191419
text-align: right;
1420-
margin: 0
1420+
margin: 0;
14211421
}
14221422
}
14231423

@@ -1602,4 +1602,10 @@ pre .support.value {
16021602

16031603
.submit-button {
16041604
margin-top: 20px;
1605+
}
1606+
1607+
.version {
1608+
border-left: 1px solid #d6d6d6;
1609+
padding-left: 8px;
1610+
margin-left: 5px;
16051611
}

0 commit comments

Comments
 (0)