-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddyfile.go
124 lines (103 loc) · 2.98 KB
/
caddyfile.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
package minifier
import (
"log"
"strconv"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
htmlminify "github.com/tdewolff/minify/v2/html"
)
var moduleName = "minifier"
func init() {
caddy.RegisterModule(Middleware{})
httpcaddyfile.RegisterHandlerDirective(moduleName, parseCaddyfile)
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
m := new(Middleware)
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
func converterStrToBoolean(str string) bool {
boolValue, err := strconv.ParseBool(str)
if err != nil {
// print error info
log.Println("error on converterStrToBoolean: ", err)
return false
}
return boolValue
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// default configuration
m.Html = htmlminify.Minifier{
KeepConditionalComments: false,
KeepSpecialComments: false,
KeepComments: false,
KeepWhitespace: false,
KeepDefaultAttrVals: false,
KeepDocumentTags: false,
KeepEndTags: false,
KeepQuotes: false,
}
// TODO: improve mechanism to matching config with available minifier config
for d.Next() {
if d.NextArg() {
return d.ArgErr()
}
// block 0 to determine config for specific content type
for d.NextBlock(0) {
if d.NextArg() {
return d.ArgErr()
}
// check content type name
var configFor = d.Val()
// block 1 to set config detail for specific content type
for d.NextBlock(1) {
if configFor == "html" {
if d.Val() == "KeepComments" {
// get value from config
d.NextArg()
m.Html.KeepComments = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepWhitespace" {
// get value from config
d.NextArg()
m.Html.KeepWhitespace = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepDefaultAttrVals" {
// get value from config
d.NextArg()
m.Html.KeepDefaultAttrVals = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepDocumentTags" {
// get value from config
d.NextArg()
m.Html.KeepDocumentTags = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepEndTags" {
// get value from config
d.NextArg()
m.Html.KeepEndTags = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepQuotes" {
// get value from config
d.NextArg()
m.Html.KeepQuotes = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepConditionalComments" {
// get value from config
d.NextArg()
m.Html.KeepConditionalComments = converterStrToBoolean(d.Val())
}
if d.Val() == "KeepSpecialComments" {
// get value from config
d.NextArg()
m.Html.KeepSpecialComments = converterStrToBoolean(d.Val())
}
}
}
}
}
return nil
}