Add INSTANCE_ID env port offset

This commit is contained in:
Jacob Gunther 2023-05-19 14:29:30 -05:00
parent b364846c1b
commit f26460c256
No known key found for this signature in database
GPG Key ID: 9E6F3F4BF45EC433
2 changed files with 25 additions and 2 deletions

View File

@ -74,9 +74,15 @@ func init() {
func main() {
defer r.Close()
log.Printf("Listening on %s:%d\n", conf.Host, conf.Port)
instanceID, err := GetInstanceID()
if err := app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port)); err != nil {
if err != nil {
log.Fatal(err)
}
log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID)
if err := app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port+instanceID)); err != nil {
log.Fatalf("failed to start server: %v", err)
}
}

View File

@ -6,7 +6,9 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
@ -124,6 +126,21 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) {
return result[0], uint16(port), nil
}
// GetInstanceID returns the INSTANCE_ID environment variable parsed as an unsigned 16-bit integer.
func GetInstanceID() (uint16, error) {
if instanceID := os.Getenv("INSTANCE_ID"); len(instanceID) > 0 {
value, err := strconv.ParseUint(instanceID, 10, 16)
if err != nil {
log.Fatal(err)
}
return uint16(value), nil
}
return 0, nil
}
// PointerOf returns a pointer of the argument passed.
func PointerOf[T any](v T) *T {
return &v