-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
59 lines (48 loc) · 1.31 KB
/
serve.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
// Copyright 2022 Reto Gantenbein
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"fmt"
"github.com/ganto/pkgproxy/pkg/pkgproxy"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/spf13/cobra"
)
var (
listenAddress string
listenPort uint16
)
const (
defaultAddress = "localhost"
defaultPort = 8080
)
func newServeCommand() *cobra.Command {
c := &cobra.Command{
Use: "serve",
Args: cobra.ArbitraryArgs,
Short: "Start forward proxy",
RunE: startServer,
TraverseChildren: true,
}
c.PersistentFlags().StringVar(&listenAddress, "host", defaultAddress, "listen address of the pkgproxy.")
c.PersistentFlags().Uint16Var(&listenPort, "port", defaultPort, "listen port of the pkgproxy.")
return c
}
func startServer(_ *cobra.Command, _ []string) error {
app := echo.New()
app.HideBanner = true
app.Use(middleware.Logger())
app.Use(middleware.Recover())
if enableDebug {
app.Logger.SetLevel(log.DEBUG)
}
pkgProxy := pkgproxy.New(&pkgproxy.PkgProxyConfig{
CacheBasePath: cacheDir,
RepositoryConfig: &repoConfig,
})
app.Use(pkgProxy.Cache)
app.Use(pkgProxy.ForwardProxy)
app.Logger.Fatal(app.Start(fmt.Sprintf("%s:%d", listenAddress, listenPort)))
return nil
}