Skip to content

Commit

Permalink
ingest: INGEST_PUBLIC_URL as alternative to INGEST_DATA_DOMAIN
Browse files Browse the repository at this point in the history
  • Loading branch information
absorbb committed Feb 1, 2024
1 parent 449618d commit 21f4a34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ingest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ type Config struct {
// # REPOSITORY CONFIG - settings for loading streams from repository
RepositoryConfig `mapstructure:",squash"`

// data domain for ingest endpoint. Required for using 'slug' to identify Stream. e.g. in ingest URL like `http://{slug}.{DATA_DOMAIN}/`
DataDomain string `mapstructure:"DATA_DOMAIN"`
// alternative to DataDomain. data domain will be extracted from this url
PublicURL string `mapstructure:"PUBLIC_URL"`

// For ingest endpoint only
GlobalHashSecret string `mapstructure:"GLOBAL_HASH_SECRET" default:"dea42a58-acf4-45af-85bb-e77e94bd5025"`
Expand Down
19 changes: 17 additions & 2 deletions ingest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"maps"
"net/http"
"net/http/pprof"
"net/url"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -49,6 +50,7 @@ type Router struct {
eventsLogService eventslog.EventsLogService
backupsLogger *BackupLogger
httpClient *http.Client
dataHosts []string
}

type IngestType string
Expand Down Expand Up @@ -90,6 +92,19 @@ func NewRouter(appContext *Context) *Router {
Timeout: time.Duration(appContext.config.DeviceFunctionsTimeoutMs) * time.Millisecond,
}

var dataHosts []string
if appContext.config.DataDomain != "" {
dataHosts = strings.Split(appContext.config.DataDomain, ",")
} else if appContext.config.PublicURL != "" {
u, err := url.ParseRequestURI(appContext.config.PublicURL)
if err != nil {
base.Errorf("Failed to parse %sPUBLIC_URL: %v", appContext.config.AppSetting.EnvPrefixWithUnderscore(), err)
} else {
dataHosts = []string{u.Hostname()}
}
}
base.Infof("Data hosts: %s", dataHosts)

router := &Router{
Router: base,
config: appContext.config,
Expand All @@ -100,6 +115,7 @@ func NewRouter(appContext *Context) *Router {
repository: appContext.repository,
scriptRepository: appContext.scriptRepository,
httpClient: httpClient,
dataHosts: dataHosts,
}
engine := router.Engine()
// get global Monitor object
Expand Down Expand Up @@ -255,7 +271,6 @@ func patchEvent(c *gin.Context, messageId string, event *AnalyticsServerEvent, t
}

func (r *Router) getDataLocator(c *gin.Context, ingestType IngestType, writeKeyExtractor func() string) (cred StreamCredentials, err error) {
dataHosts := strings.Split(r.config.DataDomain, ",")
cred.IngestType = ingestType
if c.GetHeader("Authorization") != "" {
wk := strings.Replace(c.GetHeader("Authorization"), "Basic ", "", 1)
Expand All @@ -271,7 +286,7 @@ func (r *Router) getDataLocator(c *gin.Context, ingestType IngestType, writeKeyE
cred.WriteKey = writeKeyExtractor()
}
host := strings.Split(c.Request.Host, ":")[0]
for _, dataHost := range dataHosts {
for _, dataHost := range r.dataHosts {
if dataHost != "" && strings.HasSuffix(host, "."+dataHost) {
cred.Slug = strings.TrimSuffix(host, "."+dataHost)
return
Expand Down

0 comments on commit 21f4a34

Please sign in to comment.