diff --git a/Makefile b/Makefile index 297136b..b37abbb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ TARGETS = darwin/amd64 darwin/386 linux/amd64 linux/386 windows/amd64 windows/386 GIT_COMMIT = $(shell git rev-parse HEAD) +BUILD_TIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" | tr -d '\n') DOCKER_RELEASE_TAG = "sosedoff/pgweb:$(shell git describe --abbrev=0 --tags | sed 's/v//')" BINDATA_IGNORE = $(shell git ls-files -io --exclude-standard $< | sed 's/^/-ignore=/;s/[.]/[.]/g') @@ -41,7 +42,7 @@ build: assets release: assets gox \ -osarch="$(TARGETS)" \ - -ldflags "-X main.GitCommit $(GIT_COMMIT)" \ + -ldflags "-X github.com/sosedoff/pgweb/pkg/command.GitCommit $(GIT_COMMIT) -X github.com/sosedoff/pgweb/pkg/command.BuildTime $(BUILD_TIME)" \ -output="./bin/pgweb_{{.OS}}_{{.Arch}}" bootstrap: diff --git a/main.go b/main.go index b87a62f..6651cc9 100644 --- a/main.go +++ b/main.go @@ -14,11 +14,6 @@ import ( "github.com/sosedoff/pgweb/pkg/util" ) -const VERSION = "0.5.2" - -// The git commit that was compiled. This will be filled in by the compiler. -var GitCommit string - var options command.Options func exitWithMessage(message string) { @@ -72,9 +67,9 @@ func initOptions() { } func printVersion() { - str := fmt.Sprintf("Pgweb v%s", VERSION) - if GitCommit != "" { - str += fmt.Sprintf(" (git: %s)", GitCommit) + str := fmt.Sprintf("Pgweb v%s", command.VERSION) + if command.GitCommit != "" { + str += fmt.Sprintf(" (git: %s)", command.GitCommit) } fmt.Println(str) diff --git a/pkg/api/api.go b/pkg/api/api.go index 59bd08f..93a1b5e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -197,3 +197,13 @@ func GetBookmarks(c *gin.Context) { bookmarks, err := bookmarks.ReadAll(bookmarks.Path()) serveResult(bookmarks, err, c) } + +func GetInfo(c *gin.Context) { + info := map[string]string{ + "version": command.VERSION, + "git_sha": command.GitCommit, + "build_time": command.BuildTime, + } + + c.JSON(200, info) +} diff --git a/pkg/api/helpers.go b/pkg/api/helpers.go index cf0071c..ad13d66 100644 --- a/pkg/api/helpers.go +++ b/pkg/api/helpers.go @@ -43,6 +43,7 @@ func NewError(err error) Error { // Middleware function to check database connection status before running queries func dbCheckMiddleware() gin.HandlerFunc { allowedPaths := []string{ + "/api/info", "/api/connect", "/api/bookmarks", "/api/history", diff --git a/pkg/api/routes.go b/pkg/api/routes.go index d8f6c39..14b7c8a 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -12,6 +12,7 @@ func SetupRoutes(router *gin.Engine) { { api.Use(dbCheckMiddleware()) + api.GET("/info", GetInfo) api.POST("/connect", Connect) api.GET("/databases", GetDatabases) api.GET("/connection", GetConnectionInfo) diff --git a/pkg/command/version.go b/pkg/command/version.go new file mode 100644 index 0000000..a113848 --- /dev/null +++ b/pkg/command/version.go @@ -0,0 +1,8 @@ +package command + +const VERSION = "0.5.2" + +var ( + GitCommit string + BuildTime string +)