9a80cdb798a1af8a5af97370c71a91c2822d45e2

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

feat(config): store server config in file

Diff

This diff is truncated to protect this page.

  1diff --git a/server/config/config.go b/server/config/config.go
  2index 8595fe48a599007d4425bfcfcd5ea1e75519309a..649ff70929e8ad6edac925d33258944ecb5b5ecf 100644
  3--- a/server/config/config.go
  4+++ b/server/config/config.go
  5@@ -1,97 +1,148 @@
  6 package config
  7 
  8 import (
  9+	"os"
 10 	"path/filepath"
 11 
 12 	"github.com/caarlos0/env/v6"
 13 	"github.com/charmbracelet/log"
 14 	"github.com/charmbracelet/soft-serve/server/backend"
 15+	"gopkg.in/yaml.v3"
 16 )
 17 
 18 // SSHConfig is the configuration for the SSH server.
 19 type SSHConfig struct {
 20 	// ListenAddr is the address on which the SSH server will listen.
 21-	ListenAddr string `env:"LISTEN_ADDR" envDefault:":23231"`
 22+	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 23 
 24 	// PublicURL is the public URL of the SSH server.
 25-	PublicURL string `env:"PUBLIC_URL" envDefault:"ssh://localhost:23231"`
 26+	PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
 27 
 28 	// KeyPath is the path to the SSH server's private key.
 29-	KeyPath string `env:"KEY_PATH"`
 30+	KeyPath string `env:"KEY_PATH" yaml:"key_path"`
 31 
 32 	// InternalKeyPath is the path to the SSH server's internal private key.
 33-	InternalKeyPath string `env:"INTERNAL_KEY_PATH"`
 34+	InternalKeyPath string `env:"INTERNAL_KEY_PATH" yaml:"internal_key_path"`
 35 
 36 	// MaxTimeout is the maximum number of seconds a connection can take.
 37-	MaxTimeout int `env:"MAX_TIMEOUT" envDefault:"0"`
 38+	MaxTimeout int `env:"MAX_TIMEOUT" yaml:"max_timeout`
 39 
 40 	// IdleTimeout is the number of seconds a connection can be idle before it is closed.
 41-	IdleTimeout int `env:"IDLE_TIMEOUT" envDefault:"120"`
 42+	IdleTimeout int `env:"IDLE_TIMEOUT" yaml:"idle_timeout"`
 43 }
 44 
 45 // GitConfig is the Git daemon configuration for the server.
 46 type GitConfig struct {
 47 	// ListenAddr is the address on which the Git daemon will listen.
 48-	ListenAddr string `env:"LISTEN_ADDR" envDefault:":9418"`
 49+	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 50 
 51 	// MaxTimeout is the maximum number of seconds a connection can take.
 52-	MaxTimeout int `env:"MAX_TIMEOUT" envDefault:"0"`
 53+	MaxTimeout int `env:"MAX_TIMEOUT" yaml:"max_timeout"`
 54 
 55 	// IdleTimeout is the number of seconds a connection can be idle before it is closed.
 56-	IdleTimeout int `env:"IDLE_TIMEOUT" envDefault:"3"`
 57+	IdleTimeout int `env:"IDLE_TIMEOUT" yaml:"idle_timeout"`
 58 
 59 	// MaxConnections is the maximum number of concurrent connections.
 60-	MaxConnections int `env:"MAX_CONNECTIONS" envDefault:"32"`
 61+	MaxConnections int `env:"MAX_CONNECTIONS" yaml:"max_connections"`
 62 }
 63 
 64 // HTTPConfig is the HTTP configuration for the server.
 65 type HTTPConfig struct {
 66 	// ListenAddr is the address on which the HTTP server will listen.
 67-	ListenAddr string `env:"LISTEN_ADDR" envDefault:":8080"`
 68+	ListenAddr string `env:"LISTEN_ADDR" yaml:"listen_addr"`
 69 
 70 	// PublicURL is the public URL of the HTTP server.
 71-	PublicURL string `env:"PUBLIC_URL" envDefault:"http://localhost:8080"`
 72+	PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
 73 }
 74 
 75 // Config is the configuration for Soft Serve.
 76 type Config struct {
 77 	// Name is the name of the server.
 78-	Name string `env:"NAME" envDefault:"Soft Serve"`
 79+	Name string `env:"NAME" yaml:"name"`
 80 
 81 	// SSH is the configuration for the SSH server.
 82-	SSH SSHConfig `envPrefix:"SSH_"`
 83+	SSH SSHConfig `envPrefix:"SSH_" yaml:"ssh"`
 84 
 85 	// Git is the configuration for the Git daemon.
 86-	Git GitConfig `envPrefix:"GIT_"`
 87+	Git GitConfig `envPrefix:"GIT_" yaml:"git"`
 88 
 89 	// HTTP is the configuration for the HTTP server.
 90-	HTTP HTTPConfig `envPrefix:"HTTP_"`
 91+	HTTP HTTPConfig `envPrefix:"HTTP_" yaml:"http"`
 92 
 93 	// InitialAdminKeys is a list of public keys that will be added to the list of admins.
 94-	InitialAdminKeys []string `env:"INITIAL_ADMIN_KEY" envSeparator:"\n"`
 95+	InitialAdminKeys []string `env:"INITIAL_ADMIN_KEY" envSeparator:"\n" yaml:"initial_admin_keys"`
 96 
 97 	// DataPath is the path to the directory where Soft Serve will store its data.
 98-	DataPath string `env:"DATA_PATH" envDefault:"data"`
 99+	DataPath string `env:"DATA_PATH" envDefault:"data" yaml:"-"`
100 
101 	// Backend is the Git backend to use.
102-	Backend backend.Backend
103+	Backend backend.Backend `yaml:"-"`
104+}
105diff --git a/server/config/file.go b/server/config/file.go
106new file mode 100644
107index 0000000000000000000000000000000000000000..00baa13154babf334df97282b6bbbf121e8f5644
108--- /dev/null
109+++ b/server/config/file.go
110@@ -0,0 +1,68 @@
111+package config
112+
113+import (
114+	"bytes"
115+	"text/template"
116+)
117+
118+var (
119+	configFileTmpl = template.Must(template.New("config").Parse(`# Soft Serve Server configurations
120+
121+# The name of the server.
122+# This is the name that will be displayed in the UI.
123+name: "{{ .Name }}"
124+
125+# The SSH server configuration.
126+ssh:
127+  # The address on which the SSH server will listen.
128+  listen_addr: "{{ .SSH.ListenAddr }}"
129+
130+  # The public URL of the SSH server.
131+  # This is the address will be used to clone repositories.
132+  public_url: "{{ .SSH.PublicURL }}"
133+
134+  # The relative path to the SSH server's private key.
135+  key_path: "{{ .SSH.KeyPath }}"
136+
137+  # The relative path to the SSH server's internal api private key.
138+  internal_key_path: "{{ .SSH.InternalKeyPath }}"
139+
140+  # The maximum number of seconds a connection can take.
141+  # A value of 0 means no timeout.
142+  max_timeout: {{ .SSH.MaxTimeout }}
143+
144+  # The number of seconds a connection can be idle before it is closed.
145+  idle_timeout: {{ .SSH.IdleTimeout }}
146+
147+# The Git daemon configuration.
148+git:
149+  # The address on which the Git daemon will listen.
150+  listen_addr: "{{ .Git.ListenAddr }}"
151+
152+  # The maximum number of seconds a connection can take.
153+  # A value of 0 means no timeout.
154+  max_timeout: {{ .Git.MaxTimeout }}
155+
156+  # The number of seconds a connection can be idle before it is closed.
157+  idle_timeout: {{ .Git.IdleTimeout }}
158+
159+  # The maximum number of concurrent connections.
160+  max_connections: {{ .Git.MaxConnections }}
161+
162+# The HTTP server configuration.
163+http:
164+  # The address on which the HTTP server will listen.
165+  listen_addr: "{{ .HTTP.ListenAddr }}"
166+
167+  # The public URL of the HTTP server.
168+  # This is the address will be used to clone repositories.
169+  public_url: "{{ .HTTP.PublicURL }}"
170+
171+`))
172+)
173+
174+func newConfigFile(cfg *Config) string {
175+	var b bytes.Buffer
176+	configFileTmpl.Execute(&b, cfg)
177+	return b.String()
178+}