Skip to content

Commit

Permalink
Specify pro server via URL rather than separate host and path
Browse files Browse the repository at this point in the history
  • Loading branch information
hwh33 committed Sep 29, 2024
1 parent 06e1934 commit 93d38a0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
3 changes: 1 addition & 2 deletions desktop/app/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package app
import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -200,7 +199,7 @@ func startApp(t *testing.T, helper *integrationtest.Helper) (*App, error) {
return settings.UserConfig(ss)
},
}
proClient := pro.NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), webclientOpts)
proClient := pro.NewClient(ProAPIBaseURL, webclientOpts)

a := NewApp(flags, helper.ConfigDir, proClient, ss)
id := ss.GetUserID()
Expand Down
2 changes: 1 addition & 1 deletion desktop/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func init() {
userConfig := func() common.UserConfig {
return settings.UserConfig(ss)
}
proClient = proclient.NewClient(common.ProAPIHost, common.ProAPIPath, &webclient.Opts{
proClient = proclient.NewClient(common.ProAPIBaseURL, &webclient.Opts{
UserConfig: userConfig,
})
authClient = auth.NewClient(fmt.Sprintf("https://%s", common.APIBaseURL), userConfig)
Expand Down
12 changes: 6 additions & 6 deletions internalsdk/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ var (
// GlobalStagingURL is the URL for fetching the global config in a staging environment.
GlobalStagingURL = "https://globalconfig.flashlightproxy.com/global.yaml.gz"

ProAPIHost = "df.iantem.io"
ProAPIPath = "/api/pro-server"
// ProAPIBaseURL is the URL for all requests to the back-end pro server. Paths at this URL can
// be hit directly (not recommended due to censorship), through proxies, or through domain
// fronting.
ProAPIBaseURL = "df.iantem.io/api/pro-server"

// APIBaseURL is the URL for all requests to the back-end "API service". Paths at this URL can
// be hit directly, through proxies, or through domain fronting.
// be hit directly (not recommended due to censorship), through proxies, or through domain
// fronting.
APIBaseURL = "df.iantem.io/api/v1"

log = golog.LoggerFor("flashlight.common")
Expand All @@ -65,9 +68,6 @@ func ForceStaging() {
func initInternal() {
isStaging := IsStagingEnvironment()
log.Debugf("****************************** stagingMode: %v", isStaging)
if isStaging {
ProAPIHost = "api-staging.getiantem.org"
}
forceAds, _ = strconv.ParseBool(os.Getenv("FORCEADS"))
}

Expand Down
16 changes: 9 additions & 7 deletions internalsdk/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type ProClient interface {
}

// NewClient creates a new instance of ProClient
func NewClient(baseHost, basePath string, opts *webclient.Opts) ProClient {
func NewClient(baseURL string, opts *webclient.Opts) ProClient {
if opts.HttpClient == nil {
opts.HttpClient = &http.Client{
// The default http.RoundTripper used by the ProClient is ParallelForIdempotent which
Expand All @@ -70,24 +70,26 @@ func NewClient(baseHost, basePath string, opts *webclient.Opts) ProClient {

if opts.OnBeforeRequest == nil {
opts.OnBeforeRequest = func(client *resty.Client, req *http.Request) error {
prepareProRequest(req, baseHost, basePath, opts.UserConfig())
prepareProRequest(req, opts.UserConfig())
return nil
}
}

if opts.BaseURL == "" {
opts.BaseURL = baseURL
}

return &proClient{
userConfig: opts.UserConfig,
RESTClient: webclient.NewRESTClient(opts),
}
}

// prepareProRequest normalizes requests to the pro server with device ID, user ID, etc set. The
// host is always set to baseHost and basePath is prepended to the request path.
func prepareProRequest(r *http.Request, baseHost, basePath string, userConfig common.UserConfig) {
// prepareProRequest normalizes requests to the pro server with device ID, user ID, etc set.
func prepareProRequest(r *http.Request, userConfig common.UserConfig) {
if r.URL.Scheme == "" {
r.URL.Scheme = "http"
}
r.URL.Host = baseHost
r.URL.Path = basePath + r.URL.Path
r.RequestURI = "" // http: Request.RequestURI can't be set in client requests.
r.Header.Set("Access-Control-Allow-Headers", strings.Join([]string{
common.DeviceIdHeader,
Expand Down
2 changes: 1 addition & 1 deletion internalsdk/pro/pro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package pro
// },
// }

// proClient := NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), webclientOpts)
// proClient := NewClient(common.ProAPIBaseURL, webclientOpts)

// puchaseData := map[string]interface{}{
// "idempotencyKey": strconv.FormatInt(time.Now().UnixNano(), 10),
Expand Down
2 changes: 1 addition & 1 deletion internalsdk/session_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func NewSessionModel(mdb minisql.DB, opts *SessionModelOpts) (*SessionModel, err
)
},
}
m.proClient = pro.NewClient(common.ProAPIHost, common.ProAPIPath, webclientOpts)
m.proClient = pro.NewClient(common.ProAPIBaseURL, webclientOpts)

m.authClient = auth.NewClient(common.APIBaseURL, webclientOpts.UserConfig)

Expand Down

0 comments on commit 93d38a0

Please sign in to comment.