-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontroller.go
146 lines (117 loc) · 3.42 KB
/
controller.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
package foolgo
import (
//"fmt"
"mime/multipart"
"net/http"
)
type Controller struct {
request *Request
response *Response
view *View
}
type FGController interface {
RegRouter() map[string]interface{}
}
func (this *Controller) Init(request *Request, response *Response) bool {
this.request = request
this.response = response
this.view = NewView()
return true
}
// No use just implement FGController
func (this *Controller) RegRouter() map[string]interface{} {
return nil
}
func (this *Controller) Param(key string, default_value ...string) string {
v := this.request.Param(key)
if v == "" && default_value != nil {
return default_value[0]
}
return v
}
func (this *Controller) Assign(key interface{}, value interface{}) {
this.view.Assign(key, value)
}
func (this *Controller) Display(view_path ...string) {
bytes, err := this.Render(view_path...)
if err == nil {
this.response.Header("Content-Type", "text/html; charset=utf-8")
this.response.Body(bytes)
} else {
this.response.Header("Content-Type", "text/html; charset=utf-8")
this.response.Body([]byte(err.Error()))
}
}
func (this *Controller) Render(view_path ...string) ([]byte, error) {
var view_name string
if view_path == nil || view_path[0] == "" {
view_name = this.request.GetController() + "/" + this.request.GetAction()
} else {
view_name = view_path[0]
}
return this.view.Render(view_name)
}
func (this *Controller) Cookie(name string) string {
return this.request.Cookie(name)
}
func (this *Controller) Uri() string {
return this.request.Uri()
}
func (this *Controller) Url() string {
return this.request.Url()
}
func (this *Controller) IP() string {
return this.request.IP()
}
func (this *Controller) Scheme() string {
return this.request.Scheme()
}
func (this *Controller) Header(key string) string {
return this.request.Header(key)
}
func (this *Controller) SetHeader(key, value string) {
this.response.Header(key, value)
}
func (this *Controller) SetCookie(name string, value string, others ...interface{}) {
this.response.Cookie(name, value, others...)
}
func (this *Controller) OutString(bytes []byte) {
this.response.Header("Content-Type", "text/html; charset=utf-8")
this.response.Body(bytes)
}
func (this *Controller) Json(data interface{}, coding ...bool) error {
return this.response.Json(data, coding...)
}
func (this *Controller) Jsonp(callback string, data interface{}, coding ...bool) error {
return this.response.Jsonp(callback, data, coding...)
}
func (this *Controller) Method() string {
return this.request.Method()
}
//获取所有get变量
func (this *Controller) GET() map[string]string {
return this.request.ParamGet()
}
//获取所有post提交变量
func (this *Controller) POST() map[string]interface{} {
return this.request.ParamPost()
}
func (this *Controller) Location(url string) {
http.Redirect(this.response.Writer, this.request.request, url, 301)
}
// 获取所有上传文件
// files, _ := this.GetUploadFiles("user_icon")
// for i, _ := range files {
// file, _ := files[i].Open()
// defer file.Close()
// log.Print(this.GetFileSize(&file))
// }
func (this *Controller) GetUploadFiles(key string) ([]*multipart.FileHeader, error) {
return this.request.GetUploadFiles(key)
}
func (this *Controller) MoveUploadFile(fromfile, tofile string) error {
return this.request.MoveUploadFile(fromfile, tofile)
}
func (this *Controller) GetFileSize(file *multipart.File) int64 {
return this.request.GetFileSize(file)
}