-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
300 lines (266 loc) · 8.11 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
"github.com/iris-contrib/middleware/cors"
//"github.com/kataras/iris/mvc"
//"gopkg.in/go-playground/validator.v9"
"github.com/kataras/iris/sessions"
"gosync/library"
)
var sessionManager *sessions.Sessions
func main() {
var cookieNameForSessionID = "gosync_sessionid"
sessionManager = sessions.New(sessions.Config{Cookie: cookieNameForSessionID,Expires:60*time.Minute})
//current_path, _ := filepath.Abs(`.`)
var GOPATH = os.Getenv("GOPATH")
arg_list := os.Args
var config_file string
var PROJECT_DIR = GOPATH + "/src/gosync/"
if len(arg_list) >1 {
config_file = arg_list[1]
}else {
config_file = PROJECT_DIR + "config/main.tml"
}
if !library.IsFile(config_file) {
fmt.Println("config file: "+config_file+" not exists")
os.Exit(1)
}
var(
config *library.TomlConfig
err error
)
if config, err = library.ReadConf(config_file); err != nil {
checkErr(err)
}
var FAB_FILE = config.Fabric.FabFile
log_path := config.Logs.LogPath
static_path := config.Static.StaticPath
view_path := config.Views.ViewPath
server_host := config.Server.Host
server_port := config.Server.Port
server_port_str := strconv.FormatInt(int64(server_port),10)
host_url := "http://"+server_host+":"+server_port_str
if !library.IsDir(static_path) {
fmt.Println("static path: "+static_path+" not exists")
os.Exit(1)
}
if !library.IsDir(view_path) {
fmt.Println("views path: "+view_path+" not exists")
os.Exit(1)
}
if library.Substr(log_path,-1,1) != "/" {
log_path = log_path + "/"
}
if library.Substr(view_path,-1,1) != "/" {
view_path = view_path + "/"
}
app := iris.New()
// Recover middleware recovers from any panics and writes a 500 if there was one.
app.Use(recover.New())
requestLogger := logger.New(logger.Config{
Status: true,
IP: true,
Method: true,
Path: true,
Query: true,
// if !empty then its contents derives from `ctx.Values().Get("logger_message")
// will be added to the logs.
MessageContextKeys: []string{"logger_message"},
// if !empty then its contents derives from `ctx.GetHeader("User-Agent")
MessageHeaderKeys: []string{"User-Agent"},
})
app.Use(requestLogger)
f := newLogFile(log_path)
defer f.Close()
app.Logger().SetOutput(f)
//错误处理handle 404 500
app.OnErrorCode(iris.StatusNotFound, notFound)
app.OnErrorCode(iris.StatusInternalServerError, internalServerError)
app.Use(before)
// 注册 "after" ,在所有路由的处理程序之后调用
app.Done(after)
crs := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:8081"}, //允许通过的主机名称
AllowCredentials: true,
})
app.StaticWeb("/static", static_path)
app.RegisterView(iris.HTML(view_path, ".html"))
app.Get("/", func(ctx iris.Context) {
session := sessionManager.Start(ctx)
username := session.GetString("username")
if username=="" {
ctx.Redirect("/login",http.StatusFound)
}
ctx.ViewData("username", username)
ctx.ViewData("host_url", host_url)
ctx.ViewData("title", "gosync上线系统")
ctx.View("index.html")
},crs)
app.Get("/login", func(ctx iris.Context) {
session := sessionManager.Start(ctx)
username := session.GetString("username")
if username!="" {
ctx.Redirect("/",http.StatusFound)
}
ctx.ViewData("host_url", host_url)
ctx.View("login.html")
})
app.Get("/logout",func(ctx iris.Context){
session := sessionManager.Start(ctx)
session.Delete("username")
time.Sleep(time.Duration(500) * time.Millisecond)
ctx.Redirect("/login",http.StatusFound)
})
app.Post("/api/login", func(ctx iris.Context) {
username := ctx.FormValue("username")
password := ctx.FormValue("password")
real_user := "gosync"
real_pwd := "levsion"
customer_user := "test"
customer_pwd := "wangtuo888"
h5_user := "test_h5"
h5_pwd := "wangtuo999"
if ((username!= real_user || password!= real_pwd) && (username!= customer_user || password!= customer_pwd) && (username!= h5_user || password!= h5_pwd)) {
ctx.WriteString("Error: 用户密码错误 !!!")
return
}
session := sessionManager.Start(ctx)
session.Set("username", username)
ctx.WriteString("登录成功")
})
api := app.Party("/api",corsAll)
{
api.Get("/tag_list/{project:string}",func(ctx iris.Context){
project := ctx.Params().Get("project")
rs,err := exec_shell("fab -r "+FAB_FILE+" go-tag-list "+project)
if err !=nil {
ctx.WriteString(err.Error())
}else {
sr := strings.Trim(rs,"\n")
//创建切片
tag_list := make([]string,0,1)
tag_list = strings.Split(sr,"\n")
library.ReverseArray(tag_list)
rs = strings.Join(tag_list,"\n")
ctx.WriteString(rs)
}
})
api.Get("/create_tag/{project:string}/{tag:string}",func(ctx iris.Context){
project := ctx.Params().Get("project")
tag := ctx.Params().Get("tag")
tag = strings.Trim(tag," ")
rs,err := exec_shell("fab -r "+FAB_FILE+" create-tag "+project+" "+tag)
if err !=nil {
ctx.WriteString(err.Error())
}else {
ctx.WriteString(rs)
}
})
api.Get("/testing/{project:string}",func(ctx iris.Context){
project := ctx.Params().Get("project")
rs,err := exec_shell("fab -r "+FAB_FILE+" testing "+project)
if err !=nil {
ctx.WriteString(err.Error())
}else {
ctx.WriteString(rs)
}
})
api.Get("/deploy/{project:string}",func(ctx iris.Context){
project := ctx.Params().Get("project")
rs,err := exec_shell("fab -r "+FAB_FILE+" deploy "+project)
if err !=nil {
ctx.WriteString(err.Error())
}else {
ctx.WriteString(rs)
}
})
api.Get("/rollback/{project:string}",func(ctx iris.Context){
project := ctx.Params().Get("project")
rs,err := exec_shell("fab -r "+FAB_FILE+" rollback "+project)
if err !=nil {
ctx.WriteString(err.Error())
}else {
ctx.WriteString(rs)
}
})
}
_ = app.Run(iris.Addr(":"+server_port_str),iris.WithConfiguration(iris.TOML(config_file)))
}
func newLogFile(log_path string) *os.File {
filename := log_path +strconv.Itoa(time.Now().Year()) + time.Now().Month().String() + strconv.Itoa(time.Now().Day()) +".log"
// Open the file, this will append to the today's file if server restarted.
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
return f
}
func before(ctx iris.Context) {
//
//fmt.Println("before request ")
ctx.Next()
}
func after(ctx iris.Context) {
//fmt.Println("after request ")
ctx.Next()
}
func notFound(ctx iris.Context) {
// 出现 404 的时候,就跳转到 $views_dir/errors/404.html 模板
//ctx.View("errors/404.html")
ctx.WriteString("The page not found !!!")
}
func internalServerError(ctx iris.Context) {
ctx.WriteString("Oups something went wrong, try again")
}
//阻塞式的执行外部shell命令的函数,等待执行完毕并返回标准输出
func exec_shell(s string) (string, error){
//函数返回一个*Cmd,用于使用给出的参数执行name指定的程序
cmd := exec.Command("/bin/bash", "-c", s)
//读取io.Writer类型的cmd.Stdout,再通过bytes.Buffer(缓冲byte类型的缓冲器)将byte类型转化为string类型(out.String():这是bytes类型提供的接口)
var out bytes.Buffer
cmd.Stdout = &out
//Run执行c包含的命令,并阻塞直到完成。 这里stdout被取出,cmd.Wait()无法正确获取stdin,stdout,stderr,则阻塞在那了
err := cmd.Run()
checkErr(err)
return out.String(), err
}
//错误处理函数
func checkErr(err error) {
if err != nil {
fmt.Println(err)
panic(err)
}
}
func corsAll(ctx iris.Context) {
//cors.AllowAll()
session := sessionManager.Start(ctx)
username := session.GetString("username")
if username=="" {
ctx.WriteString("Error: 请先登录 !!!")
return
}
//check user permission
ss := ctx.RequestPath(false);
r,_:=regexp.Compile("/api/testing.+")
if(username !="gosync" && !r.MatchString(ss)) {
ctx.WriteString("Error: 您没有权限 !!!")
return
}
rr,_:= regexp.Compile("/api/testing/.*h5.*")
if(username =="test_h5" && !rr.MatchString(ss)) {
ctx.WriteString("Error: 您没有权限 !!!")
return
}
ctx.Next()
}