/
server.go
353 lines (289 loc) · 9.67 KB
/
server.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Main entrypoint for Artifact Server.
//
// This is a REST API (no html frontend) implemented using Martini.
//
// Database: Postgresql
// Storage: S3
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"time"
"golang.org/x/net/context"
"gopkg.in/amz.v1/aws"
"gopkg.in/amz.v1/s3"
"gopkg.in/gorp.v1"
"gopkg.in/tylerb/graceful.v1"
"github.com/dropbox/changes-artifacts/api"
"github.com/dropbox/changes-artifacts/common"
"github.com/dropbox/changes-artifacts/common/sentry"
"github.com/dropbox/changes-artifacts/common/stats"
"github.com/dropbox/changes-artifacts/database"
"github.com/dropbox/changes-artifacts/model"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"github.com/martini-contrib/render"
"github.com/rubenv/sql-migrate"
)
type RenderOnGin struct {
render.Render // This makes sure we don't have to create dummies for methods we don't use
ginCtx *gin.Context
}
func (r RenderOnGin) JSON(statusCode int, obj interface{}) {
r.ginCtx.JSON(statusCode, obj)
}
var _ render.Render = (*RenderOnGin)(nil)
func HomeHandler(c *gin.Context) {
c.String(http.StatusOK, "Hello, I am Artie Facts, the artifact store that stores artifacts.")
}
func VersionHandler(c *gin.Context) {
c.String(http.StatusOK, common.GetVersion())
}
func bindBucket(ctx context.Context, r render.Render, gc *gin.Context, db database.Database) {
bucketId := gc.Param("bucket_id")
bucket, err := db.GetBucket(bucketId)
if err != nil && err.EntityNotFound() {
// Don't log this error to Sentry
// Changes will hit this endpoint for non-existant buckets very often.
api.RespondWithErrorf(ctx, r, http.StatusNotFound, "Bucket not found")
gc.Abort()
return
}
if err != nil {
api.LogAndRespondWithError(ctx, r, http.StatusInternalServerError, err)
gc.Abort()
return
}
if bucket == nil {
api.LogAndRespondWithErrorf(ctx, r, http.StatusBadRequest, "Got nil bucket without error for bucket: %s", bucketId)
gc.Abort()
return
}
gc.Set("bucket", bucket)
}
func bindArtifact(ctx context.Context, r render.Render, gc *gin.Context, db database.Database) *model.Artifact {
bucketId := gc.Param("bucket_id")
artifactName := gc.Param("artifact_name")
artifact, err := db.GetArtifactByName(bucketId, artifactName)
if err != nil && err.EntityNotFound() {
api.LogAndRespondWithErrorf(ctx, r, http.StatusNotFound, "Artifact not found")
gc.Abort()
return nil
}
if err != nil {
api.LogAndRespondWithError(ctx, r, http.StatusInternalServerError, err)
gc.Abort()
return nil
}
if artifact == nil {
api.LogAndRespondWithErrorf(ctx, r, http.StatusBadRequest, "Got nil artifact without error for artifact: %s/%s", bucketId, artifactName)
gc.Abort()
return nil
}
gc.Set("artifact", artifact)
return artifact
}
type config struct {
DbConnstr string
CorsURLs string
Env string
S3Server string
S3Region string
S3Bucket string
S3AccessKey string
S3SecretKey string
SentryDSN string
StatsdPrefix string
StatsdURL string
}
var defaultConfig = config{
CorsURLs: "http://dropboxlocalhost.com:5000",
DbConnstr: "postgres://artifacts:artifacts@artifactsdb/artifacts?sslmode=disable",
S3Server: "http://fakes3:4569",
S3Region: "fakes3",
S3Bucket: "artifacts",
}
func getConfigFrom(configFile string) config {
var conf config
if configFile == "" {
return defaultConfig
}
if content, err := ioutil.ReadFile(configFile); err != nil {
log.Fatalf("Unable to open config file %s\n", configFile)
} else {
if err := json.Unmarshal(content, &conf); err != nil {
log.Fatalf("Unable to decode config file %s\n", configFile)
} else {
return conf
}
}
return config{}
}
func performMigrations(db *sql.DB) error {
migrations := &migrate.AssetMigrationSource{
Asset: database.Asset,
AssetDir: database.AssetDir,
Dir: "migrations",
}
n, err := migrate.Exec(db, "postgres", migrations, migrate.Up)
log.Printf("Completed %d migrations\n", n)
if err != nil {
log.Println("Error completing DB migration:", err)
}
return err
}
func getListenAddr() string {
port := os.Getenv("PORT")
if len(port) == 0 {
// By default, we use port 3000
port = "3000"
}
// Host is "" => any address (IPv4/IPv6)
hostPort := ":" + port
log.Printf("About to listen on %s", hostPort)
return hostPort
}
func main() {
var flagConfigFile string
flag.StringVar(&flagConfigFile, "config", "", "JSON Config file containing DB parameters and S3 information")
flagVerbose := flag.Bool("verbose", false, "Enable request logging")
flagLogDBQueries := flag.Bool("log-db-queries", false, "Enable DB query logging (Use with care, will dump raw logchunk contents to logfile)")
showVersion := flag.Bool("version", false, "Show version number and quit")
onlyPerformMigrations := flag.Bool("migrations-only", false, "Only perform database migrations and quit")
dbMaxIdleConns := flag.Int("db-max-idle-conns", 20, "Maximum number of idle connections to the DB")
dbMaxOpenConns := flag.Int("db-max-open-conns", 50, "Maximum number of open connections to the DB")
shutdownTimeout := flag.Duration("shutdown-timeout", 15*time.Second, "Time to wait before closing active connections after SIGTERM signal has been recieved")
flag.Parse()
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
// Required for artifact name deduplication (not strictly necessary, but good to have)
rand.Seed(time.Now().UTC().UnixNano())
if *showVersion {
fmt.Println(common.GetVersion())
return
}
conf := getConfigFrom(flagConfigFile)
// ----- BEGIN DB Connections Setup -----
db, err := sql.Open("postgres", conf.DbConnstr)
if err != nil {
log.Fatalf("Could not connect to the database: %v\n", err)
}
if *onlyPerformMigrations {
err := performMigrations(db)
db.Close()
if err != nil {
os.Exit(1)
}
return
}
db.SetMaxIdleConns(*dbMaxIdleConns)
db.SetMaxOpenConns(*dbMaxOpenConns)
defer db.Close()
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
if *flagLogDBQueries {
dbmap.TraceOn("[gorp]", log.New(os.Stdout, "artifacts:", log.Lmicroseconds))
}
gdb := database.NewGorpDatabase(dbmap)
// ----- END DB Connections Setup -----
// ----- BEGIN AWS Connections -----
var region aws.Region
var auth aws.Auth
if conf.S3Region == "fakes3" {
region = aws.Region{
Name: conf.S3Region,
S3Endpoint: conf.S3Server,
Sign: aws.SignV2,
}
auth = aws.Auth{}
} else {
region = aws.Regions[conf.S3Region]
auth = aws.Auth{AccessKey: conf.S3AccessKey, SecretKey: conf.S3SecretKey}
}
s3Client := s3.New(auth, region)
bucket := s3Client.Bucket(conf.S3Bucket)
// ----- END AWS Connections -----
gdb.RegisterEntities()
stats.CreateStatsdClient(conf.StatsdURL, conf.StatsdPrefix)
defer stats.ShutdownStatsdClient()
g := gin.New()
g.Use(gin.Recovery())
if *flagVerbose {
g.Use(gin.Logger())
}
realClock := new(common.RealClock)
rootCtx := context.Background()
rootCtx = sentry.CreateAndInstallSentryClient(rootCtx, conf.Env, conf.SentryDSN)
g.Use(stats.Counter())
g.GET("/", HomeHandler)
g.GET("/version", VersionHandler)
g.GET("/buckets", func(gc *gin.Context) {
api.ListBuckets(rootCtx, &RenderOnGin{ginCtx: gc}, gdb)
})
g.POST("/buckets/", func(gc *gin.Context) {
api.HandleCreateBucket(rootCtx, &RenderOnGin{ginCtx: gc}, gc.Request, gdb, realClock)
})
g.POST("/buckets/:bucket_id/artifacts/:artifact_name", func(gc *gin.Context) {
render := &RenderOnGin{ginCtx: gc}
afct := bindArtifact(rootCtx, render, gc, gdb)
if !gc.IsAborted() {
api.PostArtifact(rootCtx, render, gc.Request, gdb, bucket, afct)
}
})
br := g.Group("/buckets/:bucket_id", func(gc *gin.Context) {
bindBucket(rootCtx, &RenderOnGin{ginCtx: gc}, gc, gdb)
})
{
br.GET("", func(gc *gin.Context) {
bkt := gc.MustGet("bucket").(*model.Bucket)
api.HandleGetBucket(rootCtx, &RenderOnGin{ginCtx: gc}, bkt)
})
br.POST("/close", func(gc *gin.Context) {
bkt := gc.MustGet("bucket").(*model.Bucket)
api.HandleCloseBucket(rootCtx, &RenderOnGin{ginCtx: gc}, gdb, bkt, bucket, realClock)
})
br.GET("/artifacts/", func(gc *gin.Context) {
bkt := gc.MustGet("bucket").(*model.Bucket)
api.ListArtifacts(rootCtx, &RenderOnGin{ginCtx: gc}, gc.Request, gdb, bkt)
})
br.POST("/artifacts", func(gc *gin.Context) {
bkt := gc.MustGet("bucket").(*model.Bucket)
api.HandleCreateArtifact(rootCtx, &RenderOnGin{ginCtx: gc}, gc.Request, gdb, bkt)
})
ar := br.Group("/artifacts/:artifact_name", func(gc *gin.Context) {
bindArtifact(rootCtx, &RenderOnGin{ginCtx: gc}, gc, gdb)
})
{
ar.GET("", func(gc *gin.Context) {
afct := gc.MustGet("artifact").(*model.Artifact)
api.HandleGetArtifact(rootCtx, &RenderOnGin{ginCtx: gc}, afct)
})
ar.POST("/close", func(gc *gin.Context) {
afct := gc.MustGet("artifact").(*model.Artifact)
api.HandleCloseArtifact(rootCtx, &RenderOnGin{ginCtx: gc}, gdb, bucket, afct)
})
ar.GET("/content", func(gc *gin.Context) {
afct := gc.MustGet("artifact").(*model.Artifact)
api.GetArtifactContent(rootCtx, &RenderOnGin{ginCtx: gc}, gc.Request, gc.Writer, gdb, bucket, afct)
})
ar.GET("/chunked", func(gc *gin.Context) {
if conf.CorsURLs != "" {
gc.Writer.Header().Add("Access-Control-Allow-Origin", conf.CorsURLs)
}
afct := gc.MustGet("artifact").(*model.Artifact)
api.GetArtifactContentChunks(rootCtx, &RenderOnGin{ginCtx: gc}, gc.Request, gc.Writer, gdb, bucket, afct)
})
}
}
http.Handle("/", g)
// If the process gets a SIGTERM, it will close listening port allowing another server to bind and
// begin listening immediately. Any ongoing connections will be given 15 seconds (by default) to
// complete, after which they are forcibly terminated.
graceful.Run(getListenAddr(), *shutdownTimeout, nil)
}