forked from psvmcc/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
243 lines (219 loc) · 7.6 KB
/
main.go
File metadata and controls
243 lines (219 loc) · 7.6 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
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/psvmcc/hub/pkg/handlers"
"github.com/psvmcc/hub/pkg/logging"
"github.com/psvmcc/hub/pkg/templates"
"github.com/psvmcc/hub/pkg/types"
"github.com/psvmcc/hub/pkg/victoriametrics"
"github.com/VictoriaMetrics/metrics"
"github.com/labstack/echo/v4"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)
var (
version string
commit string
cfg types.ConfigFile
)
func main() {
app := cli.NewApp()
app.Name = "HUB"
app.Usage = "Cashing proxy server"
app.Version = fmt.Sprintf("%s (%s)", version, commit)
app.Commands = []*cli.Command{
{
Name: "server",
Aliases: []string{"s"},
Usage: "Starts cashing proxy server",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Usage: "Verbose logging",
EnvVars: []string{"HUB_VERBOSE"},
},
&cli.StringFlag{
Name: "bind",
Usage: "Bind address",
Value: "0.0.0.0:6587",
EnvVars: []string{"HUB_BIND"},
},
&cli.StringFlag{
Name: "self-exporter-bind",
Usage: "Metrics self exporter bind address",
Value: "0.0.0.0:6588",
EnvVars: []string{"HUB_SELF_EXPORTER_BIND"},
},
&cli.StringFlag{
Name: "config",
Usage: "Config path",
Value: "config.yaml",
EnvVars: []string{"HUB_CONFIG"},
},
},
Action: startServer,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func startServer(c *cli.Context) error {
metrics.GetOrCreateCounter(fmt.Sprintf("hub_app_version{version=%q,commit=%q}", version, commit)).Inc()
cfg.Load(c.String("config"))
logger := logging.Build(c.Bool("verbose"))
zap.ReplaceGlobals(logger)
echo.NotFoundHandler = func(c echo.Context) error {
return c.String(http.StatusNotFound, "404 page not found")
}
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.IPExtractor = echo.ExtractIPFromXFFHeader(
echo.TrustLoopback(true),
echo.TrustLinkLocal(false),
echo.TrustPrivateNet(false),
)
httpLogger := zap.S().Named("http")
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("cfg", cfg)
c.Set("logger", httpLogger)
c.Response().Header().Set("Server", fmt.Sprintf("hub/%s (%s)", version, commit))
return next(c)
}
})
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
if strings.HasPrefix(req.RequestURI, "/ping") {
return err
}
start := time.Now()
if err = next(c); err != nil {
c.Error(err)
}
stop := time.Now()
cacheStatus := res.Header().Get("X-Cache-Status")
if cacheStatus == "" {
cacheStatus = "UNKNOWN"
}
message := fmt.Sprintf(
"[%s] %s %s requested from %s with status %d in %s [%s] cache=%s",
c.Request().Host,
req.Method,
req.RequestURI,
c.RealIP(),
res.Status,
stop.Sub(start).String(),
c.Path(),
cacheStatus,
)
logger := c.Get("logger").(*zap.SugaredLogger)
if res.Status >= 100 && res.Status <= 399 {
logger.Named("req").Info(message)
} else if res.Status >= 400 && res.Status <= 499 {
logger.Named("req").Warn(message)
} else {
logger.Named("req").Error(message)
}
return err
}
})
e.Renderer = &templates.TemplateRegistry{
Templates: template.Must(template.New("pypi").Funcs(template.FuncMap{"kindIs": templates.KindIs}).Parse(templates.PypiHTML)),
}
e.GET("/*", func(c echo.Context) error {
return c.String(http.StatusNotFound, "")
}).Name = "global::default"
e.GET("/ping", func(c echo.Context) error {
return c.String(http.StatusOK, "pong")
}).Name = "global::ping"
for k := range cfg.Server.PYPI {
p := e.Group(fmt.Sprintf("/pypi/%s", k))
p.GET("/simple/:name/", handlers.PypiSimple(k)).Name = fmt.Sprintf("pypi::%s::simple", k)
p.GET("/packages/:name/:filename", handlers.PypiPackages(k)).Name = fmt.Sprintf("pypi::%s::packages", k)
}
for k := range cfg.Server.RUBYGEMS {
r := e.Group(fmt.Sprintf("/rubygems/%s", k))
r.GET("/*", handlers.RubyGems(k)).Name = fmt.Sprintf("rubygems::%s", k)
}
for k := range cfg.Server.Static {
s := e.Group(fmt.Sprintf("/static/%s", k))
s.GET("/get/*", handlers.Static(k)).Name = fmt.Sprintf("static::%s", k)
}
for k := range cfg.Server.GOPROXY {
g := e.Group(fmt.Sprintf("/goproxy/%s", k))
g.GET("/*", func(c echo.Context) error {
path := c.Param("*")
switch {
case strings.HasSuffix(path, "/@v/list"):
return handlers.GoProxyList(k)(c)
case strings.HasSuffix(path, ".info"):
return handlers.GoProxyInfo(k)(c)
case strings.HasSuffix(path, ".mod"):
return handlers.GoProxyMod(k)(c)
case strings.HasSuffix(path, ".zip"):
return handlers.GoProxyZip(k)(c)
case strings.HasSuffix(path, "/@latest"):
return handlers.GoProxyLatest(k)(c)
default:
return c.String(http.StatusNotFound, "404 page not found")
}
}).Name = fmt.Sprintf("goproxy::%s", k)
}
for k := range cfg.Server.NPM {
n := e.Group(fmt.Sprintf("/npm/%s", k))
n.GET("/*", handlers.NpmProxy(k)).Name = fmt.Sprintf("npm::%s", k)
}
for k, source := range cfg.Server.Cargo {
if source.Base == "" {
log.Fatal("[CARGO] Wrong config definition, please set base URL.")
}
cg := e.Group(fmt.Sprintf("/cargo/%s", k))
cg.GET("/*", handlers.CargoIndex(k)).Name = fmt.Sprintf("cargo::%s::index_root", k)
cg.GET("/index/*", handlers.CargoIndex(k)).Name = fmt.Sprintf("cargo::%s::index", k)
cg.GET("/crates/:crate/:version/download", handlers.CargoCrateDownload(k)).Name = fmt.Sprintf("cargo::%s::crates", k)
cg.GET("/api/*", handlers.CargoAPIProxy(k)).Name = fmt.Sprintf("cargo::%s::api", k)
cg.HEAD("/api/*", handlers.CargoAPIProxy(k)).Name = fmt.Sprintf("cargo::%s::api::head", k)
}
for k, v := range cfg.Server.Galaxy {
g := e.Group(fmt.Sprintf("/galaxy/%s", k))
if v.URL != "" && v.Dir != "" {
log.Fatalf("[GALAXY] Wrong config definition for [%s], please don't use url and dir params together.", k)
}
g.Any("", func(c echo.Context) error {
return c.String(http.StatusOK, "")
})
g.GET("/api", func(c echo.Context) error {
data := types.APIVersions{}
data.AvailableVersions.V3 = "v3/"
return c.JSON(http.StatusOK, data)
}).Name = "galaxy::api"
if v.URL != "" {
g.GET("/api/v3/collections/:namespace/:name/", handlers.GalaxyProxyCollection(k)).Name = fmt.Sprintf("galaxy::%s::collection", k)
g.GET("/api/v3/collections/:namespace/:name/versions/", handlers.GalaxyProxyCollectionVersions(k)).Name = fmt.Sprintf("galaxy::%s::collection::versions", k)
g.GET("/api/v3/collections/:namespace/:name/versions/:version/", handlers.GalaxyProxyCollectionVersionInfo(k)).Name = fmt.Sprintf("galaxy::%s::collection::version", k)
g.GET("/get/:namespace/:name/:version", handlers.GalaxyProxyCollectionGet(k)).Name = fmt.Sprintf("galaxy::%s::get", k)
} else if v.Dir != "" {
g.GET("/api/v3/collections/:namespace/:name/", handlers.GalaxyLocalCollection(k)).Name = fmt.Sprintf("galaxy::%s::collection", k)
g.GET("/api/v3/collections/:namespace/:name/versions/", handlers.GalaxyLocalCollectionVersions(k)).Name = fmt.Sprintf("galaxy::%s::collection::versions", k)
g.GET("/api/v3/collections/:namespace/:name/versions/:version/", handlers.GalaxyLocalCollectionVersionInfo(k)).Name = fmt.Sprintf("galaxy::%s::collection::version", k)
g.GET("/get/:namespace/:name/:version", handlers.GalaxyLocalCollectionGet(k)).Name = fmt.Sprintf("galaxy::%s::get", k)
} else {
log.Fatalf("[GALAXY] Wrong config definition for [%s], please use url or dir param.", k)
}
}
go func() {
log.Fatal(e.Start(c.String("bind")))
}()
return victoriametrics.ListenMetricsServer(c.String("self-exporter-bind"))
}