You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: api.go
    path of file: ./repos/convoy/api
    
    the code of the file until where you have to start completion: package api

import (
	"embed"
	"io/fs"
	"net/http"
	"path"
	"strings"

	authz "github.com/Subomi/go-authz"
	"github.com/frain-dev/convoy/api/handlers"
	"github.com/frain-dev/convoy/api/policies"
	"github.com/frain-dev/convoy/api/types"
	"github.com/frain-dev/convoy/config"
	"github.com/frain-dev/convoy/database/postgres"
	"github.com/frain-dev/convoy/internal/pkg/metrics"
	"github.com/frain-dev/convoy/internal/pkg/middleware"
	redisqueue "github.com/frain-dev/convoy/queue/redis"
	"github.com/go-chi/chi/v5"
	chiMiddleware "github.com/go-chi/chi/v5/middleware"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/subomi/requestmigrations"
)

//go:embed ui/build
var reactFS embed.FS

func reactRootHandler(rw http.ResponseWriter, req *http.Request) {
	p := req.URL.Path
	if !strings.HasPrefix(p, "/") {
		p = "/" + p
		req.URL.Path = p
	}
	p = path.Clean(p)
	f := fs.FS(reactFS)
	static, err := fs.Sub(f, "ui/build")
	if err != nil {
		return
	}
	if _, err := static.Open(strings.TrimLeft(p, "/")); err != nil { // If file not found server index/html from root
		req.URL.Path = "/"
	}
	http.FileServer(http.FS(static)).ServeHTTP(rw, req)
}

const (
	GET    = "GET"
	POST   = "POST"
	PUT    = "PUT"
	DELETE = "DELETE"
)

const (
	VersionHeader = "X-Convoy-Version"
	serverName    = "apiserver"
)

type ApplicationHandler struct {
	Router http.Handler
	rm     *requestmigrations.RequestMigration
	A      *types.APIOptions
}

func NewApplicationHandler(a *types.APIOptions) (*ApplicationHandler, error) {
	appHandler := &ApplicationHandler{A: a}

	az, err := authz.NewAuthz(&authz.AuthzOpts{
		AuthCtxKey: authz.AuthCtxType(middleware.AuthUserCtx),
	})
	if err != nil {
		return nil, err
	}
	appHandler.A.Authz = az

	opts := &requestmigrations.RequestMigrationOptions{
		VersionHeader:  VersionHeader,
		CurrentVersion: config.DefaultAPIVersion,
		GetUserVersionFunc: func(req *http.Request) (string, error) {
			cfg, err := config.Get()
			if err != nil {
				return "", err
			}

			return cfg.APIVersion, nil
		},
		VersionFormat: requestmigrations.DateFormat,
	}
	rm, err 
    
func (a *ApplicationHandler) BuildDataPlaneRoutes() *chi.Mux {
	router := a.buildRouter()

	// Ingestion API.
	router.Route("/ingest", func(ingestRouter chi.Router) {
		ingestRouter.Get("/{maskID}", a.HandleCrcCheck)
		ingestRouter.Post("/{maskID}", a.IngestEvent)
	})

	handler := &handlers.Handler{A: a.A, RM: a.rm}

	// Public API.
	router.Route("/api", func(v1Router chi.Router) {
		v1Router.Route("/v1", func(r chi.Router) {
			r.Use(chiMiddleware.AllowContentType("application/json"))
			r.Use(middleware.JsonResponse)
			r.Use(middleware.RequireAuth())

			r.Route("/projects", func(projectRouter chi.Router) {
				projectRouter.Route("/{projectID}", func(projectSubRouter chi.Router) {
					projectSubRouter.Route("/events", func(eventRouter chi.Router) {
						// TODO(all): should the InstrumentPath change?
						eventRouter.With(middleware.InstrumentPath("/events")).Post("/", handler.CreateEndpointEvent)
						eventRouter.Post("/fanout", handler.CreateEndpointFanoutEvent)
						eventRouter.Post("/broadcast", handler.CreateBroadcastEvent)
						eventRouter.Post("/dynamic", handler.CreateDynamicEvent)
						eventRouter.With(middleware.Pagination).Get("/", handler.GetEventsPaged)
						eventRouter.Post("/batchreplay", handler.BatchReplayEvents)

						eventRouter.Route("/{eventID}", func(eventSubRouter chi.Router) {
							eventSubRouter.Get("/", handler.GetEndpointEvent)
							eventSubRouter.Put("/replay", handler.ReplayEndpointEvent)
						})
					})

					projectSubRouter.Route("/eventdeliveries", func(eventDeliveryRouter chi.Router) {
						eventDeliveryRouter.With(middleware.Pagination).Get("/", handler.GetEventDeliveriesPaged)
						eventDeliveryRouter.Post("/forceresend", handler.ForceResendEventDeliveries)
						eventDeliveryRouter.Post("/batchretry", handler.BatchRetryEventDelivery)

						eventDeliveryRouter.Route("/{eventDeliveryID}", func(eventDeliverySubRouter chi.Router) {
							eventDeliverySubRouter.Get("/", handler.GetEventDelivery)
							eventDeliverySubRouter.Put("/resend", handler.ResendEventDelivery)

							eventDeliverySubRouter.Route("/deliveryattempts", func(deliveryRouter chi.Router) {
								deliveryRouter.Get("/", handler.GetDeliveryAttempts)
								deliveryRouter.Get("/{deliveryAttemptID}", handler.GetDeliveryAttempt)
							})
						})
					})
				})
			})
		})
	})

	// Dashboard API.
	router.Route("/ui", func(uiRouter chi.Router) {
		uiRouter.Use(middleware.JsonResponse)
		uiRouter.Use(chiMiddleware.Maybe(middleware.RequireAuth(), shouldAuthRoute))

		// TODO(subomi): added these back for the tests to pass.
		// What should we do in the future?
		uiRouter.Route("/auth", func(authRouter chi.Router) {
			authRouter.Post("/login", handler.LoginUser)
			authRouter.Post("/register", handler.RegisterUser)
			authRouter.Post("/token/refresh", handler.RefreshToken)
			authRouter.Post("/logout", handler.LogoutUser)
		})

		uiRouter.Route("/organisations", func(orgRouter chi.Router) {
			orgRouter.Route("/{orgID}", func(orgSubRouter chi.Router) {
				orgSubRouter.Route("/projects", func(projectRouter chi.Router) {
					projectRouter.Route("/{projectID}", func(projectSubRouter chi.Router) {
						projectSubRouter.Route("/events", func(eventRouter chi.Router) {
							eventRouter.Post("/", handler.CreateEndpointEvent)
							eventRouter.Post("/fanout", handler.CreateEndpointFanoutEvent)
							eventRouter.With(middleware.Pagination).Get("/", handler.GetEventsPaged)
							eventRouter.Post("/batchreplay", handler.BatchReplayEvents)
							eventRouter.Get("/countbatchreplayevents", handler.CountAffectedEvents)

							eventRouter.Route("/{eventID}", func(eventSubRouter chi.Router) {
								eventSubRouter.Get("/", handler.GetEndpointEvent)
								eventSubRouter.Put("/replay", handler.ReplayEndpointEvent)
							})
						})

						projectSubRouter.Route("/eventdeliveries", func(eventDeliveryRouter chi.Router) {
							eventDeliveryRouter.With(middleware.Pagination).Get("/", handler.GetEventDeliveriesPaged)
							eventDeliveryRouter.Post("/forceresend", handler.ForceResendEventDeliveries)
							eventDeliveryRouter.Post("/batchretry", handler.BatchRetryEventDelivery)
							eventDeliveryRouter.Get("/countbatchretryevents", handler.CountAffectedEventDeliveries)

							eventDeliveryRouter.Route("/{eventDeliveryID}", func(eventDeliverySubRouter chi.Router) {
								eventDeliverySubRouter.Get("/", handler.GetEventDelivery)
								eventDeliverySubRouter.Put("/resend", handler.ResendEventDelivery)

								eventDeliverySubRouter.Route("/deliveryattempts", func(deliveryRouter chi.Router) {
									deliveryRouter.Get("/", handler.GetDeliveryAttempts)
									deliveryRouter.Get("/{deliveryAttemptID}", handler.GetDeliveryAttempt)
								})
							})
						})
					})
				})
			})
		})
	})

	// Portal Link API.
	router.Route("/portal-api", func(portalLinkRouter chi.Router) {
		portalLinkRouter.Use(middleware.JsonResponse)
		portalLinkRouter.Use(middleware.SetupCORS)
		portalLinkRouter.Use(middleware.RequireAuth())

		portalLinkRouter.Route("/events", func(eventRouter chi.Router) {
			eventRouter.Post("/", handler.CreateEndpointEvent)
			eventRouter.With(middleware.Pagination).Get("/", handler.GetEventsPaged)
			eventRouter.Post("/batchreplay", handler.BatchReplayEvents)
			eventRouter.Get("/countbatchreplayevents", handler.CountAffectedEvents)

			eventRouter.Route("/{eventID}", func(eventSubRouter chi.Router) {
				eventSubRouter.Get("/", handler.GetEndpointEvent)
				eventSubRouter.Put("/replay", handler.ReplayEndpointEvent)
			})
		})

		portalLinkRouter.Route("/eventdeliveries", func(eventDeliveryRouter chi.Router) {
			eventDeliveryRouter.With(middleware.Pagination).Get("/", handler.GetEventDeliveriesPaged)
			eventDeliveryRouter.Post("/forceresend", handler.ForceResendEventDeliveries)
			eventDeliveryRouter.Post("/batchretry", handler.BatchRetryEventDelivery)
			eventDeliveryRouter.Get("/countbatchretryevents", handler.CountAffectedEventDeliveries)

			eventDeliveryRouter.Route("/{eventDeliveryID}", func(eventDeliverySubRouter chi.Router) {
				eventDeliverySubRouter.Get("/", handler.GetEventDelivery)
				eventDeliverySubRouter.Put("/resend", handler.ResendEventDelivery)

				eventDeliverySubRouter.Route("/deliveryattempts", func(deliveryRouter chi.Router) {
					deliveryRouter.Get("/", handler.GetDeliveryAttempts)
					deliveryRouter.Get("/{deliveryAttemptID}", handler.GetDeliveryAttempt)
				})
			})
		})
	})

	a.Router = router

	return router
}