-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
100 lines (89 loc) · 2.57 KB
/
web.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
package skt
import (
"compress/gzip"
"encoding/json"
"html/template"
"io"
"net/http"
"path/filepath"
"regexp"
)
// Config 系统配置
type Config struct {
TempDir string
}
// Static 设置web静态文件目录,第一个参数是在url中的路径,第二个参数是真实文件路径
func Static(urlPath string, sysPath string) {
http.Handle(urlPath, http.StripPrefix(urlPath, http.FileServer(http.Dir(sysPath))))
}
// Render 渲染同目录下的所有文件
func Render(w http.ResponseWriter, partten string, data interface{}) error {
tempDir := "./templates/"
dir, file := filepath.Split(partten)
tb := template.New("skt").Delims("{!", "!}")
t, err := tb.ParseGlob(tempDir + dir + "*.*")
t.Delims("{!", "!}")
if err != nil {
return err
}
return t.ExecuteTemplate(w, filepath.Base(file), data)
}
// SendJSON 返回json到客户端
func SendJSON(w http.ResponseWriter, i interface{}) error {
w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Request-Method", "POST") //限制访问方式
w.Header().Set("content-type", "application/json")
return json.NewEncoder(w).Encode(i)
}
// SendJSONG 返回压缩的json到客户端
func SendJSONG(w http.ResponseWriter, i interface{}) error {
js, err := json.Marshal(i)
if err != nil {
return err
}
w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Request-Method", "POST") //限制访问方式
w.Header().Set("content-type", "application/json") //返回数据格式是json
w.Header().Set("content-encoding", "gzip")
err = ENGzip(w, js)
if err != nil {
return err
}
return nil
}
// RemoteIP 只取IP地址,去除remoteAddr的端口号
func RemoteIP(s string) string {
re := regexp.MustCompile("\\[(.*)\\]")
return re.FindString(s)
}
// DEJSON 解码object到json
func DEJSON(r io.Reader) (map[string]interface{}, error) {
var m map[string]interface{}
err := json.NewDecoder(r).Decode(&m)
return m, err
}
// DEJSONS 解码array到json
func DEJSONS(r io.Reader) ([]map[string]interface{}, error) {
var m []map[string]interface{}
err := json.NewDecoder(r).Decode(&m)
return m, err
}
// ENJSON 编码json
func ENJSON(i interface{}) ([]byte, error) {
return json.Marshal(i)
}
// ENGzip gzip压缩数据
func ENGzip(w io.Writer, b []byte) error {
x := gzip.NewWriter(w)
_, err := x.Write(b)
if err != nil {
return err
}
err = x.Flush()
if err != nil {
return err
}
return nil
}