fix(controller): correctly rejected join channel if not on dev server for building
This commit is contained in:
@@ -13,6 +13,17 @@ import (
|
|||||||
func registerBuildChannel(server *socketio.Server) {
|
func registerBuildChannel(server *socketio.Server) {
|
||||||
// Example: When clients join "build" namespace or room
|
// Example: When clients join "build" namespace or room
|
||||||
server.OnEvent("/", "subscribe:build", func(s socketio.Conn) {
|
server.OnEvent("/", "subscribe:build", func(s socketio.Conn) {
|
||||||
|
host, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
server.BroadcastToRoom("/", "build", "buildlogs", "Could not retrieve hostname")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(host, "VMS") || strings.Contains(host, "vms") {
|
||||||
|
server.BroadcastToRoom("/", "build", "buildlogs", "You are not allowed to run the build on a production server")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s.Join("build")
|
s.Join("build")
|
||||||
s.Emit("buildlogs", "👋 Connected to build channel") // this is where all the messages are actually sent to
|
s.Emit("buildlogs", "👋 Connected to build channel") // this is where all the messages are actually sent to
|
||||||
|
|
||||||
@@ -23,16 +34,6 @@ func registerBuildChannel(server *socketio.Server) {
|
|||||||
fmt.Println("🔨 Build triggered:", target)
|
fmt.Println("🔨 Build triggered:", target)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
host, err := os.Hostname()
|
|
||||||
if err != nil {
|
|
||||||
server.BroadcastToRoom("/", "build", "buildlogs", "Could not retrieve hostname")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(host, "VMS") || strings.Contains(host, "vms") {
|
|
||||||
server.BroadcastToRoom("/", "build", "buildlogs", "You are not allowed to run the build on a production server")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
server.BroadcastToRoom("/", "build", "buildlogs", "🔨 Starting build: Old App")
|
server.BroadcastToRoom("/", "build", "buildlogs", "🔨 Starting build: Old App")
|
||||||
if err := runNpmV2Build(server); err != nil {
|
if err := runNpmV2Build(server); err != nil {
|
||||||
|
|||||||
@@ -4,19 +4,33 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
socketio "github.com/googollee/go-socket.io"
|
socketio "github.com/googollee/go-socket.io"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
router "lst.net/internal/route_handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := godotenv.Load("../.env")
|
err := godotenv.Load("../.env")
|
||||||
if err != nil {
|
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")
|
fmt.Println("Warning: .env file not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
port := os.Getenv("PORT")
|
||||||
|
if port == "" {
|
||||||
|
port = "8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
// gin stuff
|
||||||
|
basePath := "/api/controller"
|
||||||
|
if os.Getenv("NODE_ENV") != "production" {
|
||||||
|
basePath = "/lst/api/controller"
|
||||||
|
}
|
||||||
|
r := router.Setup(basePath) // returns *gin.Engine
|
||||||
|
|
||||||
server := socketio.NewServer(nil)
|
server := socketio.NewServer(nil)
|
||||||
|
|
||||||
server.OnConnect("/", func(s socketio.Conn) error {
|
server.OnConnect("/", func(s socketio.Conn) error {
|
||||||
@@ -91,12 +105,16 @@ func main() {
|
|||||||
go server.Serve()
|
go server.Serve()
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
// Enable CORS wrapper for Socket.IO route
|
// mount socket.io on /socket.io/*
|
||||||
http.Handle("/socket.io/", withCORS(server))
|
r.Any("/socket.io/*any", gin.WrapH(withCORS(server)))
|
||||||
http.Handle("/", http.FileServer(http.Dir("./static")))
|
|
||||||
|
|
||||||
fmt.Println("🚀 Socket.IO server running on :8000")
|
// mount a static dir (like http.FileServer)
|
||||||
log.Fatal(http.ListenAndServe(":8000", nil))
|
//r.Static("/", "./static")
|
||||||
|
|
||||||
|
fmt.Println("🚀 Server running on :" + port)
|
||||||
|
if err := r.Run(":" + port); err != nil {
|
||||||
|
log.Fatal("Server failed:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reuse your proper CORS handler
|
// Reuse your proper CORS handler
|
||||||
@@ -104,12 +122,15 @@ func withCORS(h http.Handler) http.Handler {
|
|||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
origin := r.Header.Get("Origin")
|
origin := r.Header.Get("Origin")
|
||||||
if origin != "" {
|
if origin != "" {
|
||||||
|
// 🔑 Echo the request Origin, not "*"
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
w.Header().Set("Vary", "Origin")
|
w.Header().Set("Vary", "Origin")
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
|
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-Headers", "Content-Type, Authorization")
|
||||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
if r.Method == http.MethodOptions {
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user