ports.go

25 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package internal

import (
	"fmt"

	"congo.gg/dev/models"
)

// AllocatePort finds the next available port starting at 5001.
func AllocatePort() (int, error) {
	services, _ := models.Services.All()
	used := make(map[int]bool)
	for _, s := range services {
		if s.Port > 0 {
			used[s.Port] = true
		}
	}
	// Start at 5002 — port 5001 is used by the dev platform itself.
	for port := 5002; port < 5100; port++ {
		if !used[port] {
			return port, nil
		}
	}
	return 0, fmt.Errorf("no available ports")
}