forked from lindenlab/caddy-s3-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddyfile.go
125 lines (115 loc) · 2.94 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
125
package caddys3proxy
import (
"strconv"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func init() {
httpcaddyfile.RegisterHandlerDirective("s3proxy", parseCaddyfile)
}
// parseCaddyfile parses the s3proxy directive. It enables the proxying
// requests to S3 and configures it with this syntax:
//
// s3proxy [<matcher>] {
// root <path to prefix S3 key with>
// region <aws region>
// profile <aws profile>
// bucket <s3 bucket name>
// index <files...>
// hide <file patterns...>
// endpoint <alternative endpoint>
// enable_put
// enable_delete
// force_path_style
// use_accelerate
// errors [<http code>] [<s3 key to error page>|pass_through]
// browse [<template file>]
// }
//
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
return parseCaddyfileWithDispenser(h.Dispenser)
}
func parseCaddyfileWithDispenser(h *caddyfile.Dispenser) (*S3Proxy, error) {
var b S3Proxy
h.NextArg() // skip block beginning: "s3proxy"
parseLoop:
for h.NextBlock(0) {
switch h.Val() {
case "endpoint":
if !h.AllArgs(&b.Endpoint) {
return nil, h.ArgErr()
}
case "region":
if !h.AllArgs(&b.Region) {
return nil, h.ArgErr()
}
case "profile":
if !h.AllArgs(&b.Profile) {
return nil, h.ArgErr()
}
case "root":
if !h.AllArgs(&b.Root) {
return nil, h.ArgErr()
}
case "hide":
b.Hide = h.RemainingArgs()
if len(b.Hide) == 0 {
return nil, h.ArgErr()
}
case "bucket":
if !h.AllArgs(&b.Bucket) {
return nil, h.ArgErr()
}
if b.Bucket == "" {
break parseLoop
}
case "index":
b.IndexNames = h.RemainingArgs()
if len(b.IndexNames) == 0 {
return nil, h.ArgErr()
}
case "enable_put":
b.EnablePut = true
case "enable_delete":
b.EnableDelete = true
case "force_path_style":
b.S3ForcePathStyle = true
case "use_accelerate":
b.S3UseAccelerate = true
case "browse":
b.EnableBrowse = true
args := h.RemainingArgs()
if len(args) == 1 {
b.BrowseTemplate = args[0]
}
if len(args) > 1 {
return nil, h.ArgErr()
}
case "error_page", "errors":
if b.ErrorPages == nil {
b.ErrorPages = make(map[int]string)
}
args := h.RemainingArgs()
if len(args) == 1 {
b.DefaultErrorPage = args[0]
} else if len(args) == 2 {
httpStatusStr := args[0]
s3KeyOrPassThrough := args[1]
httpStatus, err := strconv.Atoi(httpStatusStr)
if err != nil {
return nil, h.Errf("'%s' is not a valid HTTP status code", httpStatusStr)
}
b.ErrorPages[httpStatus] = s3KeyOrPassThrough
} else {
return nil, h.ArgErr()
}
default:
return nil, h.Errf("%s not a valid s3proxy option", h.Val())
}
}
if b.Bucket == "" {
return nil, h.Err("bucket must be set and not empty")
}
return &b, nil
}