Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dashboard #31

Merged
merged 11 commits into from
Jun 4, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ interx/*
syslog-data/*
test/*
src/shidai/test/*
shidai/*
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ services:
- ./interx:/interx
- ./syslog-data:/syslog-data
- /var/run/docker.sock:/var/run/docker.sock
- ./shidai:/shidaid
networks:
- kiranet
hostname: shidai.local
Expand Down
98 changes: 98 additions & 0 deletions dev-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
services:
sekai:
depends_on:
- syslog-ng
image: ghcr.io/kiracore/sekin/sekai:v0.3.45
restart: always
logging:
driver: syslog
options:
syslog-address: "udp://10.1.0.2:514"
syslog-facility: local0
tag: "sekai"
volumes:
- ./sekai:/sekai
ports:
- "26658:26658" # ABCI
- "26657:26657" # RPC
- "26656:26656" # P2P (gRPC)
- "26660:26660" # Prometheus
- "127.0.0.1:8080:8080" # RPC scaller

networks:
- kiranet
hostname: sekai.local

interx:
depends_on:
- syslog-ng
image: ghcr.io/kiracore/sekin/interx:v0.4.48
restart: always
logging:
driver: syslog
options:
syslog-address: "udp://10.1.0.2:514"
syslog-facility: local0
tag: "interx"
volumes:
- ./interx:/interx
ports:
- "11000:11000"
- "127.0.0.1:8081:8081"
networks:
- kiranet
hostname: interx.local

shidai:
depends_on:
- syslog-ng
build:
context: ./
dockerfile: shidai.Dockerfile
restart: always
ports:
- "127.0.0.1:8282:8282"
volumes:
- ./sekai:/sekai
- ./interx:/interx
- ./syslog-data:/syslog-data
- /var/run/docker.sock:/var/run/docker.sock
- ./shidai:/shidaid
networks:
- kiranet
hostname: shidai.local

syslog-ng:
build:
context: ./
dockerfile: syslog-ng.Dockerfile
restart: always
healthcheck:
test: ["CMD", "pgrep", "syslog-ng"]
interval: 10s
timeout: 5s
retries: 3
environment:
- PUID=0
- PGID=0
volumes:
- ./syslog-data:/var/log
ports:
- "514:514/udp"
- "514:514"
networks:
kiranet:
ipv4_address: 10.1.0.2

hostname: syslog-ng.local

networks:
kiranet:
name: kiranet
driver: bridge
ipam:
driver: default
config:
- subnet: 10.1.0.0/16
gateway: 10.1.0.1
86 changes: 12 additions & 74 deletions src/shidai/internal/api/api.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,37 @@
package api

import (
"io"
"net/http"

"github.com/gin-gonic/gin"
"github.com/kiracore/sekin/src/shidai/internal/commands"
"github.com/kiracore/sekin/src/shidai/internal/docker"
"github.com/kiracore/sekin/src/shidai/internal/logger"
"github.com/kiracore/sekin/src/shidai/internal/types"
"github.com/kiracore/sekin/src/shidai/internal/utils"
"github.com/nxadm/tail"
"go.uber.org/zap"
)

var (
log *zap.Logger = logger.GetLogger()
)

func Serve() {
log := logger.GetLogger()

router := gin.New()
router.Use(gin.Recovery())
//manager := sm.NewSubscriptionManager()
if err := initCache(); err != nil {
log.Fatal("Failed to initialize dashboard cache", zap.Error(err))

}

go backgroundUpdate()

router.POST("/api/execute", commands.ExecuteCommandHandler)
router.GET("/logs/shidai", streamLogs(types.ShidaiLogPath))
router.GET("/logs/sekai", streamLogs(types.SekaiLogPath))
router.GET("/logs/interx", streamLogs(types.InterxLogPath))
router.GET("/status", infraStatus())
router.GET("/dashboard", getDashboard())

if err := router.Run(":8282"); err != nil {
log.Error("Failed to start the server", zap.Error(err))
}
}

func streamLogs(logFilePath string) gin.HandlerFunc {
return func(c *gin.Context) {
t, err := tail.TailFile(logFilePath, tail.Config{Follow: true})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to tail log file"})
return
}

c.Writer.Header().Set("Content-Type", "text/plain")
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Flush()

c.Stream(func(w io.Writer) bool {
for line := range t.Lines {
_, err := w.Write([]byte(line.Text + "\n"))
if err != nil {
return false
}
c.Writer.Flush()
}
return true
})
}
}

func infraStatus() gin.HandlerFunc {
return func(c *gin.Context) {
var (
SekaiVersion, InterxVersion, SyslogVersion, ShidaiVersion []byte
err error
)
cm, err := docker.NewContainerManager()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal server error"})
return
}
ctx := c.Request.Context()

if SekaiVersion, err = cm.ExecInContainer(ctx, "sekin-sekai-1", []string{"/sekaid", "version"}); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get Sekai version"})
return
}

if InterxVersion, err = cm.ExecInContainer(ctx, "sekin-interx-1", []string{"/interxd", "version"}); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get Interx version"})
return
}
if SyslogVersion, err = cm.ExecInContainer(ctx, "sekin-syslog-ng-1", []string{"syslog-ng", "--version"}); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get Syslog version"})
return
}

if ShidaiVersion, err = cm.ExecInContainer(ctx, "sekin-shidai-1", []string{"/shidai", "version"}); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get Shidai version"})
return
}

response := types.StatusResponse{
Sekai: types.AppInfo{Version: string(SekaiVersion), Infra: utils.CheckInfra(types.SekaiFiles)},
Interx: types.AppInfo{Version: string(InterxVersion), Infra: utils.CheckInfra(types.InterxFiles)},
Syslog: types.AppInfo{Version: string(SyslogVersion), Infra: utils.CheckInfra(types.SyslogFiles)},
Shidai: types.AppInfo{Version: string(ShidaiVersion), Infra: true},
}
c.JSON(http.StatusOK, response)
}
}
Loading
Loading