-
Notifications
You must be signed in to change notification settings - Fork 18
/
conf.go
106 lines (89 loc) · 2.33 KB
/
conf.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
package compress
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"os"
)
var (
TmpPath = "tmp"
JsFilters = []Filter{ClosureFilter}
CssFilters = []Filter{YuiFilter}
JsTagTemplate, _ = template.New("").Parse(`<script type="text/javascript" src="{{.URL}}"></script>`)
CssTagTemplate, _ = template.New("").Parse(`<link rel="stylesheet" href="{{.URL}}" />`)
)
type Filter func(source string) string
type JsCompresser interface {
CompressJs(name string) template.HTML
SetProMode(isPro bool)
SetStaticURL(url string)
}
type CssCompresser interface {
CompressCss(name string) template.HTML
SetProMode(isPro bool)
SetStaticURL(url string)
}
type Settings struct {
Js JsCompresser
Css CssCompresser
}
func NewJsCompress(srcPath, distPath, srcURL, distURL string, groups map[string]Group) JsCompresser {
compress := new(compressJs)
compress.SrcPath = srcPath
compress.DistPath = distPath
compress.SrcURL = srcURL
compress.DistURL = distURL
compress.Groups = groups
compress.StaticURL = "/"
return compress
}
func NewCssCompress(srcPath, distPath, srcURL, distURL string, groups map[string]Group) CssCompresser {
compress := new(compressCss)
compress.SrcPath = srcPath
compress.DistPath = distPath
compress.SrcURL = srcURL
compress.DistURL = distURL
compress.Groups = groups
compress.StaticURL = "/"
return compress
}
func LoadJsonConf(filePath string, proMode bool, staticURL string) (setting *Settings, err error) {
type Conf struct {
Js *compressJs
Css *compressCss
}
var data []byte
if file, err := os.Open(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("Beego Compress: Conf Load %s", err.Error())
} else {
data, err = ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("Beego Compress: Conf Read %s", err.Error())
}
}
conf := Conf{}
err = json.Unmarshal(data, &conf)
if err != nil {
return nil, fmt.Errorf("Beego Compress: Conf Parse %s", err.Error())
}
setting = new(Settings)
if conf.Js != nil {
setting.Js = conf.Js
} else {
setting.Js = new(compressJs)
}
if conf.Css != nil {
setting.Css = conf.Css
} else {
setting.Css = new(compressCss)
}
if staticURL == "" {
staticURL = "/"
}
setting.Js.SetProMode(proMode)
setting.Css.SetProMode(proMode)
setting.Js.SetStaticURL(staticURL)
setting.Css.SetStaticURL(staticURL)
return setting, nil
}