-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3dir.go
65 lines (54 loc) · 1.78 KB
/
s3dir.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
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/jessevdk/go-flags"
"github.com/relvacode/s3dir/pkg/s3dir"
"net/http"
"os"
)
type CLI struct {
ListenAddress string `long:"listen-address" env:"LISTEN_ADDRESS" default:"127.0.0.1:9001" description:"Listen on this address"`
Endpoint string `long:"endpoint" env:"ENDPOINT" description:"AWS S3 endpoint"`
Application string `long:"application" env:"APPLICATION" default:"S3" description:"Your application name. Used to configure the page title"`
ForwardHTTPHeaders []string `long:"forward-http-header" env:"FORWARD_HTTP_HEADERS" description:"Forward these incoming HTTP headers to the S3 server"`
}
func Main() error {
var cli CLI
p := flags.NewParser(&cli, flags.HelpFlag)
_, err := p.Parse()
if err != nil {
return err
}
var configOpts []func(*config.LoadOptions) error
if cli.Endpoint != "" {
configOpts = append(configOpts, config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service != s3.ServiceID {
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
}
return aws.Endpoint{
URL: cli.Endpoint,
SigningRegion: "us-west-2",
}, nil
})))
}
cfg, err := config.LoadDefaultConfig(context.Background(), configOpts...)
if err != nil {
return err
}
httpServer := &http.Server{
Addr: cli.ListenAddress,
Handler: s3dir.New(s3.NewFromConfig(cfg), &s3dir.Renderer{Title: cli.Application}, cli.ForwardHTTPHeaders),
}
return httpServer.ListenAndServe()
}
func main() {
err := Main()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}