-
Notifications
You must be signed in to change notification settings - Fork 8
/
endpoint.go
50 lines (38 loc) · 1.23 KB
/
endpoint.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package endpoint
import (
"context"
"fmt"
"os"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/fnapi"
)
var (
rpcEndpointOverride string
RegionName string
)
func SetupFlags(prefix string, flags *pflag.FlagSet, hide bool) {
endpointFlag := fmt.Sprintf("%sendpoint", prefix)
regionFlag := fmt.Sprintf("%sregion", prefix)
flags.StringVar(&rpcEndpointOverride, endpointFlag, "", "Where to dial to when reaching nscloud.")
flags.StringVar(&RegionName, regionFlag, "eu", "Which region to use.")
if hide {
_ = flags.MarkHidden(endpointFlag)
_ = flags.MarkHidden(regionFlag)
}
}
func ResolveRegionalEndpoint(ctx context.Context, tok fnapi.ResolvedToken) (string, error) {
if rpcEndpointOverride != "" {
return rpcEndpointOverride, nil
}
if rpcEndpoint := os.Getenv("NSC_ENDPOINT"); rpcEndpoint != "" {
return rpcEndpoint, nil
}
if tok.PrimaryRegion != "" {
return "https://api." + tok.PrimaryRegion, nil
}
rpcEndpoint := fmt.Sprintf("https://api.%s.nscluster.cloud", RegionName)
return rpcEndpoint, nil
}