diff --git a/cmd/api/main.go b/cmd/api/main.go index 1acd796..5db0d82 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -136,8 +136,11 @@ func setupLogging() { func makeServer(db *sql.DB, cfg *config.Config) *http.Server { routerConfig := router.Config{ - Database: db, - JWTSecret: cfg.JWTSecret, + Database: db, + JWTSecret: cfg.JWTSecret, + ServiceVersion: Version, + ServiceCommit: Commit, + ServiceBuildTime: BuildTime, } mux := router.NewMux(routerConfig) diff --git a/internal/api/handler/version.go b/internal/api/handler/version.go new file mode 100644 index 0000000..37f3bec --- /dev/null +++ b/internal/api/handler/version.go @@ -0,0 +1,23 @@ +package handler + +import ( + "net/http" + + "github.com/robindittmar/dttmr-api/internal/api/response" +) + +type Version struct { + Version string `json:"version"` + Commit string `json:"commit"` + BuildTime string `json:"buildTime"` +} + +func VersionHandler(version string, commit string, buildTime string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + response.JSON(r.Context(), w, http.StatusOK, Version{ + Version: version, + Commit: commit, + BuildTime: buildTime, + }) + } +} diff --git a/internal/api/router/router.go b/internal/api/router/router.go index f1f74bc..2a3f3f2 100644 --- a/internal/api/router/router.go +++ b/internal/api/router/router.go @@ -11,8 +11,11 @@ import ( ) type Config struct { - Database *sql.DB - JWTSecret string + Database *sql.DB + JWTSecret string + ServiceVersion string + ServiceCommit string + ServiceBuildTime string } func NewMux(cfg Config) http.Handler { @@ -32,6 +35,8 @@ func NewMux(cfg Config) http.Handler { protected := middleware.WithJWT(authService) apiMux := http.NewServeMux() + apiMux.HandleFunc("GET /version", handler.VersionHandler( + cfg.ServiceVersion, cfg.ServiceCommit, cfg.ServiceBuildTime)) apiMux.HandleFunc("GET /health", handler.HealthHandler) // Auth