From 9437f522c2ed21950acde884cafc369ca45f4b7b Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 15 May 2024 20:16:33 +0200 Subject: style: use consistent naming for config value --- internal/server/mux.go | 42 +++++++++++++++++++++--------------------- searchix.go | 16 ++++++++-------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/internal/server/mux.go b/internal/server/mux.go index c1f36fe..f9eaf03 100644 --- a/internal/server/mux.go +++ b/internal/server/mux.go @@ -68,26 +68,26 @@ var versionInfo = &VersionInfo{ var templates TemplateCollection -func applyDevModeOverrides(config *config.Config) { - if len(config.Web.ContentSecurityPolicy.ScriptSrc) == 0 { - config.Web.ContentSecurityPolicy.ScriptSrc = config.Web.ContentSecurityPolicy.DefaultSrc +func applyDevModeOverrides(cfg *config.Config) { + if len(cfg.Web.ContentSecurityPolicy.ScriptSrc) == 0 { + cfg.Web.ContentSecurityPolicy.ScriptSrc = cfg.Web.ContentSecurityPolicy.DefaultSrc } - config.Web.ContentSecurityPolicy.ScriptSrc = append( - config.Web.ContentSecurityPolicy.ScriptSrc, + cfg.Web.ContentSecurityPolicy.ScriptSrc = append( + cfg.Web.ContentSecurityPolicy.ScriptSrc, "'unsafe-inline'", ) } func NewMux( - config *config.Config, + cfg *config.Config, index *search.ReadIndex, liveReload bool, ) (*http.ServeMux, error) { err := sentry.Init(sentry.ClientOptions{ EnableTracing: true, TracesSampleRate: 1.0, - Dsn: config.Web.SentryDSN, - Environment: config.Web.Environment, + Dsn: cfg.Web.SentryDSN, + Environment: cfg.Web.Environment, }) if err != nil { return nil, errors.WithMessage(err, "could not set up sentry") @@ -102,14 +102,14 @@ func NewMux( log.Panicf("could not load templates: %v", err) } - errorHandler := createErrorHandler(config) + errorHandler := createErrorHandler(cfg) top := http.NewServeMux() mux := http.NewServeMux() mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) { indexData := TemplateData{ - ExtraHeadHTML: config.Web.ExtraHeadHTML, - Sources: config.Importer.Sources, + ExtraHeadHTML: cfg.Web.ExtraHeadHTML, + Sources: cfg.Importer.Sources, Version: *versionInfo, } err := templates["index"].Execute(w, indexData) @@ -122,7 +122,7 @@ func NewMux( mux.HandleFunc("/options/{source}/search", func(w http.ResponseWriter, r *http.Request) { sourceKey := r.PathValue("source") - source := config.Importer.Sources[sourceKey] + source := cfg.Importer.Sources[sourceKey] if source == nil { errorHandler(w, r, "Source not found", http.StatusNotFound) @@ -155,9 +155,9 @@ func NewMux( tdata := ResultData[options.NixOption]{ TemplateData: TemplateData{ - ExtraHeadHTML: config.Web.ExtraHeadHTML, + ExtraHeadHTML: cfg.Web.ExtraHeadHTML, Source: *source, - Sources: config.Importer.Sources, + Sources: cfg.Importer.Sources, Version: *versionInfo, }, ResultsPerPage: search.ResultsPerPage, @@ -215,8 +215,8 @@ func NewMux( } err = templates["search"].Execute(w, TemplateData{ - ExtraHeadHTML: config.Web.ExtraHeadHTML, - Sources: config.Importer.Sources, + ExtraHeadHTML: cfg.Web.ExtraHeadHTML, + Sources: cfg.Importer.Sources, Source: *source, SourceResult: sourceResult, Version: *versionInfo, @@ -232,8 +232,8 @@ func NewMux( mux.Handle("/static/", http.FileServer(http.FS(frontend.Files))) if liveReload { - applyDevModeOverrides(config) - config.Web.ExtraHeadHTML = jsSnippet + applyDevModeOverrides(cfg) + cfg.Web.ExtraHeadHTML = jsSnippet liveReload := livereload.New() liveReload.Start() top.Handle("/livereload", liveReload) @@ -258,7 +258,7 @@ func NewMux( } var logWriter io.Writer - if config.Web.Environment == "production" { + if cfg.Web.Environment == "production" { logWriter = law.NewWriteAsyncer(os.Stdout, nil) } else { logWriter = os.Stdout @@ -267,11 +267,11 @@ func NewMux( AddHeadersMiddleware( sentryHandler.Handle( wrapHandlerWithLogging(mux, wrappedHandlerOptions{ - defaultHostname: config.Web.BaseURL.Hostname(), + defaultHostname: cfg.Web.BaseURL.Hostname(), logger: logWriter, }), ), - config, + cfg, ), ) // no logging, no sentry diff --git a/searchix.go b/searchix.go index 8dca470..9cdd67d 100644 --- a/searchix.go +++ b/searchix.go @@ -54,25 +54,25 @@ func main() { os.Exit(0) } - conf, err := config.GetConfig(*configFile) + cfg, err := config.GetConfig(*configFile) if err != nil { log.Panicf("error parsing configuration file: %v", err) } - slog.SetLogLoggerLevel(conf.LogLevel) - if conf.Web.Environment == "production" { + slog.SetLogLoggerLevel(cfg.LogLevel) + if cfg.Web.Environment == "production" { log.SetFlags(0) } else { log.SetFlags(log.LstdFlags) } - read, write, exists, err := index.OpenOrCreate(conf.DataPath, *replace) + read, write, exists, err := index.OpenOrCreate(cfg.DataPath, *replace) if err != nil { log.Fatalf("Failed to open or create index: %v", err) } if !exists { slog.Info("Index doesn't exist. Starting build job...") - err = importer.Start(conf, write, *replace) + err = importer.Start(cfg, write, *replace) if err != nil { log.Fatalf("Failed to build index: %v", err) } @@ -80,7 +80,7 @@ func main() { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt) - sv, err := server.New(conf, read, *liveReload) + sv, err := server.New(cfg, read, *liveReload) if err != nil { log.Fatalf("error setting up server: %v", err) } @@ -95,13 +95,13 @@ func main() { }() go func() { - nextRun := nextOccurrenceOfLocalTime(conf.Importer.UpdateAt) + nextRun := nextOccurrenceOfLocalTime(cfg.Importer.UpdateAt) for { slog.Debug("scheduling next run", "next-run", nextRun) <-time.After(time.Until(nextRun)) wg.Add(1) slog.Info("updating index") - err = importer.Start(conf, write, false) + err = importer.Start(cfg, write, false) wg.Done() if err != nil { slog.Warn("error updating index", "error", err) -- cgit 1.4.1