Skip to content

Commit

Permalink
Add option to not compress static files (siglens#1670)
Browse files Browse the repository at this point in the history
* Add option to not compress static files

* Default config value to true

* Make compression enable Brotli compression
  • Loading branch information
AndrewHess authored Sep 25, 2024
1 parent d6c2f9a commit bf75587
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
14 changes: 8 additions & 6 deletions pkg/config/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ type Configuration struct {
AgileAggsEnabledConverted bool
DualCaseCheck string `yaml:"dualCaseCheck"` // This is to support the old data that does not have case-insensitive search support. TODO: Remove this after 2 months from now: Aug 21st, 2024.
DualCaseCheckConverted bool
QueryHostname string `yaml:"queryHostname"` // hostname of the query server. i.e. if DNS is https://cloud.siglens.com, this should be cloud.siglens.com
IngestUrl string `yaml:"ingestUrl"` // full address of the ingest server, including scheme and port, e.g. https://ingest.siglens.com:8080
S3 S3Config `yaml:"s3"` // s3 related config
Log LogConfig `yaml:"log"` // Log related config
TLS TLSConfig `yaml:"tls"` // TLS related config
Tracing TracingConfig `yaml:"tracing"` // Tracing related config
QueryHostname string `yaml:"queryHostname"` // hostname of the query server. i.e. if DNS is https://cloud.siglens.com, this should be cloud.siglens.com
IngestUrl string `yaml:"ingestUrl"` // full address of the ingest server, including scheme and port, e.g. https://ingest.siglens.com:8080
S3 S3Config `yaml:"s3"` // s3 related config
Log LogConfig `yaml:"log"` // Log related config
TLS TLSConfig `yaml:"tls"` // TLS related config
CompressStatic string `yaml:"compressStatic"` // compress static files
CompressStaticConverted bool
Tracing TracingConfig `yaml:"tracing"` // Tracing related config
EmailConfig EmailConfig `yaml:"emailConfig"`
DatabaseConfig DatabaseConfig `yaml:"minionSearch"`
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func GetTLSPrivateKeyPath() string {
return runningConfig.TLS.PrivateKeyPath
}

func ShouldCompressStaticFiles() bool {
return runningConfig.CompressStaticConverted
}

// used by
func GetQueryHostname() string {
return runningConfig.QueryHostname
Expand Down Expand Up @@ -518,6 +522,8 @@ func GetTestConfig(dataPath string) common.Configuration {
QueryHostname: "",
Log: common.LogConfig{LogPrefix: "", LogFileRotationSizeMB: 100, CompressLogFile: false},
TLS: common.TLSConfig{Enabled: false, CertificatePath: "", PrivateKeyPath: ""},
CompressStatic: "false",
CompressStaticConverted: false,
Tracing: common.TracingConfig{ServiceName: "", Endpoint: "", SamplingPercentage: 1},
DatabaseConfig: common.DatabaseConfig{Enabled: true, Provider: "sqlite"},
EmailConfig: common.EmailConfig{SmtpHost: "smtp.gmail.com", SmtpPort: 587, SenderEmail: "[email protected]", GmailAppPassword: " "},
Expand Down Expand Up @@ -792,6 +798,18 @@ func ExtractConfigData(yamlData []byte) (common.Configuration, error) {
config.TLS.PrivateKeyPath = strings.Trim(config.TLS.PrivateKeyPath, "./")
}

if len(config.CompressStatic) <= 0 {
config.CompressStatic = "true"
}
compressStatic, err := strconv.ParseBool(config.CompressStatic)
if err != nil {
compressStatic = true
config.CompressStatic = "true"
log.Errorf("ExtractConfigData: failed to parse compress static flag. Defaulting to %v. Error: %v",
compressStatic, err)
}
config.CompressStaticConverted = compressStatic

// Check for Tracing Config through environment variables
if os.Getenv("TRACESTORE_ENDPOINT") != "" {
config.Tracing.Endpoint = os.Getenv("TRACESTORE_ENDPOINT")
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Test_ExtractConfigData(t *testing.T) {
logPrefix: "./pkg/ingestor/httpserver/"
logFileRotationSizeMB: 100
compressLogFile: false
compressStatic: false
`),
false,
common.Configuration{
Expand Down Expand Up @@ -110,6 +111,8 @@ func Test_ExtractConfigData(t *testing.T) {
DualCaseCheckConverted: true,
SafeServerStart: true,
Log: common.LogConfig{LogPrefix: "./pkg/ingestor/httpserver/", LogFileRotationSizeMB: 100, CompressLogFile: false},
CompressStatic: "false",
CompressStaticConverted: false,
Tracing: common.TracingConfig{Endpoint: "http://localhost:4317", ServiceName: "siglens", SamplingPercentage: 100},
},
},
Expand Down Expand Up @@ -172,6 +175,8 @@ invalidField: "invalid"
DualCaseCheck: "true",
DualCaseCheckConverted: true,
Log: common.LogConfig{LogPrefix: "", LogFileRotationSizeMB: 100, CompressLogFile: false},
CompressStatic: "true",
CompressStaticConverted: true,
Tracing: common.TracingConfig{Endpoint: "", ServiceName: "siglens", SamplingPercentage: 0},
},
},
Expand Down
26 changes: 17 additions & 9 deletions pkg/server/query/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type queryserverCfg struct {
Config config.WebConfig
Addr string
// Log *zap.Logger //ToDo implement debug logger
ln net.Listener
lnTls net.Listener
Router *router.Router
debug bool
ln net.Listener
lnTls net.Listener
Router *router.Router
staticHandler fasthttp.RequestHandler
debug bool
}

var (
Expand All @@ -57,12 +58,19 @@ var (

// ConstructHttpServer new fasthttp server
func ConstructQueryServer(cfg config.WebConfig, ServerAddr string) *queryserverCfg {
staticFs := fasthttp.FS{
Root: "./static",
IndexNames: []string{"index.html"},
Compress: config.ShouldCompressStaticFiles(),
CompressBrotli: config.ShouldCompressStaticFiles(),
}

s := &queryserverCfg{
Config: cfg,
Addr: ServerAddr,
Router: router.New(),
debug: true,
Config: cfg,
Addr: ServerAddr,
Router: router.New(),
staticHandler: staticFs.NewRequestHandler(),
debug: true,
}
return s
}
Expand Down Expand Up @@ -285,7 +293,7 @@ func (hs *queryserverCfg) Run(htmlTemplate *htmltemplate.Template, textTemplate
return
}

fasthttp.ServeFile(ctx, "static/"+filepath)
hs.staticHandler(ctx)
})
}

Expand Down

0 comments on commit bf75587

Please sign in to comment.