Monorepo for Tangled tangled.org

spindle/config: add abilility to override motd #906

merged opened by evan.jarrett.net targeting master from evan.jarrett.net/core: spindle-motd
Labels

None yet.

assignee

None yet.

Participants 3
AT URI
at://did:plc:pddp4xt5lgnv2qsegbzzs4xg/sh.tangled.repo.pull/3mahvraqqgz22
+21 -12
Interdiff #0 โ†’ #1
-1
spindle/config/config.go
··· 20 LogDir string `env:"LOG_DIR, default=/var/log/spindle"` 21 QueueSize int `env:"QUEUE_SIZE, default=100"` 22 MaxJobCount int `env:"MAX_JOB_COUNT, default=2"` // max number of jobs that run at a time 23 - MOTDFile string `env:"MOTD_FILE"` // optional path to custom MOTD file 24 } 25 26 func (s Server) Did() syntax.DID {
··· 20 LogDir string `env:"LOG_DIR, default=/var/log/spindle"` 21 QueueSize int `env:"QUEUE_SIZE, default=100"` 22 MaxJobCount int `env:"MAX_JOB_COUNT, default=2"` // max number of jobs that run at a time 23 } 24 25 func (s Server) Did() syntax.DID {
+21 -11
spindle/server.go
··· 8 "log/slog" 9 "maps" 10 "net/http" 11 - "os" 12 13 "github.com/go-chi/chi/v5" 14 "tangled.org/core/api/tangled" ··· 31 ) 32 33 //go:embed motd 34 - var motd []byte 35 36 const ( 37 rbacDomain = "thisserver" ··· 48 cfg *config.Config 49 ks *eventconsumer.Consumer 50 res *idresolver.Resolver 51 - vault secrets.Manager 52 } 53 54 // New creates a new Spindle server with the provided configuration and engines. ··· 129 cfg: cfg, 130 res: resolver, 131 vault: vault, 132 } 133 134 err = e.AddSpindle(rbacDomain) ··· 202 return s.e 203 } 204 205 // Start starts the Spindle server (blocking). 206 func (s *Spindle) Start(ctx context.Context) error { 207 // starts a job queue runner in the background ··· 247 mux := chi.NewRouter() 248 249 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 250 - if s.cfg.Server.MOTDFile != "" { 251 - if content, err := os.ReadFile(s.cfg.Server.MOTDFile); err == nil { 252 - w.Write(content) 253 - return 254 - } 255 - s.l.Warn("failed to read custom MOTD file, using default", "path", s.cfg.Server.MOTDFile) 256 - } 257 - w.Write(motd) 258 }) 259 mux.HandleFunc("/events", s.Events) 260 mux.HandleFunc("/logs/{knot}/{rkey}/{name}", s.Logs)
··· 8 "log/slog" 9 "maps" 10 "net/http" 11 + "sync" 12 13 "github.com/go-chi/chi/v5" 14 "tangled.org/core/api/tangled" ··· 31 ) 32 33 //go:embed motd 34 + var defaultMotd []byte 35 36 const ( 37 rbacDomain = "thisserver" ··· 48 cfg *config.Config 49 ks *eventconsumer.Consumer 50 res *idresolver.Resolver 51 + vault secrets.Manager 52 + motd []byte 53 + motdMu sync.RWMutex 54 } 55 56 // New creates a new Spindle server with the provided configuration and engines. ··· 131 cfg: cfg, 132 res: resolver, 133 vault: vault, 134 + motd: defaultMotd, 135 } 136 137 err = e.AddSpindle(rbacDomain) ··· 205 return s.e 206 } 207 208 + // SetMotdContent sets custom MOTD content, replacing the embedded default. 209 + func (s *Spindle) SetMotdContent(content []byte) { 210 + s.motdMu.Lock() 211 + defer s.motdMu.Unlock() 212 + s.motd = content 213 + } 214 + 215 + // GetMotdContent returns the current MOTD content. 216 + func (s *Spindle) GetMotdContent() []byte { 217 + s.motdMu.RLock() 218 + defer s.motdMu.RUnlock() 219 + return s.motd 220 + } 221 + 222 // Start starts the Spindle server (blocking). 223 func (s *Spindle) Start(ctx context.Context) error { 224 // starts a job queue runner in the background ··· 264 mux := chi.NewRouter() 265 266 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 267 + w.Write(s.GetMotdContent()) 268 }) 269 mux.HandleFunc("/events", s.Events) 270 mux.HandleFunc("/logs/{knot}/{rkey}/{name}", s.Logs)

History

2 rounds 7 comments
sign up or login to add to the discussion
1 commit
expand
spindle/config: add abilility to override motd
expand 4 comments

Instead of setting the files via the config, what I really want is to just set the byte content directly. I have updated the PR to reflect this.

how is the motd overridden in this scenario? SetMotd is not being called anywhere.

Its not called in tangled core. I'm using tangled as a libary and I was opening it as a way for me to set the motd directly with a []byte, rather than go through the original route I was going to do by setting the MOTD_FILE in the config. I can still go that route, but seems like its going to refactored based on boltless's commit to add datadir.

sounds good, lgtm!

pull request successfully merged
1 commit
expand
spindle/config: add abilility to override motd
expand 3 comments

Thank you for the contribution!

I'd prefer motd file path and db path to be fixed under configurable spindle data dir (see commit), so I might eventually change the config schema later.

But this seems good to merge for now!

I'm using the config directly and specifying my own path to an embedded motd

	if _, err := motdFile.Write(motd); err != nil {
		setupLog.Error(err, "failed to write MOTD temp file")
		os.Exit(1)
	}
	motdFile.Close()
	spindleCfg.Server.MOTDFile = motdFile.Name()

If so, you can do same for {cfg.DataDir}/motd file.

// spindle/config.go
func (s Server) MotdPath() string {
	return filepath.Join(s.DataDir, "motd")
}

// ...
	if err := os.WriteFile(path, data, 0644); err != nil {
		setupLog.Error(err, "failed to write MOTD file")
		os.Exit(1)
	}

Just write embedded motd bytes to {cfg.DataDir}/motd file.