Add INSTANCE_ID env port offset
This commit is contained in:
parent
b364846c1b
commit
f26460c256
10
src/main.go
10
src/main.go
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
17
src/util.go
17
src/util.go
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user