Skip to content

Commit

Permalink
provide versioning via goreleaser
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Mar 12, 2024
1 parent 73b94d0 commit ec1ca05
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 65 deletions.
13 changes: 6 additions & 7 deletions internal/db/models/repeater_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/USA-RedDragon/DMRHub/internal/dmr/dmrconst"
"github.com/USA-RedDragon/DMRHub/internal/logging"
"github.com/USA-RedDragon/DMRHub/internal/sdk"
)

//go:generate go run github.com/tinylib/msgp
Expand Down Expand Up @@ -74,7 +73,7 @@ const (
packageIDMaxLen = 40
)

func (c *RepeaterConfiguration) ParseConfig(data []byte) error {
func (c *RepeaterConfiguration) ParseConfig(data []byte, version, commit string) error {
c.Callsign = strings.ToUpper(strings.TrimRight(string(data[8:16]), " "))

rxFreq, err := strconv.ParseInt(strings.TrimRight(string(data[16:25]), " "), 0, 32)
Expand Down Expand Up @@ -141,18 +140,18 @@ func (c *RepeaterConfiguration) ParseConfig(data []byte) error {

c.SoftwareID = strings.TrimRight(string(data[222:262]), " ")
if c.SoftwareID == "" {
c.SoftwareID = "USA-RedDragon/DMRHub v" + sdk.Version + "-" + sdk.GitCommit
c.SoftwareID = "USA-RedDragon/DMRHub " + version + "-" + commit
}

c.PackageID = strings.TrimRight(string(data[262:302]), " ")
if c.PackageID == "" {
c.PackageID = "v" + sdk.Version + "-" + sdk.GitCommit
c.PackageID = version + "-" + commit
}

return c.Check()
return c.Check(version, commit)
}

func (c *RepeaterConfiguration) Check() error {
func (c *RepeaterConfiguration) Check(version, commit string) error {
if len(c.Callsign) < 4 || len(c.Callsign) > 8 {
return ErrInvalidCallsign
}
Expand Down Expand Up @@ -196,7 +195,7 @@ func (c *RepeaterConfiguration) Check() error {
if len(c.SoftwareID) > softwareIDMaxLen {
c.SoftwareID = c.SoftwareID[:softwareIDMaxLen]
} else if c.SoftwareID == "" {
c.SoftwareID = "USA-RedDragon/DMRHub v" + sdk.Version + "-" + sdk.GitCommit
c.SoftwareID = "USA-RedDragon/DMRHub v" + version + "-" + commit
}

if len(c.PackageID) > packageIDMaxLen {
Expand Down
2 changes: 1 addition & 1 deletion internal/dmr/servers/hbrp/packet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func (s *Server) handleRPTCPacket(ctx context.Context, remoteAddr net.UDPAddr, d
return
}

err = repeater.ParseConfig(data)
err = repeater.ParseConfig(data, s.Version, s.Commit)
if err != nil {
s.sendCommand(ctx, repeaterID, dmrconst.CommandMSTNAK, repeaterIDBytes)
return
Expand Down
6 changes: 5 additions & 1 deletion internal/dmr/servers/hbrp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Server struct {
DB *gorm.DB
Redis *servers.RedisClient
CallTracker *calltracker.CallTracker
Version string
Commit string
}

var (
Expand All @@ -59,7 +61,7 @@ const repeaterIDLength = 4
const bufferSize = 1000000 // 1MB

// MakeServer creates a new DMR server.
func MakeServer(db *gorm.DB, redis *redis.Client, redisClient *servers.RedisClient, callTracker *calltracker.CallTracker) Server {
func MakeServer(db *gorm.DB, redis *redis.Client, redisClient *servers.RedisClient, callTracker *calltracker.CallTracker, version, commit string) Server {
return Server{
Buffer: make([]byte, largestMessageSize),
SocketAddress: net.UDPAddr{
Expand All @@ -71,6 +73,8 @@ func MakeServer(db *gorm.DB, redis *redis.Client, redisClient *servers.RedisClie
DB: db,
Redis: redisClient,
CallTracker: callTracker,
Version: version,
Commit: commit,
}
}

Expand Down
18 changes: 16 additions & 2 deletions internal/http/api/controllers/v1/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"time"

"github.com/USA-RedDragon/DMRHub/internal/config"
"github.com/USA-RedDragon/DMRHub/internal/sdk"
"github.com/USA-RedDragon/DMRHub/internal/logging"
"github.com/gin-gonic/gin"
)

Expand All @@ -38,7 +38,21 @@ func GETNetworkName(c *gin.Context) {
}

func GETVersion(c *gin.Context) {
_, err := io.WriteString(c.Writer, fmt.Sprintf("%s-%s", sdk.Version, sdk.GitCommit))
version, ok := c.MustGet("Version").(string)
if !ok {
logging.Errorf("Unable to get Version from context")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Try again later"})
return
}

commit, ok := c.MustGet("Commit").(string)
if !ok {
logging.Errorf("Unable to get Commit from context")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Try again later"})
return
}

_, err := io.WriteString(c.Writer, fmt.Sprintf("%s-%s", version, commit))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting version"})
}
Expand Down
3 changes: 0 additions & 3 deletions internal/http/api/controllers/v1/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"testing"
"time"

"github.com/USA-RedDragon/DMRHub/internal/sdk"
"github.com/USA-RedDragon/DMRHub/internal/testutils"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -94,6 +93,4 @@ func TestVersionRoute(t *testing.T) {

assert.Equal(t, 200, w.Code)
assert.NotEmpty(t, w.Body.String())

assert.Equal(t, fmt.Sprintf("%s-%s", sdk.Version, sdk.GitCommit), w.Body.String())
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
//
// The source code is available at <https://github.com/USA-RedDragon/DMRHub>

package sdk
package middleware

import (
// embed the commit.txt file into the binary.
_ "embed"
"github.com/gin-gonic/gin"
)

//go:generate bash -c "bash ../../hack/git_commit.sh > commit.txt"
var (
//go:embed commit.txt
GitCommit string

// Version of the program
Version = "1.0.56" //nolint:golint,gochecknoglobals
)
func VersionProvider(version, commit string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("Version", version)
c.Set("Commit", commit)
c.Next()
}
}
13 changes: 8 additions & 5 deletions internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ const debugWriteTimeout = 60 * time.Second
const rateLimitRate = time.Second
const rateLimitLimit = 10

func MakeServer(db *gorm.DB, redisClient *redis.Client) Server {
func MakeServer(db *gorm.DB, redisClient *redis.Client, version, commit string) Server {
if config.GetConfig().Debug {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}

r := CreateRouter(db, redisClient)
r := CreateRouter(db, redisClient, version, commit)

writeTimeout := defTimeout
if config.GetConfig().Debug {
Expand Down Expand Up @@ -95,7 +95,7 @@ func MakeServer(db *gorm.DB, redisClient *redis.Client) Server {
//go:embed frontend/dist/*
var FS embed.FS

func addMiddleware(r *gin.Engine, db *gorm.DB, redisClient *redis.Client) {
func addMiddleware(r *gin.Engine, db *gorm.DB, redisClient *redis.Client, version, commit string) {
// Debug
if config.GetConfig().Debug {
pprof.Register(r)
Expand All @@ -121,9 +121,12 @@ func addMiddleware(r *gin.Engine, db *gorm.DB, redisClient *redis.Client) {
// Sessions
sessionStore, _ := redisSessions.NewStore(redisClient, config.GetConfig().Secret, config.GetConfig().Secret)
r.Use(sessions.Sessions("sessions", sessionStore))

// Versioning
r.Use(middleware.VersionProvider(version, commit))
}

func CreateRouter(db *gorm.DB, redisClient *redis.Client) *gin.Engine {
func CreateRouter(db *gorm.DB, redisClient *redis.Client, version, commit string) *gin.Engine {
if config.GetConfig().Debug {
gin.SetMode(gin.DebugMode)
} else {
Expand All @@ -139,7 +142,7 @@ func CreateRouter(db *gorm.DB, redisClient *redis.Client) *gin.Engine {
logging.Errorf("Failed setting trusted proxies: %v", err)
}

addMiddleware(r, db, redisClient)
addMiddleware(r, db, redisClient, version, commit)

ratelimitStore := ratelimit.RedisStore(&ratelimit.RedisOptions{
RedisClient: redisClient,
Expand Down
29 changes: 0 additions & 29 deletions internal/sdk/version_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/testutils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,5 @@ func CreateTestDBRouter() (*gin.Engine, *TestDB) {
os.Setenv("TEST", "test")
var t TestDB
t.database = db.MakeDB()
return http.CreateRouter(db.MakeDB(), t.createRedis()), &t
return http.CreateRouter(db.MakeDB(), t.createRedis(), "test", "deadbeef"), &t
}
17 changes: 12 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/USA-RedDragon/DMRHub/internal/logging"
"github.com/USA-RedDragon/DMRHub/internal/metrics"
"github.com/USA-RedDragon/DMRHub/internal/repeaterdb"
"github.com/USA-RedDragon/DMRHub/internal/sdk"
"github.com/USA-RedDragon/DMRHub/internal/userdb"
"github.com/go-co-op/gocron/v2"
"github.com/redis/go-redis/extra/redisotel/v9"
Expand Down Expand Up @@ -88,13 +87,21 @@ func initTracer() func(context.Context) error {
return exporter.Shutdown
}

// https://goreleaser.com/cookbooks/using-main.version/
//
//nolint:golint,gochecknoglobals
var (
version = "dev"
commit = "none"
)

func main() {
os.Exit(start())
}

func start() int {
logging.Errorf("DMRHub v%s-%s", sdk.Version, sdk.GitCommit)
logging.Logf("DMRHub v%s-%s", sdk.Version, sdk.GitCommit)
logging.Errorf("DMRHub v%s-%s", version, commit)
logging.Logf("DMRHub v%s-%s", version, commit)
defer logging.Close()

ctx := context.Background()
Expand Down Expand Up @@ -205,7 +212,7 @@ func start() int {

redisClient := servers.MakeRedisClient(redis)

hbrpServer := hbrp.MakeServer(database, redis, redisClient, callTracker)
hbrpServer := hbrp.MakeServer(database, redis, redisClient, callTracker, version, commit)
err = hbrpServer.Start(ctx)
if err != nil {
logging.Errorf("Failed to start HBRP server: %v", err)
Expand Down Expand Up @@ -245,7 +252,7 @@ func start() int {
}()
}

http := http.MakeServer(database, redis)
http := http.MakeServer(database, redis, version, commit)
err = http.Start()
if err != nil {
logging.Errorf("Failed to start HTTP server %v", err)
Expand Down

0 comments on commit ec1ca05

Please sign in to comment.