-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
executable file
·206 lines (178 loc) · 5.44 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
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
package main
import (
"context"
"flag"
"log"
"log/slog"
"net"
"os"
"runtime/debug"
"strings"
"syscall"
"time"
"aws-in-a-box/arn"
"aws-in-a-box/http"
"aws-in-a-box/server"
"aws-in-a-box/services/dynamodb"
"aws-in-a-box/services/kinesis"
"aws-in-a-box/services/kms"
"aws-in-a-box/services/s3"
"aws-in-a-box/services/sqs"
)
var BazelSuffix = ""
func versionString() string {
version := Version + BazelSuffix
// We only have ReadBuildInfo in non-bazel builds.
if version != Version {
buildinfo, ok := debug.ReadBuildInfo()
if ok {
for _, s := range buildinfo.Settings {
if s.Key == "vcs.modified" {
version += " (dirty)"
break
}
}
}
}
return version
}
func main() {
addr := flag.String("addr", "localhost:4569", "Address to run on")
reusePort := flag.Bool("reuse-port", false, "If true, sets the SO_REUSEPORT socket option")
persistDir := flag.String("persistDir", "", "Directory to persist data to. If empty, data is not persisted.")
logLevel := flag.String("logLevel", "debug", "debug/info/warn/error")
enableKinesis := flag.Bool("enableKinesis", true, "Enable Kinesis service")
kinesisInitialStreams := flag.String("kinesisInitialStreams", "",
"Streams to create at startup. Example: stream1,stream2,stream3")
kinesisInitialShardsPerStream := flag.Int64("kinesisInitialShardsPerStream", 2,
"How many shards to create for each stream listed in -kinesisInitialStreams")
kinesisDefaultDuration := flag.Duration("kinesisDefaultDuration", 24*time.Hour,
"How long to retain messages. Can be used to control memory usage. After creation, retention can be adjusted with [Increase/Decrease]StreamRetentionPeriod")
kinesisStreamCreateDuration := flag.Duration("kinesisStreamCreateDuration", 5*time.Second,
"How long a new Kinesis stream stays in CREATING status")
kinesisStreamDeleteDuration := flag.Duration("kinesisStreamDeleteDuration", 5*time.Second,
"How long a deleted Kinesis stream stays in DELETING status")
enableKMS := flag.Bool("enableKMS", true, "Enable Kinesis service")
enableDynamoDB := flag.Bool("experimental_enableDynamoDB", true, "Enable DynamoDB service")
enableS3 := flag.Bool("enableS3", true, "Enable S3 service")
s3InitialBuckets := flag.String("s3InitialBuckets", "", "Buckets to create at startup. Example: bucket1,bucket2,bucket3")
enableSQS := flag.Bool("enableSQS", true, "Enable SQS service")
flag.Parse()
var level slog.Level
switch *logLevel {
case "debug":
level = slog.LevelDebug
case "info":
level = slog.LevelInfo
case "warn":
level = slog.LevelWarn
case "error":
level = slog.LevelError
default:
panic("Invalid log level")
}
textHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
logger := slog.New(textHandler).With("version", versionString())
methodRegistry := make(http.Registry)
arnGenerator := arn.Generator{
// TODO: make these configurable?
AwsAccountId: "123456789012",
Region: "us-east-1",
}
if *enableKinesis {
logger := logger.With("service", "kinesis")
k := kinesis.New(kinesis.Options{
Logger: logger,
ArnGenerator: arnGenerator,
DefaultRetention: *kinesisDefaultDuration,
StreamCreateDuration: *kinesisStreamCreateDuration,
StreamDeleteDuration: *kinesisStreamDeleteDuration,
})
for _, name := range strings.Split(*kinesisInitialStreams, ",") {
k.CreateStream(kinesis.CreateStreamInput{
StreamName: name,
ShardCount: *kinesisInitialShardsPerStream,
})
}
k.RegisterHTTPHandlers(logger, methodRegistry)
logger.Info("Enabled Kinesis")
}
if *enableKMS {
logger := logger.With("service", "kms")
k, err := kms.New(kms.Options{
Logger: logger,
ArnGenerator: arnGenerator,
PersistDir: *persistDir,
})
if err != nil {
log.Fatal(err)
}
k.RegisterHTTPHandlers(logger, methodRegistry)
logger.Info("Enabled KMS")
}
if *enableDynamoDB {
logger := logger.With("service", "dynamodb")
d := dynamodb.New(dynamodb.Options{
Logger: logger,
ArnGenerator: arnGenerator,
})
d.RegisterHTTPHandlers(logger, methodRegistry)
logger.Info("Enabled DynamoDB (EXPERIMENTAL!!!)")
}
handlerChain := []server.HandlerFunc{server.HandlerFuncFromRegistry(logger, methodRegistry)}
if *enableSQS {
logger := logger.With("service", "sqs")
s := sqs.New(sqs.Options{
Logger: logger,
ArnGenerator: arnGenerator,
})
// Register JSON handler
s.RegisterHTTPHandlers(logger, methodRegistry)
// Register form data handler
handlerChain = append(handlerChain, sqs.NewHandler(logger, s))
logger.Info("Enabled SQS")
}
if *enableS3 {
logger := logger.With("service", "s3")
s, err := s3.New(s3.Options{
Logger: logger,
Addr: *addr,
PersistDir: *persistDir,
})
if err != nil {
log.Fatal(err)
}
for _, name := range strings.Split(*s3InitialBuckets, ",") {
s.CreateBucket(s3.CreateBucketInput{
Bucket: name,
})
}
handlerChain = append(handlerChain, s3.NewHandler(logger, s))
}
srv := server.NewWithHandlerChain(handlerChain...)
lc := net.ListenConfig{
Control: func(network, address string, conn syscall.RawConn) error {
if !*reusePort {
return nil
}
var setSockoptErr error
err := conn.Control(func(fd uintptr) {
setSockoptErr = setSockopt(fd)
})
if err != nil {
return err
}
return setSockoptErr
},
}
l, err := lc.Listen(context.Background(), "tcp", *addr)
if err != nil {
log.Fatal(err)
}
err = srv.Serve(l)
if err != nil {
panic(err)
}
}