Skip to content

Commit

Permalink
Add info about version (#300)
Browse files Browse the repository at this point in the history
* Add info about version.

* Fix lint.

* Move 'cmd/internal' package to the project root as 'internal' package.

* Add '/version' HTTP route to the applications HTTP APIs.

* Fix gosec warning.
  • Loading branch information
nickeskov authored Oct 17, 2024
1 parent 588e9ec commit 501e1a9
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 7 deletions.
1 change: 1 addition & 0 deletions Dockerfile-nodemon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN go mod download
COPY Makefile .
COPY cmd cmd
COPY pkg pkg
COPY internal internal

RUN make build-nodemon-linux-amd64

Expand Down
1 change: 1 addition & 0 deletions Dockerfile-nodemon-discord
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN go mod download
COPY Makefile .
COPY cmd cmd
COPY pkg pkg
COPY internal internal

RUN make build-bots-linux-amd64

Expand Down
1 change: 1 addition & 0 deletions Dockerfile-nodemon-telegram
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN go mod download
COPY Makefile .
COPY cmd cmd
COPY pkg pkg
COPY internal internal

RUN make build-bots-linux-amd64

Expand Down
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export GO111MODULE=on

SOURCE_DIRS = cmd pkg
VERSION=$(shell git describe --tags --always --dirty)
SOURCE_DIRS = cmd pkg internal

.PHONY: vendor vetcheck fmtcheck clean build gotest mod-clean

Expand All @@ -20,7 +21,7 @@ clean:
rm -r build/

build:
go build -o build/nodemon ./cmd/nodemon
@go build -o build/nodemon -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/nodemon

gotest:
go test -cover -race -covermode=atomic ./...
Expand All @@ -29,7 +30,8 @@ mod-clean:
go mod tidy

build-bots-linux-amd64:
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon-telegram ./cmd/bots/telegram
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon-discord ./cmd/bots/discord
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon-telegram -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/telegram
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon-discord -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/discord

build-nodemon-linux-amd64:
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon ./cmd/nodemon
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/nodemon
11 changes: 9 additions & 2 deletions cmd/bots/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"nodemon/cmd/bots/internal/common/initial"
"nodemon/cmd/bots/internal/common/messaging"
"nodemon/cmd/bots/internal/discord/handlers"
"nodemon/internal"
"nodemon/pkg/messaging/pair"
"nodemon/pkg/tools"

Expand Down Expand Up @@ -92,6 +93,8 @@ func runDiscordBot() error {
}
}(logger)

logger.Info("Starting discord bot", zap.String("version", internal.Version()))

if validationErr := cfg.validate(logger); validationErr != nil {
return validationErr
}
Expand Down Expand Up @@ -154,12 +157,16 @@ func runDiscordBot() error {
}()
<-ctx.Done()

waitScheduler(taskScheduler, logger)
logger.Info("Discord bot finished")
return nil
}

func waitScheduler(taskScheduler chrono.TaskScheduler, logger *zap.Logger) {
if !taskScheduler.IsShutdown() {
<-taskScheduler.Shutdown()
logger.Info("Task scheduler has been shutdown successfully")
}
logger.Info("Discord bot finished")
return nil
}

func runMessagingClients(
Expand Down
2 changes: 2 additions & 0 deletions cmd/bots/internal/common/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.uber.org/zap"

"nodemon/cmd/bots/internal/common/messaging"
"nodemon/internal"
"nodemon/pkg/messaging/pair"
"nodemon/pkg/tools"
)
Expand Down Expand Up @@ -70,6 +71,7 @@ func (a *BotAPI) routes(logger *zap.Logger) chi.Router {
r.Get("/health", a.health)
r.Handle("/log/level", a.atom)
r.Handle("/metrics", tools.PrometheusHTTPMetricsHandler(mwLog{logger}))
r.Get("/version", internal.VersionHTTPHandler)
return r
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/bots/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"nodemon/cmd/bots/internal/common/messaging"
"nodemon/cmd/bots/internal/telegram/config"
"nodemon/cmd/bots/internal/telegram/handlers"
"nodemon/internal"
"nodemon/pkg/messaging/pair"
"nodemon/pkg/tools"

Expand Down Expand Up @@ -107,6 +108,8 @@ func runTelegramBot() error {
}
}(logger)

logger.Info("Starting telegram bot", zap.String("version", internal.Version()))

if validationErr := cfg.validate(logger); validationErr != nil {
return validationErr
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/nodemon/nodemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/pkg/errors"

"nodemon/internal"
"nodemon/pkg/analysis/l2"

"go.uber.org/zap"
Expand Down Expand Up @@ -297,6 +298,8 @@ func run() error {
}
}(logger)

logger.Info("Starting nodemon", zap.String("version", internal.Version()))

if validateErr := cfg.validate(logger); validateErr != nil {
return validateErr
}
Expand Down
27 changes: 27 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package internal

import (
"encoding/json"
"fmt"
"html"
"net/http"
)

var version = "v0.0.0"

// Version returns the version of the application.
func Version() string { return version }

func VersionHTTPHandler(w http.ResponseWriter, _ *http.Request) {
type resp struct {
Version string `json:"version"`
}
h := w.Header()
h.Set("Content-Type", "application/json")
h.Set("X-Content-Type-Options", "nosniff")
if err := json.NewEncoder(w).Encode(resp{Version: Version()}); err != nil {
msg := fmt.Sprintf("{\"error\":%q}\n", html.EscapeString(err.Error()))
http.Error(w, msg, http.StatusInternalServerError)
return
}
}
24 changes: 24 additions & 0 deletions internal/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package internal_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"nodemon/internal"
)

func TestVersionHTTPHandler(t *testing.T) {
r := httptest.NewRecorder()
internal.VersionHTTPHandler(r, nil)
assert.Equal(t, http.StatusOK, r.Code)
h := r.Header()
assert.Equal(t, "application/json", h.Get("Content-Type"))
assert.Equal(t, "nosniff", h.Get("X-Content-Type-Options"))
body := r.Body.Bytes()
assert.True(t, json.Valid(body))
assert.Equal(t, "{\"version\":\"v0.0.0\"}\n", string(body))
}
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"nodemon/internal"
"nodemon/pkg/entities"
"nodemon/pkg/storing/events"
"nodemon/pkg/storing/nodes"
Expand Down Expand Up @@ -109,6 +110,7 @@ func (a *API) routes(logger *zap.Logger) chi.Router {
r.Get("/health", a.health)
r.Handle("/log/level", a.atom)
r.Handle("/metrics", tools.PrometheusHTTPMetricsHandler(mwLog{logger}))
r.Get("/version", internal.VersionHTTPHandler)
return r
}

Expand Down

0 comments on commit 501e1a9

Please sign in to comment.