From 7f5d02179ea47c9e01bdd1caccf0f6fe50e6fd7f Mon Sep 17 00:00:00 2001
From: JP <37535226+RabbITCybErSeC@users.noreply.github.com>
Date: Fri, 6 Sep 2024 15:52:38 +0200
Subject: [PATCH] added status (#46)
* added status
* removed unused param
* addressed review maarten
---
Makefile | 4 ++--
go.mod | 10 +++++-----
go.sum | 20 ++++++++++----------
internal/status/status.go | 20 ++++++++++++++++++++
models/status/status.go | 16 ++++++++++++++++
server/main.go | 8 ++++++--
views/components/navbar/navbar.templ | 7 +++++--
7 files changed, 64 insertions(+), 21 deletions(-)
create mode 100644 internal/status/status.go
create mode 100644 models/status/status.go
diff --git a/Makefile b/Makefile
index 0ef2671..3b9de56 100644
--- a/Makefile
+++ b/Makefile
@@ -9,11 +9,11 @@ BUILDTIME := $(shell date '+%Y-%m-%dT%T%z')
GOLDFLAGS += -X main.Version=$(VERSION)
GOLDFLAGS += -X main.Buildtime=$(BUILDTIME)
GOFLAGS = -ldflags "$(GOLDFLAGS)"
+
#-----------------------------------------------------
# DEV
#-----------------------------------------------------
-
dev:
@make -j dev-templ dev-tailwind dev-server
@@ -21,7 +21,7 @@ dev-server:
# run air to detect any go file changes to re-build and re-run the server.
@go run github.com/air-verse/air@latest \
- --build.cmd "templ generate && go build --tags dev -o tmp/bin/main ./server/" --build.bin "tmp/bin/main" --build.delay "100" \
+ --build.cmd "templ generate && go build -ldflags \"-X main.Version=$(VERSION)\" --tags dev -o tmp/bin/main ./server/" --build.bin "tmp/bin/main" --build.delay "100" \
--build.exclude_dir "node_modules" \
--build.exclude_regex ".*_templ.go" \
--build.include_ext "go,templ" \
diff --git a/go.mod b/go.mod
index 429beaa..beea72e 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module soarca-gui
go 1.22.2
require (
- github.com/a-h/templ v0.2.747
+ github.com/a-h/templ v0.2.771
github.com/gin-gonic/gin v1.10.0
github.com/stretchr/testify v1.9.0
)
@@ -35,10 +35,10 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
- golang.org/x/crypto v0.23.0 // indirect
- golang.org/x/net v0.25.0 // indirect
- golang.org/x/sys v0.21.0 // indirect
- golang.org/x/text v0.15.0 // indirect
+ golang.org/x/crypto v0.26.0 // indirect
+ golang.org/x/net v0.28.0 // indirect
+ golang.org/x/sys v0.23.0 // indirect
+ golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
diff --git a/go.sum b/go.sum
index 212820c..716efe9 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
-github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
+github.com/a-h/templ v0.2.771 h1:4KH5ykNigYGGpCe0fRJ7/hzwz72k3qFqIiiLLJskbSo=
+github.com/a-h/templ v0.2.771/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -70,16 +70,16 @@ github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
-golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
-golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
-golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
-golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
+golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
+golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
+golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
+golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
-golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
-golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
-golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
+golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
+golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
+golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
diff --git a/internal/status/status.go b/internal/status/status.go
new file mode 100644
index 0000000..965a08a
--- /dev/null
+++ b/internal/status/status.go
@@ -0,0 +1,20 @@
+package status
+
+import (
+ "runtime"
+ "soarca-gui/models/status"
+ "soarca-gui/utils"
+ "time"
+)
+
+var internalStatus = status.Status{Uptime: status.Uptime{Since: time.Now(), Milliseconds: 0},
+ Mode: utils.GetEnv("LOG_MODE", "production"),
+ Runtime: runtime.GOOS}
+
+func SetVersion(version string) {
+ internalStatus.Version = version
+}
+
+func GetVersion() string {
+ return internalStatus.Version
+}
diff --git a/models/status/status.go b/models/status/status.go
new file mode 100644
index 0000000..8bdd0c2
--- /dev/null
+++ b/models/status/status.go
@@ -0,0 +1,16 @@
+package status
+
+import "time"
+
+type Uptime struct {
+ Since time.Time `json:"since"`
+ Milliseconds uint64 `json:"milliseconds"`
+}
+
+type Status struct {
+ Version string `json:"version"`
+ Runtime string `json:"runtime"`
+ Mode string `json:"mode"`
+ Time time.Time `json:"time"`
+ Uptime Uptime `json:"uptime"`
+}
diff --git a/server/main.go b/server/main.go
index d840661..3a7d142 100644
--- a/server/main.go
+++ b/server/main.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
+ "soarca-gui/internal/status"
"soarca-gui/routes"
"soarca-gui/utils"
@@ -11,18 +12,21 @@ import (
)
var (
- Version string
- Buildtime string
+ Version string = "dev"
+ Buildtime string = ""
)
// @title SOARCA-GUI
// @version 0.0.1
func main() {
+ fmt.Println("Version: ", Version)
+ fmt.Println("Buildtime ", Buildtime)
errenv := godotenv.Load(".env")
if errenv != nil {
fmt.Println("Failed to read env variable, but will continue")
}
+ status.SetVersion(Version)
app := gin.Default()
routes.Setup(app)
diff --git a/views/components/navbar/navbar.templ b/views/components/navbar/navbar.templ
index 36e9028..ed82e22 100644
--- a/views/components/navbar/navbar.templ
+++ b/views/components/navbar/navbar.templ
@@ -1,6 +1,9 @@
package navbar
-import "soarca-gui/views/components/icons"
+import (
+ "soarca-gui/internal/status"
+ "soarca-gui/views/components/icons"
+)
const (
soarcaLogoUrlPath = "/public/assets/icons/soarca-logo.svg"
@@ -24,7 +27,7 @@ templ Navsidebar(title string, homeLink string, version string, documentationUrl
@icons.NewIcon("book").InsertIcon("mr-3")
Documentation
- GUI Version: { version }
+ Version: { status.GetVersion() }
}