Skip to content

Commit

Permalink
Add /version endpoint and --version cli flag
Browse files Browse the repository at this point in the history
Closes #18

Allow the server operator a way to view the current RAS version
  • Loading branch information
jgknight committed Aug 29, 2024
1 parent 2e0cf41 commit c9ef402
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"log/slog"
"net"
Expand All @@ -18,7 +19,30 @@ import (
"github.com/kelseyhightower/envconfig"
)

// Default build fields are populated by GoReleaser
var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
bld := config.Build{
Version: version,
Commit: commit,
Date: date,
}
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "Display build information")
flag.BoolVar(&showVersion, "v", false, "Display build information")
flag.Parse()
if showVersion {
fmt.Printf("%-10s %s\n", "version:", bld.Version)
fmt.Printf("%-10s %s\n", "commit:", bld.Commit)
fmt.Printf("%-10s %s\n", "date:", bld.Date)
os.Exit(0)
}

var cfg config.Config
err := envconfig.Process("", &cfg)
if err != nil {
Expand Down Expand Up @@ -47,7 +71,7 @@ func main() {
wg.Add(7)

go func() {
http.StartManagementAPI(cfg, feedbagStore, sessionManager, feedbagStore, feedbagStore, chatSessionManager, sessionManager, feedbagStore, feedbagStore, feedbagStore, feedbagStore, logger)
http.StartManagementAPI(bld, cfg, feedbagStore, sessionManager, feedbagStore, feedbagStore, chatSessionManager, sessionManager, feedbagStore, feedbagStore, feedbagStore, feedbagStore, logger)
wg.Done()
}()
go func(logger *slog.Logger) {
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ type Config struct {
LogLevel string `envconfig:"LOG_LEVEL" required:"true" val:"info" description:"Set logging granularity. Possible values: 'trace', 'debug', 'info', 'warn', 'error'."`
OSCARHost string `envconfig:"OSCAR_HOST" required:"true" val:"127.0.0.1" description:"The hostname that AIM clients connect to in order to reach OSCAR services (auth, BOS, BUCP, etc). Make sure the hostname is reachable by all clients. For local development, the default loopback address should work provided the server and AIM client(s) are running on the same machine. For LAN-only clients, a private IP address (e.g. 192.168..) or hostname should suffice. For clients connecting over the Internet, specify your public IP address and ensure that TCP ports 5190-5196 are open on your firewall."`
}

type Build struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}
16 changes: 16 additions & 0 deletions server/http/mgmt_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

func StartManagementAPI(
bld config.Build,
cfg config.Config,
userManager UserManager,
sessionRetriever SessionRetriever,
Expand Down Expand Up @@ -96,6 +97,11 @@ func StartManagementAPI(
postInstantMessageHandler(w, r, messageRelayer, logger)
})

// Handlers for '/version' route
mux.HandleFunc("GET /version", func(w http.ResponseWriter, r *http.Request) {
getVersionHandler(w, bld)
})

addr := net.JoinHostPort(cfg.ApiHost, cfg.ApiPort)
logger.Info("starting management API server", "addr", addr)
if err := http.ListenAndServe(addr, mux); err != nil {
Expand Down Expand Up @@ -569,3 +575,13 @@ func getUserAccountHandler(w http.ResponseWriter, r *http.Request, userManager U
return
}
}

// getVersionHandler handles the GET /version endpoint.
func getVersionHandler(w http.ResponseWriter, bld config.Build) {
w.Header().Set("Content-Type", "application/json")
fmt.Printf(bld.Version)
if err := json.NewEncoder(w).Encode(bld); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
37 changes: 37 additions & 0 deletions server/http/mgmt_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/mk6i/retro-aim-server/config"
"github.com/mk6i/retro-aim-server/state"
"github.com/mk6i/retro-aim-server/wire"
)
Expand Down Expand Up @@ -1248,3 +1249,39 @@ func TestInstantMessageHandler_POST(t *testing.T) {
})
}
}

func TestVersionHandler_GET(t *testing.T) {
tt := []struct {
name string
want string
statusCode int
buildInfo config.Build
}{
{
name: "get ras version",
want: `{"version":"13.3.7","commit":"asdfASDF12345678","date":"2024-03-01"}`,
statusCode: http.StatusOK,
buildInfo: config.Build{
Version: "13.3.7",
Commit: "asdfASDF12345678",
Date: "2024-03-01",
},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
responseRecorder := httptest.NewRecorder()

getVersionHandler(responseRecorder, tc.buildInfo)

if responseRecorder.Code != tc.statusCode {
t.Errorf("Want status '%d', got '%d'", tc.statusCode, responseRecorder.Code)
}

if strings.TrimSpace(responseRecorder.Body.String()) != tc.want {
t.Errorf("Want '%s', got '%s'", tc.want, responseRecorder.Body)
}
})
}
}

0 comments on commit c9ef402

Please sign in to comment.