-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
185 lines (171 loc) · 4.75 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"database/sql"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/toddlerya/fakerfactory/faker"
)
var port = "8001"
var dbPath string = `./data/data.db`
var Conn *sql.DB = faker.CreateConn(dbPath) // 不应该在这里建立连接, 每次请求都会建立连接, 资源消耗比较多, 后续改进
func StartServer() {
// Disable Console Color, you don't need console color when writing the logs to file.
gin.DisableConsoleColor()
// Logging to a file.
// TODO 后续投入生产要考虑日志分割,日志大小等问题
f, _ := os.Create("serve.log")
// Use the following code if you need to write the logs to file and console at the same time.
// gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
gin.DefaultWriter = io.MultiWriter(f)
router := gin.Default()
router.Use(cors.Default()) // 允许任何服务ajax跨域调用
v1 := router.Group("api/v1")
{
v1.GET("/fakerfactory", GetFaker)
}
err := router.Run(fmt.Sprintf(":%s", port))
if err != nil{
log.Fatalf("在%s端口启动服务失败!", port)
}
}
func GetFaker(c *gin.Context) {
// todo: 需要对Query参数进行bind,先粗暴的判断下长度
columns := c.Query("columns")
number := c.Query("number")
// fmt.Println("columns==>", columns, len(columns))
// fmt.Println("number==>", number, len(number))
if len(columns) == 0 || len(number) == 0 {
c.JSON(http.StatusOK, gin.H{
"status": gin.H{
"status": "error",
"code": "100"},
"data": gin.H{
"count": nil,
"records": "请输入有效的参数"},
})
} else {
// startTime := time.Now()
records, count := fakerData(columns, number)
// costTime := time.Now().Sub(startTime)
// fmt.Println("构造数据耗时", costTime)
c.JSON(http.StatusOK, gin.H{
"status": gin.H{
"status": "ok",
"code": "0"},
"data": gin.H{
"count": count,
"records": records},
})
}
}
func fakerData(columns, number string) ([]map[string]interface{}, int) {
itemCols := strings.Split(columns, ",")
fakerNumber, err := strconv.Atoi(number)
if err != nil {
panic(err)
}
if fakerNumber >= 10000 {
fakerNumber = 10000
}
var results []map[string]interface{}
for i := 0; i < fakerNumber; i++ {
resultMap := make(map[string]interface{})
for _, col := range itemCols {
resultMap[col] = matchFaker(strings.ToLower(col), Conn)
}
results = append(results, resultMap)
}
count := len(results)
return results, count
}
func matchFaker(col string, c *sql.DB) interface{} {
switch col {
case "color":
return faker.Color("zh_CN")
case "job":
return faker.Job("zh_CN")
case "name":
return faker.Name("zh_CN")
case "sex":
return faker.Gender("zh_CN")
case "address":
return faker.Address(c)
case "citycode": // 中国长途区号
return faker.CityCode()
case "idcard":
return faker.IdCard()
case "age":
return faker.Age()
case "mobilephone": // 移动电话
return faker.MobilePhone("zh_CN")
case "telphone": // 固定电话
return faker.TelPhone("zh_CN")
case "specialphone": // 特殊号码,比如95555招商银行,10086中国移动
return faker.SpecialTellPhone()
case "email":
return faker.Email()
case "imid":
return faker.IMID()
case "nickname":
return faker.NickName()
case "username":
return faker.UserName()
case "password":
return faker.PassWord(true, true, true, true, true, 10)
case "website":
return faker.WebSite()
case "url":
return faker.URL()
case "airport":
return faker.AirPortInfo()
case "voyage": // 航班号
return faker.Voyage()
case "airlineinfo": // 航空公司信息(代号+名称)
return faker.AirlineInfo()
case "traintrips":
return faker.TrainTripis()
case "trainseat":
return faker.SeatOfTrain()
case "flightseat":
return faker.SeatOfFlight()
case "ipv4":
return faker.IPv4Address()
case "ipv6":
return faker.IPv6Address()
case "mac": // 暂时随机返回各种类型的MAC地址
return faker.RandMacAddress()
case "imsi": // 暂时只支持国内imsi
return faker.Imsi()
case "imei": //
return faker.Imei()
case "meid": // 随机大小写
return faker.RandMeid()
case "deviceid": //采集设备ID、固定21位、前9位为安全厂商ID(如FIBERHOME),后12位为采集设备MAC,规则同MAC、所有字母大写
return faker.DeviceID()
case "date": // 数据库日期格式{YYYYMMDD,hh:mm:ss} (当前时间)
return faker.NowDate()
case "capturetime": // 10位绝对秒(当前时间)
return faker.NowTimeStamp()
case "useragent":
return faker.UserAgent()
case "carbrand":
return faker.CarBrand("zh_CN")
case "gapassport":
return "暂未支持"
case "twpassport":
return "暂未支持"
default:
return "未知字段, 请检查输入参数"
}
}
func main() {
StartServer()
defer Conn.Close()
}