forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
102 lines (86 loc) · 2.81 KB
/
static.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
// 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 (
"errors"
"net/http"
"time"
"github.com/Masterminds/semver/v3"
"github.com/gorilla/mux"
"go.elastic.co/apm/module/apmzap/v2"
"go.uber.org/zap"
"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/proxymode"
)
const staticRouterPath = "/package/{packageName}/{packageVersion}/{name:.*}"
type staticParams struct {
packageName string
packageVersion string
fileName string
}
func staticHandler(logger *zap.Logger, indexer Indexer, cacheTime time.Duration) http.HandlerFunc {
return staticHandlerWithProxyMode(logger, indexer, proxymode.NoProxy(logger), cacheTime)
}
func staticHandlerWithProxyMode(logger *zap.Logger, indexer Indexer, proxyMode *proxymode.ProxyMode, cacheTime time.Duration) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := logger.With(apmzap.TraceContext(r.Context())...)
params, err := staticParamsFromRequest(r)
if err != nil {
badRequest(w, err.Error())
return
}
opts := packages.NameVersionFilter(params.packageName, params.packageVersion)
pkgs, err := indexer.Get(r.Context(), &opts)
if err != nil {
logger.Error("getting package path failed",
zap.String("package.name", params.packageName),
zap.String("package.version", params.packageVersion),
zap.Error(err))
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
if len(pkgs) == 0 && proxyMode.Enabled() {
proxiedPackage, err := proxyMode.Package(r)
if err != nil {
logger.Error("proxy mode: package failed", zap.Error(err))
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
if proxiedPackage != nil {
pkgs = pkgs.Join(packages.Packages{proxiedPackage})
}
}
if len(pkgs) == 0 {
notFoundError(w, errPackageRevisionNotFound)
return
}
cacheHeaders(w, cacheTime)
packages.ServePackageResource(logger, w, r, pkgs[0], params.fileName)
}
}
func staticParamsFromRequest(r *http.Request) (*staticParams, error) {
vars := mux.Vars(r)
packageName, ok := vars["packageName"]
if !ok {
return nil, errors.New("missing package name")
}
packageVersion, ok := vars["packageVersion"]
if !ok {
return nil, errors.New("missing package version")
}
_, err := semver.StrictNewVersion(packageVersion)
if err != nil {
return nil, errors.New("invalid package version")
}
fileName, ok := vars["name"]
if !ok {
return nil, errors.New("missing file name")
}
params := staticParams{
packageName: packageName,
packageVersion: packageVersion,
fileName: fileName,
}
return ¶ms, nil
}