From a5fe96d2e8772b05cf45fa18bca6d13e22390929 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Sun, 15 Mar 2020 20:45:27 +0000 Subject: [PATCH] feat(api): add info endpoint --- Makefile | 13 ++++++++++--- main.go | 7 ++++--- pkg/api/index.go | 6 +++++- pkg/api/info.go | 28 ++++++++++++++++++++++++++++ pkg/version/version.go | 30 ++++++++++++++++++++++++++++++ version.go | 21 --------------------- 6 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 pkg/api/info.go create mode 100644 pkg/version/version.go delete mode 100644 version.go diff --git a/Makefile b/Makefile index 3337cf4..a6007da 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,15 @@ ARCHIVE=$(APPNAME)-$(GOOS)-$(GOARCH).tgz EXECUTABLE=$(APPNAME)$(EXT) # Extract version infos -VERSION:=`git describe --tags` -LDFLAGS=-ldflags "-X main.Version=$(VERSION)" +PKG_VERSION:=github.com/ncarlier/$(APPNAME)/pkg/version +VERSION:=`git describe --always --dirty` +GIT_COMMIT:=`git rev-list -1 HEAD --abbrev-commit` +BUILT:=`date` +define LDFLAGS +-X '$(PKG_VERSION).Version=$(VERSION)' \ +-X '$(PKG_VERSION).GitCommit=$(GIT_COMMIT)' \ +-X '$(PKG_VERSION).Built=$(BUILT)' +endef all: build @@ -39,7 +46,7 @@ clean: build: -mkdir -p release echo "Building: $(EXECUTABLE) $(VERSION) for $(GOOS)-$(GOARCH) ..." - GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(LDFLAGS) -o release/$(EXECUTABLE) + GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -o release/$(EXECUTABLE) .PHONY: build release/$(EXECUTABLE): build diff --git a/main.go b/main.go index aec7eb5..afdd274 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "github.com/ncarlier/webhookd/pkg/logger" "github.com/ncarlier/webhookd/pkg/notification" "github.com/ncarlier/webhookd/pkg/server" + "github.com/ncarlier/webhookd/pkg/version" "github.com/ncarlier/webhookd/pkg/worker" ) @@ -24,9 +25,9 @@ func main() { flag.Parse() - if *version { - printVersion() - return + if *version.ShowVersion { + version.Print() + os.Exit(0) } level := "info" diff --git a/pkg/api/index.go b/pkg/api/index.go index 9741c60..4c429af 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -57,10 +57,14 @@ func triggerWebhook(w http.ResponseWriter, r *http.Request) { // Get script location p := strings.TrimPrefix(r.URL.Path, "/") + if p == "" { + infoHandler(w, r) + return + } script, err := worker.ResolveScript(scriptDir, p) if err != nil { logger.Error.Println(err.Error()) - http.Error(w, err.Error(), http.StatusNotFound) + http.Error(w, "hook not found", http.StatusNotFound) return } diff --git a/pkg/api/info.go b/pkg/api/info.go new file mode 100644 index 0000000..d800e47 --- /dev/null +++ b/pkg/api/info.go @@ -0,0 +1,28 @@ +package api + +import ( + "encoding/json" + "net/http" + + "github.com/ncarlier/webhookd/pkg/version" +) + +// Info API informations model structure. +type Info struct { + Name string `json:"name"` + Version string `json:"version"` +} + +func infoHandler(w http.ResponseWriter, r *http.Request) { + info := Info{ + Name: "webhookd", + Version: version.Version, + } + data, err := json.Marshal(info) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.Write(data) +} diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000..eedb315 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,30 @@ +package version + +import ( + "flag" + "fmt" +) + +// Version of the app +var Version = "snapshot" + +// GitCommit is the GIT commit revision +var GitCommit = "n/a" + +// Built is the built date +var Built = "n/a" + +// ShowVersion is the flag used to print version +var ShowVersion = flag.Bool("version", false, "Print version") + +// Print version to stdout +func Print() { + fmt.Printf(`Version: %s +Git commit: %s +Built: %s + +Copyright (C) 2020 Nicolas Carlier +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. +`, Version, GitCommit, Built) +} diff --git a/version.go b/version.go deleted file mode 100644 index b1bd957..0000000 --- a/version.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "flag" - "fmt" -) - -// Version of the app -var Version = "snapshot" - -var ( - version = flag.Bool("version", false, "Print version") -) - -func printVersion() { - fmt.Printf(`webhookd (%s) - -Copyright (C) 2020 Nicolas Carlier -This is free software: you are free to change and redistribute it. -There is NO WARRANTY, to the extent permitted by law.`, Version) -}