diff --git a/config/config.go b/config/config.go index a3f2111..01f06c3 100644 --- a/config/config.go +++ b/config/config.go @@ -39,6 +39,8 @@ var DockerClientID, DockerClientSecret string var PlaygroundDomain string +var SegmentId string + func ParseFlags() { flag.StringVar(&LetsEncryptCertsDir, "letsencrypt-certs-dir", "/certs", "Path where let's encrypt certs will be stored") flag.BoolVar(&UseLetsEncrypt, "letsencrypt-enable", false, "Enabled let's encrypt tls certificates") @@ -74,6 +76,8 @@ func ParseFlags() { flag.StringVar(&PlaygroundDomain, "playground-domain", "localhost", "Domain to use for the playground") flag.StringVar(&AdminToken, "admin-token", "", "Token to validate admin user for admin endpoints") + flag.StringVar(&SegmentId, "segment-id", "", "Segment id to post metrics") + flag.Parse() SecureCookie = securecookie.New([]byte(CookieHashKey), []byte(CookieBlockKey)) diff --git a/handlers/bootstrap.go b/handlers/bootstrap.go index c29cf4c..9d35369 100644 --- a/handlers/bootstrap.go +++ b/handlers/bootstrap.go @@ -1,9 +1,12 @@ package handlers import ( + "bytes" "context" "crypto/tls" "fmt" + "html/template" + "io/ioutil" "log" "net/http" "time" @@ -22,6 +25,7 @@ import ( var core pwd.PWDApi var e event.EventApi +var landings = map[string][]byte{} type HandlerExtender func(h *mux.Router) @@ -31,6 +35,8 @@ func Bootstrap(c pwd.PWDApi, ev event.EventApi) { } func Register(extend HandlerExtender) { + initLandings() + r := mux.NewRouter() corsRouter := mux.NewRouter() @@ -143,3 +149,29 @@ func Register(extend HandlerExtender) { log.Fatal(httpServer.ListenAndServe()) } } + +func initLandings() { + pgs, err := core.PlaygroundList() + if err != nil { + log.Fatal("Error getting playgrounds to initialize landings") + } + for _, p := range pgs { + if p.AssetsDir == "" { + p.AssetsDir = "default" + } + + var b bytes.Buffer + t, err := template.New("landing.html").Delims("[[", "]]").ParseFiles(fmt.Sprintf("./www/%s/landing.html", p.AssetsDir)) + if err != nil { + log.Fatalf("Error parsing template %v", err) + } + if err := t.Execute(&b, struct{ SegmentId string }{config.SegmentId}); err != nil { + log.Fatalf("Error executing template %v", err) + } + landingBytes, err := ioutil.ReadAll(&b) + if err != nil { + log.Fatalf("Error reading template bytes %v", err) + } + landings[p.Id] = landingBytes + } +} diff --git a/handlers/home.go b/handlers/home.go index 474f3f7..3fc46f9 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -46,10 +46,6 @@ func Landing(rw http.ResponseWriter, req *http.Request) { return } - index := filepath.Join("./www", playground.AssetsDir, "/landing.html") - if _, err := os.Stat(index); os.IsNotExist(err) { - index = "./www/default/landing.html" - } + rw.Write(landings[playground.Id]) - http.ServeFile(rw, req, index) } diff --git a/www/default/landing.html b/www/default/landing.html index 0579fa1..f4ff0d1 100644 --- a/www/default/landing.html +++ b/www/default/landing.html @@ -64,6 +64,16 @@ + + [[ if .SegmentId ]] + + [[ end ]] + @@ -79,9 +89,15 @@ method: 'GET', url: '/users/me' }).then(function(response) { + [[ if .SegmentId ]] + analytics.identify(response.data.email); + [[ end ]] $scope.user = response.data; $scope.loggedIn = true; }, function(response) { + [[ if .SegmentId ]] + analytics.identify(); + [[ end ]] console.log('ERROR', response); $scope.user = null; $scope.loggedIn = false; diff --git a/www/k8s/landing.html b/www/k8s/landing.html index 2322e95..d7a8ff0 100644 --- a/www/k8s/landing.html +++ b/www/k8s/landing.html @@ -64,6 +64,16 @@ + + [[ if .SegmentId ]] + + [[ end ]] + @@ -79,9 +89,15 @@ method: 'GET', url: '/users/me' }).then(function(response) { + [[ if .SegmentId ]] + analytics.identify(response.data.email); + [[ end ]] $scope.user = response.data; $scope.loggedIn = true; }, function(response) { + [[ if .SegmentId ]] + analytics.identify(); + [[ end ]] console.log('ERROR', response); $scope.user = null; $scope.loggedIn = false;