mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-09 22:24:03 +00:00
feat(api): add info endpoint
This commit is contained in:
parent
14f7d7abf9
commit
a5fe96d2e8
13
Makefile
13
Makefile
|
@ -20,8 +20,15 @@ ARCHIVE=$(APPNAME)-$(GOOS)-$(GOARCH).tgz
|
||||||
EXECUTABLE=$(APPNAME)$(EXT)
|
EXECUTABLE=$(APPNAME)$(EXT)
|
||||||
|
|
||||||
# Extract version infos
|
# Extract version infos
|
||||||
VERSION:=`git describe --tags`
|
PKG_VERSION:=github.com/ncarlier/$(APPNAME)/pkg/version
|
||||||
LDFLAGS=-ldflags "-X main.Version=$(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
|
all: build
|
||||||
|
|
||||||
|
@ -39,7 +46,7 @@ clean:
|
||||||
build:
|
build:
|
||||||
-mkdir -p release
|
-mkdir -p release
|
||||||
echo "Building: $(EXECUTABLE) $(VERSION) for $(GOOS)-$(GOARCH) ..."
|
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
|
.PHONY: build
|
||||||
|
|
||||||
release/$(EXECUTABLE): build
|
release/$(EXECUTABLE): build
|
||||||
|
|
7
main.go
7
main.go
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/ncarlier/webhookd/pkg/logger"
|
"github.com/ncarlier/webhookd/pkg/logger"
|
||||||
"github.com/ncarlier/webhookd/pkg/notification"
|
"github.com/ncarlier/webhookd/pkg/notification"
|
||||||
"github.com/ncarlier/webhookd/pkg/server"
|
"github.com/ncarlier/webhookd/pkg/server"
|
||||||
|
"github.com/ncarlier/webhookd/pkg/version"
|
||||||
"github.com/ncarlier/webhookd/pkg/worker"
|
"github.com/ncarlier/webhookd/pkg/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,9 +25,9 @@ func main() {
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *version {
|
if *version.ShowVersion {
|
||||||
printVersion()
|
version.Print()
|
||||||
return
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
level := "info"
|
level := "info"
|
||||||
|
|
|
@ -57,10 +57,14 @@ func triggerWebhook(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Get script location
|
// Get script location
|
||||||
p := strings.TrimPrefix(r.URL.Path, "/")
|
p := strings.TrimPrefix(r.URL.Path, "/")
|
||||||
|
if p == "" {
|
||||||
|
infoHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
script, err := worker.ResolveScript(scriptDir, p)
|
script, err := worker.ResolveScript(scriptDir, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error.Println(err.Error())
|
logger.Error.Println(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, "hook not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
28
pkg/api/info.go
Normal file
28
pkg/api/info.go
Normal file
|
@ -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)
|
||||||
|
}
|
30
pkg/version/version.go
Normal file
30
pkg/version/version.go
Normal file
|
@ -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)
|
||||||
|
}
|
21
version.go
21
version.go
|
@ -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)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user