API and Database Deployment Update

This commit is contained in:
2026-02-04 06:53:46 +00:00
parent 1d0ccca7d1
commit e902e5f320
27 changed files with 2156 additions and 395 deletions

View File

@@ -1,18 +1,16 @@
package ports
import (
"fmt"
"math/rand"
"net"
"sync"
)
type Manager struct {
mu sync.Mutex
startPort int
endPort int
allocations map[string]int
}
func NewManager(start, end int) *Manager {
return &Manager{
startPort: start,
@@ -20,18 +18,15 @@ func NewManager(start, end int) *Manager {
allocations: make(map[string]int),
}
}
func (m *Manager) GetPort(appName string, specificPort int) (int, error) {
m.mu.Lock()
defer m.mu.Unlock()
if port, exists := m.allocations[appName]; exists {
if specificPort > 0 && specificPort != port {
return 0, fmt.Errorf("app %s is already running on port %d", appName, port)
}
return port, nil
}
if specificPort > 0 {
if err := m.checkPortAvailable(specificPort); err != nil {
return 0, err
@@ -39,8 +34,10 @@ func (m *Manager) GetPort(appName string, specificPort int) (int, error) {
m.allocations[appName] = specificPort
return specificPort, nil
}
for port := m.startPort; port <= m.endPort; port++ {
rangeSize := m.endPort - m.startPort + 1
offsets := rand.Perm(rangeSize)
for _, offset := range offsets {
port := m.startPort + offset
if err := m.checkPortAvailable(port); err == nil {
if !m.isPortAllocatedInternal(port) {
m.allocations[appName] = port
@@ -48,10 +45,8 @@ func (m *Manager) GetPort(appName string, specificPort int) (int, error) {
}
}
}
return 0, fmt.Errorf("no available ports in range %d-%d", m.startPort, m.endPort)
}
func (m *Manager) isPortAllocatedInternal(port int) bool {
for _, p := range m.allocations {
if p == port {
@@ -60,16 +55,13 @@ func (m *Manager) isPortAllocatedInternal(port int) bool {
}
return false
}
func (m *Manager) checkPortAvailable(port int) error {
if port < m.startPort || port > m.endPort {
return fmt.Errorf("port %d is out of allowed range %d-%d", port, m.startPort, m.endPort)
}
if m.isPortAllocatedInternal(port) {
return fmt.Errorf("port %d is internally allocated", port)
}
addr := fmt.Sprintf("localhost:%d", port)
conn, err := net.Dial("tcp", addr)
if err != nil {