-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
156 lines (138 loc) · 4.36 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
flag "github.com/spf13/pflag"
)
var bucketHost string
var bucketPort string
var bucketHostPort string
var bucketName string
var bucketRegion string
var awsCredentials aws.Credentials
const metricsNamespace = "s3gw"
var (
requestsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricsNamespace,
Name: "request_count_total",
Help: "A counter for requests to the wrapped handler.",
},
[]string{"code", "method", "handler"},
)
durationHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: metricsNamespace,
Name: "request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: []float64{.25, .5, 1, 2.5, 5, 10, 25, 50, 100},
},
[]string{"code", "method", "handler"},
)
)
func init() {
prometheus.MustRegister(requestsCounter)
prometheus.MustRegister(durationHistogram)
}
var flagUsePathStyle bool
var flagListen string
var flagHostsAllow string
var flagHostsDeny string
var flagReadTimeout time.Duration
func init() {
flag.BoolVar(&flagUsePathStyle, "use-path-style", false, "use path style bucket name")
flag.StringVar(&flagListen, "listen", ":80", "addr:port to listen to")
flag.StringVar(&flagHostsAllow, "hosts-allow", "", "subnets allowed to access to this gw, separated by comma")
flag.StringVar(&flagHostsDeny, "hosts-deny", "", "subnets denied to access to this gw, separated by comma")
flag.DurationVar(&flagReadTimeout, "read-timeout", 0, "read timeout for each requests as a http server. the default value (0) means library default, which is 30s, not indefinite.")
}
func main() {
flag.Parse()
well.LogConfig{}.Apply()
// names of envs which must not be empty
envNames := []string{
"BUCKET_HOST",
"BUCKET_NAME",
"BUCKET_REGION",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
}
emptyEnvNames := []string{}
for _, n := range envNames {
if os.Getenv(n) == "" {
emptyEnvNames = append(emptyEnvNames, n)
}
}
if len(emptyEnvNames) != 0 {
log.ErrorExit(fmt.Errorf("some environment variables are empty: %v", emptyEnvNames))
}
bucketHost = os.Getenv("BUCKET_HOST")
bucketName = os.Getenv("BUCKET_NAME")
bucketPort = os.Getenv("BUCKET_PORT")
bucketRegion = os.Getenv("BUCKET_REGION")
awsCredentials.AccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
awsCredentials.SecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
if bucketPort == "" {
bucketHostPort = bucketHost
} else {
bucketHostPort = bucketHost + ":" + bucketPort
}
allowdeny, err := ParseAllowDeny(flagHostsAllow, flagHostsDeny)
if err != nil {
log.ErrorExit(err)
}
client = s3.NewFromConfig(aws.Config{
Region: bucketRegion,
Credentials: credentialsProvider{},
}, func(opts *s3.Options) {
opts.BaseEndpoint = aws.String("http://" + bucketHostPort + "/")
opts.UsePathStyle = flagUsePathStyle
})
mux := http.NewServeMux()
server := &well.HTTPServer{
Server: &http.Server{
Handler: mux,
Addr: flagListen,
ReadTimeout: flagReadTimeout,
},
}
listHandler :=
promhttp.InstrumentHandlerCounter(requestsCounter.MustCurryWith(prometheus.Labels{"handler": "list"}),
promhttp.InstrumentHandlerDuration(durationHistogram.MustCurryWith(prometheus.Labels{"handler": "list"}),
http.HandlerFunc(listHandlerFunc)))
objectHandler :=
promhttp.InstrumentHandlerCounter(requestsCounter.MustCurryWith(prometheus.Labels{"handler": "object"}),
promhttp.InstrumentHandlerDuration(durationHistogram.MustCurryWith(prometheus.Labels{"handler": "object"}),
http.HandlerFunc(objectHandlerFunc)))
mux.HandleFunc("/bucket/", func(res http.ResponseWriter, req *http.Request) {
if !allowdeny.IsAllowedHostPort(req.RemoteAddr) {
res.WriteHeader(http.StatusForbidden)
return
}
if req.URL.Path == "/bucket/" {
listHandler(res, req)
} else {
objectHandler(res, req)
}
})
mux.HandleFunc("/health", healthHandler)
mux.Handle("/metrics", promhttp.Handler())
err = server.ListenAndServe()
if err != nil {
log.ErrorExit(err)
}
log.Info("starting...", nil)
well.Stop()
err = well.Wait()
if !well.IsSignaled(err) && err != nil {
log.ErrorExit(err)
}
}