diff --git a/internal/db/models/repeater_configuration.go b/internal/db/models/repeater_configuration.go index acaabed94..6b50d9592 100644 --- a/internal/db/models/repeater_configuration.go +++ b/internal/db/models/repeater_configuration.go @@ -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 @@ -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) @@ -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 } @@ -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 { diff --git a/internal/dmr/servers/hbrp/packet_handlers.go b/internal/dmr/servers/hbrp/packet_handlers.go index 1552cd50b..f3dbb51c1 100644 --- a/internal/dmr/servers/hbrp/packet_handlers.go +++ b/internal/dmr/servers/hbrp/packet_handlers.go @@ -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 diff --git a/internal/dmr/servers/hbrp/server.go b/internal/dmr/servers/hbrp/server.go index 0d7c9a1d6..bd3b3d2f9 100644 --- a/internal/dmr/servers/hbrp/server.go +++ b/internal/dmr/servers/hbrp/server.go @@ -47,6 +47,8 @@ type Server struct { DB *gorm.DB Redis *servers.RedisClient CallTracker *calltracker.CallTracker + Version string + Commit string } var ( @@ -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{ @@ -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, } } diff --git a/internal/http/api/controllers/v1/meta.go b/internal/http/api/controllers/v1/meta.go index d2a5d1e90..e57fe01c8 100644 --- a/internal/http/api/controllers/v1/meta.go +++ b/internal/http/api/controllers/v1/meta.go @@ -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" ) @@ -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"}) } diff --git a/internal/http/api/controllers/v1/meta_test.go b/internal/http/api/controllers/v1/meta_test.go index abefd4b9a..131b930ed 100644 --- a/internal/http/api/controllers/v1/meta_test.go +++ b/internal/http/api/controllers/v1/meta_test.go @@ -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" ) @@ -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()) } diff --git a/internal/sdk/version.go b/internal/http/api/middleware/version.go similarity index 77% rename from internal/sdk/version.go rename to internal/http/api/middleware/version.go index 0fcb8f4a5..14798e1e4 100644 --- a/internal/sdk/version.go +++ b/internal/http/api/middleware/version.go @@ -17,18 +17,16 @@ // // The source code is available at -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() + } +} diff --git a/internal/http/server.go b/internal/http/server.go index e9b1a6fa6..9262c7552 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -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 { @@ -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) @@ -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 { @@ -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, diff --git a/internal/sdk/version_test.go b/internal/sdk/version_test.go deleted file mode 100644 index a1eeba629..000000000 --- a/internal/sdk/version_test.go +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-or-later -// DMRHub - Run a DMR network server in a single binary -// Copyright (C) 2023 Jacob McSwain -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . -// -// The source code is available at - -package sdk_test - -import ( - "testing" -) - -func TestNoop(t *testing.T) { - t.Parallel() - t.Log("Noop") -} diff --git a/internal/testutils/http.go b/internal/testutils/http.go index 972f26234..6662004d0 100644 --- a/internal/testutils/http.go +++ b/internal/testutils/http.go @@ -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 } diff --git a/main.go b/main.go index fc24e4fd1..74d0fab26 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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() @@ -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) @@ -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)