-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
57 lines (49 loc) · 1.32 KB
/
setup.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
package gomods
import (
"fmt"
"net/http"
"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"
)
type Gomods struct {
Config Config
}
func init() {
caddy.RegisterModule(Gomods{})
httpcaddyfile.RegisterHandlerDirective("gomods", parse)
}
func parse(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var g Gomods
err := g.UnmarshalCaddyfile(h.Dispenser)
return g, err
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (g *Gomods) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if d.Val() == "gomods" {
d.Next()
if d.Val() != "{" {
break
}
d.Next()
}
if err := g.Config.ParseGomods(d); err != nil {
return fmt.Errorf("[gomods]: Couldn't parse the config: %s", err.Error())
}
}
g.Config.SetDefaults()
return nil
}
// CaddyModule returns the Caddy module information.
func (Gomods) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
Name: "http.handlers.gomods",
New: func() caddy.Module { return new(Gomods) },
}
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (g Gomods) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
return g.Config.Serve(w, r)
}