Skip to content

Commit

Permalink
add get config endpoint and some minor fixes on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
ross96D committed Nov 11, 2024
1 parent 92ca23a commit 4048e79
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
30 changes: 25 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *Server) setHandlers() {
s.router.Group(func(r chi.Router) {
r.Use(auth.AuthMiddelware)
r.Get("/list", List)
r.Get("/config", Config)
r.Group(func(r chi.Router) {
r.Use(logger.ResponseWithLogger)
r.Post("/update", Update)
Expand Down Expand Up @@ -93,21 +94,40 @@ func Upgrade(w http.ResponseWriter, r *http.Request) {
os.Exit(1)
}

func Config(w http.ResponseWriter, r *http.Request) {
data, err := share.ReadConfigFile()
if err != nil {
log.Error().Err(err).Send()
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(200)
_, err = w.Write(data)
if err != nil {
log.Error().Err(err).Msg("sending config file")
}
}

func Reload(w http.ResponseWriter, r *http.Request) {
data, err := io.ReadAll(r.Body)
if err != nil {
log.Error().Err(err).Send()
http.Error(w, err.Error(), 500)
return
}

// TODO is this right? i am not shure because this is only usefull on testing.. maybe use a header to confirm that we want to use the default config file
if len(data) == 0 {
err = share.Reload("config.cue")
} else {
err = share.ReloadString(string(data))
http.Error(w, "invalid: emtpy config", 500)
return
}

err = share.ReloadString(string(data))
if err != nil {
http.Error(w, err.Error(), 400)
return
}
err = share.ReplaceConfigFile(data)
if err != nil {
log.Error().Err(err).Msg("replacing config file")
http.Error(w, err.Error(), 500)
return
}
Expand Down
20 changes: 20 additions & 0 deletions share/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ import (
)

var config configuration.Configuration
var configPath string

func ConfigPath() string {
return configPath
}

func ReplaceConfigFile(data []byte) error {
f, err := os.Create(configPath)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
return err
}

var DefaultPath string = "nothing for now"

Expand All @@ -28,6 +43,7 @@ func Init(path string) error {
if err = changeConfig(newConfig); err != nil {
return err
}
configPath = path
return nil
}

Expand Down Expand Up @@ -227,6 +243,10 @@ func ReloadString(data string) error {
return changeConfig(newConfig)
}

func ReadConfigFile() ([]byte, error) {
return os.ReadFile(configPath)
}

func Reload(path string) error {
newConfig, err := configuration.Load(path)
if err != nil {
Expand Down

0 comments on commit 4048e79

Please sign in to comment.