Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convoy version header #2174

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package api

import (
"embed"
"fmt"
"github.com/frain-dev/convoy"
"github.com/frain-dev/convoy/util"
"github.com/go-chi/render"
"io/fs"
"net/http"
"path"
Expand Down Expand Up @@ -43,13 +47,6 @@ func reactRootHandler(rw http.ResponseWriter, req *http.Request) {
http.FileServer(http.FS(static)).ServeHTTP(rw, req)
}

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

const (
VersionHeader = "X-Convoy-Version"
serverName = "apiserver"
Expand Down Expand Up @@ -114,6 +111,7 @@ func (a *ApplicationHandler) buildRouter() *chi.Mux {
router.Use(chiMiddleware.RequestID)
router.Use(chiMiddleware.Recoverer)
router.Use(middleware.WriteRequestIDHeader)
router.Use(middleware.WriteVersionHeader(VersionHeader, a.cfg.APIVersion))
router.Use(middleware.InstrumentRequests(serverName, router))
router.Use(middleware.LogHttpRequest(a.A))
router.Use(chiMiddleware.Maybe(middleware.SetupCORS, shouldApplyCORS))
Expand Down Expand Up @@ -495,6 +493,10 @@ func (a *ApplicationHandler) BuildControlPlaneRoutes() *chi.Mux {
router.HandleFunc("/metrics", promhttp.HandlerFor(metrics.Reg(), promhttp.HandlerOpts{Registry: metrics.Reg()}).ServeHTTP)
}

router.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
_ = render.Render(w, r, util.NewServerResponse(fmt.Sprintf("Convoy %v", convoy.GetVersion()), nil, http.StatusOK))
})

router.HandleFunc("/*", reactRootHandler)

a.Router = router
Expand All @@ -509,6 +511,10 @@ func (a *ApplicationHandler) BuildDataPlaneRoutes() *chi.Mux {
router.HandleFunc("/metrics", promhttp.HandlerFor(metrics.Reg(), promhttp.HandlerOpts{Registry: metrics.Reg()}).ServeHTTP)
}

router.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
_ = render.Render(w, r, util.NewServerResponse(fmt.Sprintf("Convoy %v", convoy.GetVersion()), nil, http.StatusOK))
})

// Ingestion API.
router.Route("/ingest", func(ingestRouter chi.Router) {
ingestRouter.Use(middleware.RateLimiterHandler(a.A.Rate, a.cfg.ApiRateLimit))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/slack-go/slack v0.10.2
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stealthrocket/netjail v0.1.2
github.com/stretchr/testify v1.9.0
github.com/subomi/requestmigrations v0.4.0
github.com/swaggo/swag v1.16.3
Expand Down Expand Up @@ -205,7 +206,6 @@ require (
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
github.com/stealthrocket/netjail v0.1.2 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
Expand Down
11 changes: 10 additions & 1 deletion internal/pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ func WriteRequestIDHeader(next http.Handler) http.Handler {
})
}

func WriteVersionHeader(header, version string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(header, version)
next.ServeHTTP(w, r)
})
}
}

func CanAccessFeature(fflag *fflag.FFlag, featureKey fflag.FeatureFlagKey) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -351,7 +360,7 @@ func requestLogFields(r *http.Request) map[string]interface{} {
return requestFields
}

func responseLogFields(w middleware.WrapResponseWriter, wbuf *bytes.Buffer, t time.Time, lvl logrus.Level) map[string]interface{} {
func responseLogFields(w middleware.WrapResponseWriter, wbuf *bytes.Buffer, t time.Time, _ logrus.Level) map[string]interface{} {
responseFields := map[string]interface{}{
"status": w.Status(),
"byes": w.BytesWritten(),
Expand Down
2 changes: 1 addition & 1 deletion util/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ValidateEndpoint(s string, enforceSecure bool) (string, error) {
return "", errors.New("only https endpoints allowed")
}
case "https":
client := &http.Client{Timeout: 60 * time.Second, Transport: &http.Transport{
client := &http.Client{Timeout: 10 * time.Second, Transport: &http.Transport{
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := tls.Dial(network, addr, &tls.Config{MinVersion: tls.VersionTLS12})
return conn, err
Expand Down
Loading