feat(controller): added copy by server only currently
This commit is contained in:
@@ -3,89 +3,117 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
socketio "github.com/googollee/go-socket.io"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
//log := logger.New()
|
||||
//log.Info("Warning: .env file not found (ok in Docker/production)", "system", map[string]interface{}{})
|
||||
fmt.Println("Warning: .env file not found")
|
||||
}
|
||||
server := socketio.NewServer(nil)
|
||||
|
||||
// POST /build -> run npm build + increment .build
|
||||
r.POST("/build", func(c *gin.Context) {
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "Could not retrieve hostname"})
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(host)
|
||||
if strings.Contains(host, "VMS") || strings.Contains(host, "vms") {
|
||||
c.JSON(500, gin.H{"error": "You are not allowed to run the build on a production server"})
|
||||
return
|
||||
}
|
||||
// run the old builder first this will be removed once we switch fully over here and shut down the old version
|
||||
if err := runNpmV2Build(); err != nil {
|
||||
c.JSON(500, gin.H{"error": "npm build failed on lstV2", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// the new builder
|
||||
if err := runNpmBuild(); err != nil {
|
||||
c.JSON(500, gin.H{"error": "npm build failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
buildNum, err := bumpBuild()
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "failed updating build counter", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// run the zip
|
||||
includes, _ := loadIncludePatterns("../.include")
|
||||
|
||||
// Name the archive after build number if available
|
||||
data, _ := os.ReadFile("../.build")
|
||||
buildNum1 := strings.TrimSpace(string(data))
|
||||
if buildNum1 == "" {
|
||||
buildNum1 = "0"
|
||||
}
|
||||
|
||||
buildDir, err := getBuildDir()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
//buildDir, err := ensureBuildDir("../builds")
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
zipPath := filepath.Join(buildDir, fmt.Sprintf("release-%d.zip", buildNum))
|
||||
|
||||
if err := zipProject("..", zipPath, includes); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"message": "build successful",
|
||||
"build": buildNum,
|
||||
})
|
||||
server.OnConnect("/", func(s socketio.Conn) error {
|
||||
fmt.Println("✅ Client connected:", s.ID())
|
||||
return nil
|
||||
})
|
||||
|
||||
// GET /version -> read current build version
|
||||
r.GET("/version", func(c *gin.Context) {
|
||||
data, err := os.ReadFile("../.build")
|
||||
if err != nil {
|
||||
c.JSON(404, gin.H{"error": "no build info"})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"build": strings.TrimSpace(string(data))})
|
||||
// ROOM SUBSCRIBE
|
||||
server.OnEvent("/", "subscribe:logs", func(s socketio.Conn) {
|
||||
s.Join("logs")
|
||||
fmt.Println("📺", s.ID(), "joined logs")
|
||||
s.Emit("info", "Subscribed to logs")
|
||||
})
|
||||
|
||||
r.Run(":8080") // serve API
|
||||
server.OnEvent("/", "unsubscribe:logs", func(s socketio.Conn) {
|
||||
s.Leave("logs")
|
||||
fmt.Println("👋", s.ID(), "left logs")
|
||||
s.Emit("info", "Unsubscribed from logs")
|
||||
})
|
||||
|
||||
server.OnEvent("/", "subscribe:errors", func(s socketio.Conn) {
|
||||
s.Join("errors")
|
||||
fmt.Println("📺", s.ID(), "joined errors")
|
||||
s.Emit("info", "Subscribed to errors")
|
||||
})
|
||||
|
||||
server.OnEvent("/", "unsubscribe:errors", func(s socketio.Conn) {
|
||||
s.Leave("errors")
|
||||
fmt.Println("👋", s.ID(), "left errors")
|
||||
s.Emit("info", "Unsubscribed from errors")
|
||||
})
|
||||
|
||||
server.OnDisconnect("/", func(s socketio.Conn, reason string) {
|
||||
fmt.Println("❌ Client disconnected:", s.ID(), reason)
|
||||
})
|
||||
|
||||
// build stuff
|
||||
// Subscribe to build room
|
||||
server.OnEvent("/", "subscribe:build", func(s socketio.Conn) {
|
||||
s.Join("build")
|
||||
fmt.Println("📺", s.ID(), "joined build room")
|
||||
s.Emit("info", "Subscribed to build log room")
|
||||
})
|
||||
|
||||
server.OnEvent("/", "unsubscribe:build", func(s socketio.Conn) {
|
||||
s.Leave("build")
|
||||
fmt.Println("👋", s.ID(), "left build room")
|
||||
s.Emit("info", "Unsubscribed from build log room")
|
||||
})
|
||||
|
||||
registerBuildChannel(server)
|
||||
registerUpdateChannel(server)
|
||||
|
||||
// Broadcast logs to room
|
||||
go func() {
|
||||
for i := 0; ; i++ {
|
||||
time.Sleep(2 * time.Second)
|
||||
msg := fmt.Sprintf("Log line %d @ %s", i, time.Now().Format(time.RFC3339))
|
||||
server.BroadcastToRoom("/", "logs", "logs", msg)
|
||||
}
|
||||
}()
|
||||
|
||||
// Broadcast errors to room
|
||||
go func() {
|
||||
for i := 0; ; i++ {
|
||||
time.Sleep(5 * time.Second)
|
||||
msg := fmt.Sprintf("Error #%d @ %s", i, time.Now().Format(time.RFC3339))
|
||||
server.BroadcastToRoom("/", "errors", "errors", msg)
|
||||
}
|
||||
}()
|
||||
|
||||
go server.Serve()
|
||||
defer server.Close()
|
||||
|
||||
// Enable CORS wrapper for Socket.IO route
|
||||
http.Handle("/socket.io/", withCORS(server))
|
||||
http.Handle("/", http.FileServer(http.Dir("./static")))
|
||||
|
||||
fmt.Println("🚀 Socket.IO server running on :8000")
|
||||
log.Fatal(http.ListenAndServe(":8000", nil))
|
||||
}
|
||||
|
||||
// Reuse your proper CORS handler
|
||||
func withCORS(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
origin := r.Header.Get("Origin")
|
||||
if origin != "" {
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
w.Header().Set("Vary", "Origin")
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user