Skip to content

Commit

Permalink
Fix the number of Dart sass transpiler instances spun up
Browse files Browse the repository at this point in the history
  • Loading branch information
KinyaElGrande committed Jul 12, 2024
1 parent 7c17708 commit 26cbf36
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 92 deletions.
104 changes: 50 additions & 54 deletions server/app/boot_levels.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
package app

import (
"context"
"crypto/tls"
"fmt"
"github.com/cortezaproject/corteza/server/pkg/sass"
"net/url"
"os"
"regexp"
"strings"
"time"

authService "github.com/cortezaproject/corteza/server/auth"
"github.com/cortezaproject/corteza/server/auth/saml"
authSettings "github.com/cortezaproject/corteza/server/auth/settings"
autService "github.com/cortezaproject/corteza/server/automation/service"
cmpService "github.com/cortezaproject/corteza/server/compose/service"
cmpEvent "github.com/cortezaproject/corteza/server/compose/service/event"
discoveryService "github.com/cortezaproject/corteza/server/discovery/service"
fedService "github.com/cortezaproject/corteza/server/federation/service"
"github.com/cortezaproject/corteza/server/pkg/actionlog"
"github.com/cortezaproject/corteza/server/pkg/apigw"
apigwTypes "github.com/cortezaproject/corteza/server/pkg/apigw/types"
"github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/corredor"
"github.com/cortezaproject/corteza/server/pkg/eventbus"
"github.com/cortezaproject/corteza/server/pkg/healthcheck"
"github.com/cortezaproject/corteza/server/pkg/http"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/locale"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/cortezaproject/corteza/server/pkg/mail"
"github.com/cortezaproject/corteza/server/pkg/messagebus"
"github.com/cortezaproject/corteza/server/pkg/monitor"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/pkg/provision"
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/pkg/scheduler"
"github.com/cortezaproject/corteza/server/pkg/sentry"
"github.com/cortezaproject/corteza/server/pkg/valuestore"
"github.com/cortezaproject/corteza/server/pkg/version"
"github.com/cortezaproject/corteza/server/pkg/websocket"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/service"
sysService "github.com/cortezaproject/corteza/server/system/service"
sysEvent "github.com/cortezaproject/corteza/server/system/service/event"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/lestrrat-go/jwx/jwt"
"go.uber.org/zap"
gomail "gopkg.in/mail.v2"
"context"
"crypto/tls"
"fmt"
"net/url"
"os"
"regexp"
"strings"
"time"

authService "github.com/cortezaproject/corteza/server/auth"
"github.com/cortezaproject/corteza/server/auth/saml"
authSettings "github.com/cortezaproject/corteza/server/auth/settings"
autService "github.com/cortezaproject/corteza/server/automation/service"
cmpService "github.com/cortezaproject/corteza/server/compose/service"
cmpEvent "github.com/cortezaproject/corteza/server/compose/service/event"
discoveryService "github.com/cortezaproject/corteza/server/discovery/service"
fedService "github.com/cortezaproject/corteza/server/federation/service"
"github.com/cortezaproject/corteza/server/pkg/actionlog"
"github.com/cortezaproject/corteza/server/pkg/apigw"
apigwTypes "github.com/cortezaproject/corteza/server/pkg/apigw/types"
"github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/corredor"
"github.com/cortezaproject/corteza/server/pkg/eventbus"
"github.com/cortezaproject/corteza/server/pkg/healthcheck"
"github.com/cortezaproject/corteza/server/pkg/http"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/locale"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/cortezaproject/corteza/server/pkg/mail"
"github.com/cortezaproject/corteza/server/pkg/messagebus"
"github.com/cortezaproject/corteza/server/pkg/monitor"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/pkg/provision"
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/pkg/scheduler"
"github.com/cortezaproject/corteza/server/pkg/sentry"
"github.com/cortezaproject/corteza/server/pkg/valuestore"
"github.com/cortezaproject/corteza/server/pkg/version"
"github.com/cortezaproject/corteza/server/pkg/websocket"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/service"
sysService "github.com/cortezaproject/corteza/server/system/service"
sysEvent "github.com/cortezaproject/corteza/server/system/service/event"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/lestrrat-go/jwx/jwt"
"go.uber.org/zap"
gomail "gopkg.in/mail.v2"
)

const (
Expand Down Expand Up @@ -561,12 +560,11 @@ func (app *CortezaApp) Activate(ctx context.Context) (err error) {
updateDiscoverySettings(app.Opt.Discovery, service.CurrentSettings)
updateLocaleSettings(app.Opt.Locale)

updateSassInstallSettings(ctx, app.Log)

app.AuthService.Watch(ctx)

updateSassInstallSettings(ctx, sysService.DefaultStylesheet.SassInstalled(), app.Log)
//Generate CSS for webapps
if err = service.GenerateCSS(sysService.CurrentSettings, app.Opt.Webapp.ScssDirPath, app.Log); err != nil {
if err = sysService.DefaultStylesheet.GenerateCSS(sysService.CurrentSettings, app.Opt.Webapp.ScssDirPath, app.Log); err != nil {
return fmt.Errorf("could not generate css for webapps: %w", err)
}

Expand Down Expand Up @@ -970,9 +968,7 @@ func updateSmtpSettings(log *zap.Logger, current *types.AppSettings) {
setupSmtpDialer(log, current.SMTP.Servers...)
}

func updateSassInstallSettings(ctx context.Context, log *zap.Logger) {
sassInstalled := sass.DartSassTranspiler(log) != nil

func updateSassInstallSettings(ctx context.Context, sassInstalled bool, log *zap.Logger) {
// update dart-sass installed setting
err := updateSetting(ctx, "ui.studio.sass-installed", sassInstalled)
if err != nil {
Expand Down
63 changes: 41 additions & 22 deletions server/system/service/service.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package service

import (
"context"
"errors"
"time"

automationService "github.com/cortezaproject/corteza/server/automation/service"
discoveryService "github.com/cortezaproject/corteza/server/discovery/service"
"github.com/cortezaproject/corteza/server/pkg/actionlog"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/eventbus"
"github.com/cortezaproject/corteza/server/pkg/healthcheck"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/cortezaproject/corteza/server/pkg/objstore"
"github.com/cortezaproject/corteza/server/pkg/objstore/minio"
"github.com/cortezaproject/corteza/server/pkg/objstore/plain"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/pkg/valuestore"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/automation"
"github.com/cortezaproject/corteza/server/system/types"
"go.uber.org/zap"
"context"
"errors"
"github.com/bep/godartsass/v2"
"time"

automationService "github.com/cortezaproject/corteza/server/automation/service"
discoveryService "github.com/cortezaproject/corteza/server/discovery/service"
"github.com/cortezaproject/corteza/server/pkg/actionlog"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/cortezaproject/corteza/server/pkg/eventbus"
"github.com/cortezaproject/corteza/server/pkg/healthcheck"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/cortezaproject/corteza/server/pkg/objstore"
"github.com/cortezaproject/corteza/server/pkg/objstore/minio"
"github.com/cortezaproject/corteza/server/pkg/objstore/plain"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/pkg/valuestore"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/automation"
"github.com/cortezaproject/corteza/server/system/types"
"go.uber.org/zap"
)

type (
Expand Down Expand Up @@ -62,6 +63,8 @@ var (
// DefaultSettings controls system's settings
DefaultSettings *settings

DefaultStylesheet *stylesheet

// DefaultAccessControl Access control checking
DefaultAccessControl *accessControl

Expand Down Expand Up @@ -152,9 +155,12 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, ws websock
}
}

sassTranspiler := dartSassTranspiler(log)

DefaultAccessControl = AccessControl(s)

DefaultSettings = Settings(ctx, DefaultStore, DefaultLogger, DefaultAccessControl, DefaultActionlog, CurrentSettings, c.Webapps)
DefaultStylesheet = Stylesheet(sassTranspiler, log)

DefaultDalConnection = Connection(ctx, dal.Service(), c.DB)

Expand Down Expand Up @@ -344,3 +350,16 @@ func isStale(new *time.Time, updatedAt *time.Time, createdAt time.Time) bool {

return new.Equal(createdAt)
}

func dartSassTranspiler(log *zap.Logger) *godartsass.Transpiler {
transpiler, err := godartsass.Start(godartsass.Options{
DartSassEmbeddedFilename: "sass",
})

if err != nil {
log.Warn("dart sass is not installed in your system", zap.Error(err))
return nil
}

return transpiler
}
2 changes: 1 addition & 1 deletion server/system/service/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (svc *settings) BulkSet(ctx context.Context, vv types.SettingValueSet) (err
compStyles = current.FindByName("ui.studio.themes")
}

updateCSS(v, current.FindByName(v.Name), compStyles, v.Name, svc.webappsConf.ScssDirPath, svc.logger)
DefaultStylesheet.updateCSS(v, current.FindByName(v.Name), compStyles, v.Name, svc.webappsConf.ScssDirPath, svc.logger)
}

svc.logChange(ctx, v)
Expand Down
43 changes: 28 additions & 15 deletions server/system/service/stylesheet.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package service

import (
"fmt"
"github.com/cespare/xxhash/v2"
"strings"
"fmt"
"github.com/bep/godartsass/v2"
"github.com/cespare/xxhash/v2"
"github.com/cortezaproject/corteza/server/pkg/sass"
"github.com/cortezaproject/corteza/server/system/types"
"go.uber.org/zap"
"strings"
)

"github.com/cortezaproject/corteza/server/pkg/sass"
"github.com/cortezaproject/corteza/server/system/types"
"go.uber.org/zap"
type (
stylesheet struct {
transpiler *godartsass.Transpiler
logger *zap.Logger
}
)

func Stylesheet(transpiler *godartsass.Transpiler, logger *zap.Logger) *stylesheet {
return &stylesheet{
transpiler: transpiler,
logger: logger,
}
}

// GenerateCSS takes care of creating CSS for webapps by reading SASS content from embedded assets,
// combining it with different themeSASS and customCSS themes, and then transpiling it using the dart-sass compiler.
//
Expand All @@ -18,7 +32,7 @@ import (
// If dart isn't installed on the host machine, customCustom css will continue to function, but without sass support.
//
// In case of an error, it will return default css and log out the error
func GenerateCSS(settings *types.AppSettings, sassDirPath string, log *zap.Logger) (err error) {
func (svc *stylesheet) GenerateCSS(settings *types.AppSettings, sassDirPath string, log *zap.Logger) (err error) {
var (
studio = settings.UI.Studio
customCSSMap = make(map[string]string)
Expand All @@ -39,20 +53,18 @@ func GenerateCSS(settings *types.AppSettings, sassDirPath string, log *zap.Logge
return
}

transpiler := sass.DartSassTranspiler(log)

// transpile sass to css for each theme
for _, theme := range studio.Themes {
if studio.CustomCSS == nil {
err := sass.Transpile(transpiler, log, theme.ID, theme.Values, "", sassDirPath)
err := sass.Transpile(svc.transpiler, log, theme.ID, theme.Values, "", sassDirPath)
if err != nil {
continue
}
}

customCSS := processCustomCSS(theme.ID, customCSSMap)
// transpile sass to css
err := sass.Transpile(transpiler, log, theme.ID, theme.Values, customCSS, sassDirPath)
err := sass.Transpile(svc.transpiler, log, theme.ID, theme.Values, customCSS, sassDirPath)
if err != nil {
continue
}
Expand All @@ -61,6 +73,9 @@ func GenerateCSS(settings *types.AppSettings, sassDirPath string, log *zap.Logge
return
}

func (svc *stylesheet) SassInstalled() bool {
return svc.transpiler != nil
}
// processCustomCSS, processes CustomCSS input and gives priority to theme specific customCSS
func processCustomCSS(themeID string, customCSSMap map[string]string) (customCSS string) {
var stringsBuilder strings.Builder
Expand All @@ -82,16 +97,14 @@ func processCustomCSS(themeID string, customCSSMap map[string]string) (customCSS
}

// updateCSS, updates theme css when ui.studio.themes or ui.studio.custom-css settings are updated
func updateCSS(current, old, compStyles *types.SettingValue, name, sassDirPath string, log *zap.Logger) {
transpiler := sass.DartSassTranspiler(log)

func (svc *stylesheet) updateCSS(current, old, compStyles *types.SettingValue, name, sassDirPath string, log *zap.Logger) {
complimentaryStylesMap := themeMap(compStyles)
oldThemesMap := themeMap(old)
currentThemesMap := themeMap(current)

transpileSASS := func(themeID, themeSASS string, themeCustomCSS map[string]string) {
customCSS := processCustomCSS(themeID, themeCustomCSS)
err := sass.Transpile(transpiler, log, themeID, themeSASS, customCSS, sassDirPath)
err := sass.Transpile(svc.transpiler, log, themeID, themeSASS, customCSS, sassDirPath)
if err != nil {
log.Error("failed to transpile sass to css", zap.Error(err))
}
Expand Down

0 comments on commit 26cbf36

Please sign in to comment.