Skip to content

Commit

Permalink
chore: test app on fly
Browse files Browse the repository at this point in the history
Test deploying the app on fly, redirect to ngrok -> local vector instance

Signed-off-by: grouville <[email protected]>
  • Loading branch information
grouville authored and grouville committed Aug 3, 2023
1 parent 1773f2f commit eabb86d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 153 deletions.
5 changes: 2 additions & 3 deletions magefiles/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"dagger.io/dagger"
"github.com/containers/image/v5/docker/reference"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

Expand All @@ -26,7 +25,7 @@ const (
// https://hub.docker.com/r/flyio/flyctl/tags
flyctlVersion = "0.1.65"

appName = "dagger-registry-2023-01-23"
appName = "dagger-registry-2023-07-28"
appImageRegistry = "registry.fly.io"
binaryName = "registry-redirect"

Expand Down Expand Up @@ -189,7 +188,7 @@ func deploy(ctx context.Context, c *dagger.Client, imageRef string) {

// [lints, tests], builds, publishes & deploys a new version of the app
func All(ctx context.Context) {
mg.CtxDeps(ctx, Lint, Test)
// mg.CtxDeps(ctx, Lint, Test)

c := daggerClient(ctx)
defer c.Close()
Expand Down
29 changes: 24 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"os"
"os/signal"
"sync"
"time"

"github.com/chainguard-dev/registry-redirect/pkg/logger"
Expand Down Expand Up @@ -55,9 +56,9 @@ func main() {
// info, warning, error and fatal
logCfg := logger.Config{
Level: "info",
Component: "dagger-registry-2023-01-23",
Component: "dagger-registry-2023-07-28",
Protocol: "tcp",
Address: "localhost:1514",
Address: "toto.localhost:16901", // 0.tcp.eu.ngrok.io:16901
}

ctx, syslogger, err := logger.NewLogger(ctx, &logCfg)
Expand Down Expand Up @@ -85,8 +86,9 @@ func serve(ctx context.Context, logger *zap.SugaredLogger) (err error) {
if *gcr {
host = "gcr.io"
}
wg := &sync.WaitGroup{}
r := redirect.New(host, *repo, *prefix)
http.Handle("/", r)
customHandler := NewCustomHandler(wg, r)

port := os.Getenv("PORT")
if port == "" {
Expand All @@ -95,7 +97,7 @@ func serve(ctx context.Context, logger *zap.SugaredLogger) (err error) {
logger.Info("http server starting...")
srv := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: nil,
Handler: customHandler,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
go func() {
Expand All @@ -112,6 +114,9 @@ func serve(ctx context.Context, logger *zap.SugaredLogger) (err error) {
cancel()
}()

// Wait for in-flight requests to complete before shutting down
wg.Wait()

if err = srv.Shutdown(ctxShutDown); err != nil {
logger.Fatalf("http server shutdown failed:%+s", err)
}
Expand All @@ -121,6 +126,20 @@ func serve(ctx context.Context, logger *zap.SugaredLogger) (err error) {
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
}
Loading

0 comments on commit eabb86d

Please sign in to comment.