-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathmain.go
More file actions
233 lines (195 loc) · 7.48 KB
/
main.go
File metadata and controls
233 lines (195 loc) · 7.48 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/cloudlena/adapters/logging"
"github.com/cloudlena/s3manager/internal/app/s3manager"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
//go:embed web/template
var templateFS embed.FS
//go:embed web/static
var staticFS embed.FS
type s3InstanceConfig struct {
Name string
Endpoint string
UseIam bool
IamEndpoint string
AccessKeyID string
SecretAccessKey string
Region string
UseSSL bool
SkipSSLVerification bool
SignatureType string
}
type configuration struct {
S3Instances []s3InstanceConfig
AllowDelete bool
ForceDownload bool
ListRecursive bool
Port string
Timeout int32
SseType string
SseKey string
}
func parseConfiguration() configuration {
viper.AutomaticEnv()
// Parse S3 instances from numbered environment variables
var s3Instances []s3InstanceConfig
for i := 1; ; i++ {
prefix := fmt.Sprintf("%d_", i)
name := viper.GetString(prefix + "NAME")
// Support unnamed single instance configs
if i == 1 && name == "" {
prefix = ""
name = "Default"
}
endpoint := viper.GetString(prefix + "ENDPOINT")
// If NAME or ENDPOINT is not found, stop parsing
if name == "" || endpoint == "" {
break
}
accessKeyID := viper.GetString(prefix + "ACCESS_KEY_ID")
secretAccessKey := viper.GetString(prefix + "SECRET_ACCESS_KEY")
useIam := viper.GetBool(prefix + "USE_IAM")
iamEndpoint := viper.GetString(prefix + "IAM_ENDPOINT")
region := viper.GetString(prefix + "REGION")
viper.SetDefault(prefix+"USE_SSL", true)
useSSL := viper.GetBool(prefix + "USE_SSL")
viper.SetDefault(prefix+"SKIP_SSL_VERIFICATION", false)
skipSSLVerification := viper.GetBool(prefix + "SKIP_SSL_VERIFICATION")
viper.SetDefault(prefix+"SIGNATURE_TYPE", "V4")
signatureType := viper.GetString(prefix + "SIGNATURE_TYPE")
if !useIam {
if accessKeyID == "" {
log.Fatalf("please provide %sACCESS_KEY_ID for instance %s", prefix, name)
}
if secretAccessKey == "" {
log.Fatalf("please provide %sSECRET_ACCESS_KEY for instance %s", prefix, name)
}
}
s3Instances = append(s3Instances, s3InstanceConfig{
Name: name,
Endpoint: endpoint,
UseIam: useIam,
IamEndpoint: iamEndpoint,
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
Region: region,
UseSSL: useSSL,
SkipSSLVerification: skipSSLVerification,
SignatureType: signatureType,
})
}
if len(s3Instances) == 0 {
log.Fatal("no S3 instances configured. Please provide numbered environment variables like 1_NAME, 1_ENDPOINT, etc.")
}
viper.SetDefault("ALLOW_DELETE", true)
allowDelete := viper.GetBool("ALLOW_DELETE")
viper.SetDefault("FORCE_DOWNLOAD", true)
forceDownload := viper.GetBool("FORCE_DOWNLOAD")
listRecursive := viper.GetBool("LIST_RECURSIVE")
viper.SetDefault("PORT", "8080")
port := viper.GetString("PORT")
viper.SetDefault("TIMEOUT", 600)
timeout := viper.GetInt32("TIMEOUT")
viper.SetDefault("SSE_TYPE", "")
sseType := viper.GetString("SSE_TYPE")
viper.SetDefault("SSE_KEY", "")
sseKey := viper.GetString("SSE_KEY")
return configuration{
S3Instances: s3Instances,
AllowDelete: allowDelete,
ForceDownload: forceDownload,
ListRecursive: listRecursive,
Port: port,
Timeout: timeout,
SseType: sseType,
SseKey: sseKey,
}
}
func main() {
configuration := parseConfiguration()
sseType := s3manager.SSEType{Type: configuration.SseType, Key: configuration.SseKey}
serverTimeout := time.Duration(configuration.Timeout) * time.Second
// Set up templates
templates, err := fs.Sub(templateFS, "web/template")
if err != nil {
log.Fatal(err)
}
// Set up statics
statics, err := fs.Sub(staticFS, "web/static")
if err != nil {
log.Fatal(err)
}
// Convert configuration to s3manager format
var s3Configs []s3manager.S3InstanceConfig
for _, instance := range configuration.S3Instances {
s3Configs = append(s3Configs, s3manager.S3InstanceConfig{
Name: instance.Name,
Endpoint: instance.Endpoint,
UseIam: instance.UseIam,
IamEndpoint: instance.IamEndpoint,
AccessKeyID: instance.AccessKeyID,
SecretAccessKey: instance.SecretAccessKey,
Region: instance.Region,
UseSSL: instance.UseSSL,
SkipSSLVerification: instance.SkipSSLVerification,
SignatureType: instance.SignatureType,
})
}
// Set up Multi S3 Manager
s3Manager, err := s3manager.NewMultiS3Manager(s3Configs)
if err != nil {
log.Fatalln(fmt.Errorf("error creating multi s3 manager: %w", err))
}
// Check for a root URL to insert into HTML templates in case of reverse proxying
rootURL, rootSet := os.LookupEnv("ROOT_URL")
if rootSet && !strings.HasPrefix(rootURL, "/") {
rootURL = "/" + rootURL
}
// Set up router
r := mux.NewRouter()
// Root redirects to first instance's buckets page
r.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
instances := s3Manager.GetAllInstances()
if len(instances) > 0 {
http.Redirect(w, r, rootURL+"/"+instances[0].Name+"/buckets", http.StatusPermanentRedirect)
} else {
http.Error(w, "No S3 instances configured", http.StatusInternalServerError)
}
})).Methods(http.MethodGet)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(statics)))).Methods(http.MethodGet)
// S3 instance management endpoints
r.Handle("/api/s3-instances", s3manager.HandleGetS3Instances(s3Manager)).Methods(http.MethodGet)
// S3 management endpoints (with instance in URL)
r.Handle("/{instance}/buckets", s3manager.HandleBucketsViewWithManager(s3Manager, templates, configuration.AllowDelete, rootURL)).Methods(http.MethodGet)
r.PathPrefix("/{instance}/buckets/").Handler(s3manager.HandleBucketViewWithManager(s3Manager, templates, configuration.AllowDelete, configuration.ListRecursive, rootURL)).Methods(http.MethodGet)
r.Handle("/{instance}/api/buckets", s3manager.HandleCreateBucketWithManager(s3Manager)).Methods(http.MethodPost)
if configuration.AllowDelete {
r.Handle("/{instance}/api/buckets/{bucketName}", s3manager.HandleDeleteBucketWithManager(s3Manager)).Methods(http.MethodDelete)
}
r.Handle("/{instance}/api/buckets/{bucketName}/objects", s3manager.HandleCreateObjectWithManager(s3Manager, sseType)).Methods(http.MethodPost)
r.Handle("/{instance}/api/buckets/{bucketName}/objects/{objectName:.*}/url", s3manager.HandleGenerateURLWithManager(s3Manager)).Methods(http.MethodGet)
r.Handle("/{instance}/api/buckets/{bucketName}/objects/{objectName:.*}", s3manager.HandleGetObjectWithManager(s3Manager, configuration.ForceDownload)).Methods(http.MethodGet)
if configuration.AllowDelete {
r.Handle("/{instance}/api/buckets/{bucketName}/objects/{objectName:.*}", s3manager.HandleDeleteObjectWithManager(s3Manager)).Methods(http.MethodDelete)
r.Handle("/{instance}/api/buckets/{bucketName}/objects/bulk-delete", s3manager.HandleBulkDeleteObjectsWithManager(s3Manager)).Methods(http.MethodPost)
}
r.Handle("/{instance}/api/buckets/{bucketName}/objects/bulk-download", s3manager.HandleBulkDownloadObjectsWithManager(s3Manager)).Methods(http.MethodPost)
lr := logging.Handler(os.Stdout)(r)
srv := &http.Server{
Addr: ":" + configuration.Port,
Handler: lr,
ReadTimeout: serverTimeout,
WriteTimeout: serverTimeout,
}
log.Fatal(srv.ListenAndServe())
}