forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifacts.go
69 lines (56 loc) · 1.78 KB
/
artifacts.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"log"
"net/http"
"time"
"github.com/Masterminds/semver/v3"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/elastic/package-registry/archiver"
)
const artifactsRouterPath = "/epr/{packageName}/{packageName:[a-z0-9_]+}-{packageVersion}.zip"
var errArtifactNotFound = errors.New("artifact not found")
func artifactsHandler(packagesBasePaths []string, cacheTime time.Duration) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
packageName, ok := vars["packageName"]
if !ok {
badRequest(w, "missing package name")
return
}
packageVersion, ok := vars["packageVersion"]
if !ok {
badRequest(w, "missing package version")
return
}
_, err := semver.StrictNewVersion(packageVersion)
if err != nil {
badRequest(w, "invalid package version")
return
}
packagePath, err := getPackagePath(packagesBasePaths, packageName, packageVersion)
if err == errResourceNotFound {
notFoundError(w, errArtifactNotFound)
return
}
if err != nil {
log.Printf("stat package path '%s' failed: %v", packagePath, err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/gzip")
cacheHeaders(w, cacheTime)
err = archiver.ArchivePackage(w, archiver.PackageProperties{
Name: packageName,
Version: packageVersion,
Path: packagePath,
})
if err != nil {
log.Printf("archiving package path '%s' failed: %v", packagePath, err)
return
}
}
}