feat: add banner printing functionality to server startup

This commit is contained in:
mlogclub
2026-05-24 20:13:18 +08:00
parent 8bbd6c9414
commit c9ad978da6
3 changed files with 86 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package bootstrap
import (
"fmt"
"io"
"os"
"cs-agent/internal/pkg/config"
)
func printBanner() {
printBannerTo(os.Stdout, config.Current())
}
func printBannerTo(w io.Writer, cfg config.Config) {
_, _ = fmt.Fprint(w, renderBanner(cfg))
}
func renderBanner(cfg config.Config) string {
port := cfg.Server.Port
if port <= 0 {
port = 8080
}
dbType := cfg.DB.Type
if dbType == "" {
dbType = "unknown"
}
return fmt.Sprintf(`
____ _ _ _ _ ___
/ ___|| |__ ___| | | / \ |_ _|
\___ \| '_ \ / _ \ | | / _ \ | |
___) | | | | __/ | | / ___ \ | |
|____/|_| |_|\___|_|_| /_/ \_\___|
:: Shell AI ::
Port : %d
DB : %s
Address : http://127.0.0.1:%d
`, port, dbType, port)
}
+42
View File
@@ -0,0 +1,42 @@
package bootstrap
import (
"strings"
"testing"
"cs-agent/internal/pkg/config"
)
func TestRenderBanner(t *testing.T) {
got := renderBanner(config.Config{
Server: config.ServerConfig{Port: 8083},
DB: config.DBConfig{Type: "sqlite"},
})
expected := []string{
":: Shell AI ::",
"Port : 8083",
"DB : sqlite",
"Address : http://127.0.0.1:8083",
}
for _, item := range expected {
if !strings.Contains(got, item) {
t.Fatalf("renderBanner() missing %q in:\n%s", item, got)
}
}
}
func TestRenderBannerDefaults(t *testing.T) {
got := renderBanner(config.Config{})
expected := []string{
"Port : 8080",
"DB : unknown",
"Address : http://127.0.0.1:8080",
}
for _, item := range expected {
if !strings.Contains(got, item) {
t.Fatalf("renderBanner() missing %q in:\n%s", item, got)
}
}
}
+1
View File
@@ -25,6 +25,7 @@ func NewServer() (*gin.Engine, error) {
cfg := config.Current()
gin.SetMode(gin.ReleaseMode)
printBanner()
app := gin.New()
app.Use(corsMiddleware())