about summary refs log tree commit diff stats
path: root/internal/config
diff options
context:
space:
mode:
authorAlan Pearce2024-05-13 19:34:12 +0200
committerAlan Pearce2024-05-13 19:34:12 +0200
commit55efc5bec9703a299de5aac89006ed85600445fc (patch)
tree2e73e77ead7b6ad3820b3622be2b779b369e66ad /internal/config
parent37deedc9b1da92571548c920721984d545269eb4 (diff)
downloadsearchix-55efc5bec9703a299de5aac89006ed85600445fc.tar.lz
searchix-55efc5bec9703a299de5aac89006ed85600445fc.tar.zst
searchix-55efc5bec9703a299de5aac89006ed85600445fc.zip
refactor(config): simplify configuration
commit 8225dbdb692c99b39dcafe2e5ec6ddc4daf08fb5
Author: Alan Pearce <alan@alanpearce.eu>
Date:   Mon May 13 19:18:26 2024 +0200

    refactor: consolidate configuration to reduce command-line options

commit 5616d4c5a9bc6c0c14f744f812fa6609f859dc34
Author: Alan Pearce <alan@alanpearce.eu>
Date:   Mon May 13 17:41:58 2024 +0200

    refactor: move config file parsing to program entry points
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go157
1 files changed, 95 insertions, 62 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index 340b027..c3a5a90 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -26,75 +26,108 @@ func (u *URL) UnmarshalText(text []byte) (err error) {
 	return nil
 }
 
+func mustURL(in string) (u URL) {
+	var err error
+	u.URL, err = url.Parse(in)
+	if err != nil {
+		panic(errors.Errorf("URL cannot be parsed: %s", in))
+	}
+
+	return u
+}
+
+type Web struct {
+	ContentSecurityPolicy CSP
+	ListenAddress         string
+	Port                  string
+	BaseURL               URL
+	SentryDSN             string
+	Environment           string
+	ExtraBodyHTML         template.HTML
+	Headers               map[string]string
+}
+
+type Importer struct {
+	Sources map[string]*Source
+	Timeout time.Duration
+}
+
 type Config struct {
-	DataPath      string        `toml:"data-path"`
-	CSP           CSP           `toml:"content-security-policy"`
-	ExtraBodyHTML template.HTML `toml:"extra-body-html"`
-	Headers       map[string]string
-	Sources       map[string]*Source
+	DataPath string
+	LogLevel slog.Level
+	Web      *Web
+	Importer *Importer
 }
 
 var defaultConfig = Config{
 	DataPath: "./data",
-	CSP: CSP{
-		DefaultSrc: []string{"'self'"},
-	},
-	Headers: map[string]string{
-		"x-content-type-options": "nosniff",
+	Web: &Web{
+		ListenAddress: "localhost",
+		Port:          "3000",
+		BaseURL:       mustURL("http://localhost:3000"),
+		ContentSecurityPolicy: CSP{
+			DefaultSrc: []string{"'self'"},
+		},
+		Headers: map[string]string{
+			"x-content-type-options": "nosniff",
+		},
 	},
-	Sources: map[string]*Source{
-		"nixos": {
-			Name:          "NixOS",
-			Key:           "nixos",
-			Enable:        true,
-			Type:          Channel,
-			Channel:       "nixpkgs",
-			URL:           "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz",
-			ImportPath:    "nixos/release.nix",
-			Attribute:     "options",
-			OutputPath:    "share/doc/nixos/options.json",
-			FetchTimeout:  5 * time.Minute,
-			ImportTimeout: 15 * time.Minute,
-			Repo: Repository{
-				Type:  "github",
-				Owner: "NixOS",
-				Repo:  "nixpkgs",
+	Importer: &Importer{
+		Timeout: 30 * time.Minute,
+		Sources: map[string]*Source{
+			"nixos": {
+				Name:          "NixOS",
+				Key:           "nixos",
+				Enable:        true,
+				Type:          Channel,
+				Channel:       "nixpkgs",
+				URL:           "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz",
+				ImportPath:    "nixos/release.nix",
+				Attribute:     "options",
+				OutputPath:    "share/doc/nixos/options.json",
+				FetchTimeout:  5 * time.Minute,
+				ImportTimeout: 15 * time.Minute,
+				Repo: Repository{
+					Type:  "github",
+					Owner: "NixOS",
+					Repo:  "nixpkgs",
+				},
 			},
-		},
-		"darwin": {
-			Name:          "Darwin",
-			Key:           "darwin",
-			Enable:        false,
-			Type:          Channel,
-			Channel:       "darwin",
-			URL:           "https://github.com/LnL7/nix-darwin/archive/master.tar.gz",
-			ImportPath:    "release.nix",
-			Attribute:     "options",
-			OutputPath:    "share/doc/darwin/options.json",
-			FetchTimeout:  5 * time.Minute,
-			ImportTimeout: 15 * time.Minute,
-			Repo: Repository{
-				Type:  "github",
-				Owner: "LnL7",
-				Repo:  "nix-darwin",
+			"darwin": {
+				Name:          "Darwin",
+				Key:           "darwin",
+				Enable:        false,
+				Type:          Channel,
+				Channel:       "darwin",
+				URL:           "https://github.com/LnL7/nix-darwin/archive/master.tar.gz",
+				ImportPath:    "release.nix",
+				Attribute:     "options",
+				OutputPath:    "share/doc/darwin/options.json",
+				FetchTimeout:  5 * time.Minute,
+				ImportTimeout: 15 * time.Minute,
+				Repo: Repository{
+					Type:  "github",
+					Owner: "LnL7",
+					Repo:  "nix-darwin",
+				},
 			},
-		},
-		"home-manager": {
-			Name:          "Home Manager",
-			Key:           "home-manager",
-			Enable:        false,
-			Channel:       "home-manager",
-			URL:           "https://github.com/nix-community/home-manager/archive/master.tar.gz",
-			Type:          Channel,
-			ImportPath:    "default.nix",
-			Attribute:     "docs.json",
-			OutputPath:    "share/doc/home-manager/options.json",
-			FetchTimeout:  5 * time.Minute,
-			ImportTimeout: 15 * time.Minute,
-			Repo: Repository{
-				Type:  "github",
-				Owner: "nix-community",
-				Repo:  "home-manager",
+			"home-manager": {
+				Name:          "Home Manager",
+				Key:           "home-manager",
+				Enable:        false,
+				Channel:       "home-manager",
+				URL:           "https://github.com/nix-community/home-manager/archive/master.tar.gz",
+				Type:          Channel,
+				ImportPath:    "default.nix",
+				Attribute:     "docs.json",
+				OutputPath:    "share/doc/home-manager/options.json",
+				FetchTimeout:  5 * time.Minute,
+				ImportTimeout: 15 * time.Minute,
+				Repo: Repository{
+					Type:  "github",
+					Owner: "nix-community",
+					Repo:  "home-manager",
+				},
 			},
 		},
 	},
@@ -122,7 +155,7 @@ func GetConfig(filename string) (*Config, error) {
 		}
 	}
 
-	maps.DeleteFunc(config.Sources, func(_ string, v *Source) bool {
+	maps.DeleteFunc(config.Importer.Sources, func(_ string, v *Source) bool {
 		return !v.Enable
 	})