forked from chainguard-dev/registry-redirect
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
155 lines (129 loc) · 3.67 KB
/
main.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
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"sync"
"time"
"github.com/chainguard-dev/registry-redirect/pkg/logger"
"github.com/chainguard-dev/registry-redirect/pkg/redirect"
"go.uber.org/zap"
"knative.dev/pkg/logging"
)
// TODO:
// - Also support anonymous and Basic-type auth?
// - take a config for registries/repos to redirect from/to.
var (
// Redirect requests for example.dev/static -> ghcr.io/static
// If repo is empty, example.dev/foo/bar -> ghcr.io/foo/bar
repo = flag.String("repo", "", "repo to redirect to")
// TODO(jason): Support arbitrary registries.
gcr = flag.Bool("gcr", false, "if true, use GCR mode")
// prefix is the user-visible repo prefix.
// For example, if repo is "example" and prefix is "unicorns",
// users hitting example.dev/unicorns/foo/bar will be redirected to
// ghcr.io/example/foo/bar.
// If prefix is unset, hitting example.dev/unicorns/foo/bar will
// redirect to ghcr.io/unicorns/foo/bar.
// If prefix is set, and users hit a path without the prefix, it's ignored:
// - example.dev/foo/bar -> ghcr.io/distroless/foo/bar
// (this is for backward compatibility with prefix-less redirects)
prefix = flag.String("prefix", "", "if set, user-visible repo prefix")
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
syslogHost := os.Getenv("SYSLOG_HOST")
if syslogHost == "" {
syslogHost = "127.0.0.1:514"
}
appName := os.Getenv("APP_NAME")
if appName == "" {
appName = "registry-redirect"
}
// Minimum severity of the messages that the logger will log:
// info, warning, error and fatal
logCfg := logger.Config{
Level: "info",
Component: appName,
Protocol: "tcp",
Address: syslogHost,
}
ctx, syslogger, err := logger.NewLogger(ctx, &logCfg)
if err != nil {
panic(err)
}
logger := logging.FromContext(ctx)
go func() {
oscall := <-c
logger.Infof("system call:%+v", oscall)
cancel()
}()
if err := serve(ctx, logger); err != nil {
logger.Fatalf("failed to serve:+%v\n", err)
}
syslogger.Close()
}
func serve(ctx context.Context, logger *zap.SugaredLogger) (err error) {
flag.Parse()
host := "ghcr.io"
if *gcr {
host = "gcr.io"
}
wg := &sync.WaitGroup{}
r := redirect.New(host, *repo, *prefix)
customHandler := NewCustomHandler(wg, r)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
logger.Info("http server starting...")
srv := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: customHandler,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
go func() {
if err = srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("listen:%+s\n", err)
}
}()
logger.Infof("http server listening on port: %s", port)
<-ctx.Done()
logger.Info("http server stopped")
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err = srv.Shutdown(ctxShutDown); err != nil {
logger.Fatalf("http server shutdown failed:%+s", err)
}
// Wait for in-flight requests to complete before shutting down
wg.Wait()
logger.Infof("http server shutdown gracefully")
if err == http.ErrServerClosed {
err = nil
}
return
}
type CustomHandler struct {
wg *sync.WaitGroup
handler http.Handler
}
func NewCustomHandler(wg *sync.WaitGroup, handler http.Handler) *CustomHandler {
return &CustomHandler{wg: wg, handler: handler}
}
func (h *CustomHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.wg.Add(1)
defer h.wg.Done()
h.handler.ServeHTTP(w, r) // Call your original handler
}