forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (50 loc) · 1.18 KB
/
Copy pathmain.go
File metadata and controls
60 lines (50 loc) · 1.18 KB
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
51
52
53
54
55
56
57
58
59
60
package gateway
import (
"context"
"fmt"
"net/http"
gql "github.com/machinebox/graphql"
configs "github.com/railwayapp/cli/configs"
"github.com/railwayapp/cli/errors"
)
const (
CLI_SOURCE_HEADER = "cli"
)
type Gateway struct {
cfg *configs.Configs
gqlClient *gql.Client
}
func (g *Gateway) authorize(ctx context.Context, header http.Header) error {
user, err := g.cfg.GetUserConfigs()
if err != nil {
return err
}
header.Add("authorization", fmt.Sprintf("Bearer %s", user.Token))
header.Add("x-source", CLI_SOURCE_HEADER)
return nil
}
func (g *Gateway) setProjectToken(ctx context.Context, req *gql.Request) error {
if g.cfg.RailwayProductionToken == "" {
return errors.ProductionTokenNotSet
}
req.Header.Add("project-access-token", g.cfg.RailwayProductionToken)
return nil
}
func GetHost() string {
baseURL := "https://backboard.railway.app"
if configs.IsDevMode() {
baseURL = fmt.Sprintf("http://localhost:8082")
}
return baseURL
}
func GetGQLHost() string {
baseURL := GetHost()
return fmt.Sprintf("%s/graphql", baseURL)
}
func New() *Gateway {
gqlClient := gql.NewClient(GetGQLHost())
return &Gateway{
cfg: configs.New(),
gqlClient: gqlClient,
}
}