diff --git a/config/config.go b/config/config.go index 816977a..3db9d8a 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,8 @@ package config import ( + "encoding/json" + "errors" "log" "gopkg.in/yaml.v2" @@ -30,7 +32,7 @@ type Config struct { ConfigVersion int `yaml:"config_version" json:"config_version"` } -func DefaultConfig() Config { +func DefaultConfig() *Config { defaultConfig := Config{} stdProfile := DownloadProfile{Name: "standard youtube-dl video", Command: "youtube-dl", Args: []string{ "--newline", @@ -40,6 +42,8 @@ func DefaultConfig() Config { }} defaultConfig.DownloadProfiles = append(defaultConfig.DownloadProfiles, stdProfile) + defaultConfig.DownloadProfiles = append(defaultConfig.DownloadProfiles, stdProfile) + defaultConfig.Server.Port = 6123 defaultConfig.Server.Address = "http://localhost:6123" defaultConfig.Server.DownloadPath = "./" @@ -49,7 +53,25 @@ func DefaultConfig() Config { defaultConfig.ConfigVersion = 1 - return defaultConfig + return &defaultConfig +} + +func (c *Config) UpdateFromJSON(j []byte) error { + newConfig := Config{} + err := json.Unmarshal(j, &newConfig) + if err != nil { + log.Printf("Unmarshal error in config: %v", err) + return err + } + log.Printf("Config is unmarshalled ok") + + // other checks + if newConfig.UI.PopupHeight < 100 || newConfig.UI.PopupHeight > 2000 { + return errors.New("bad popup height") + } + + *c = newConfig + return nil } func WriteDefaultConfig(path string) { diff --git a/main.go b/main.go index ba9a150..e5ef62b 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,7 @@ var versionInfo = version.Info{CurrentVersion: "v0.5.0"} //go:embed web var webFS embed.FS -var conf config.Config +var conf *config.Config func main() { conf = config.DefaultConfig() @@ -131,7 +131,28 @@ func ConfigHandler(w http.ResponseWriter, r *http.Request) { } func ConfigRESTHandler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) + + type errorResponse struct { + Error string `json:"error"` + } + + if r.Method == "POST" { + log.Printf("Updating config") + b, err := io.ReadAll(r.Body) + if err != nil { + panic(err) + } + log.Printf("Got this request:", string(b)) + err = conf.UpdateFromJSON(b) + + if err != nil { + errorRes := errorResponse{Error: err.Error()} + errorResB, _ := json.Marshal(errorRes) + w.WriteHeader(400) + w.Write(errorResB) + return + } + } b, _ := json.Marshal(conf) w.Write(b) diff --git a/version/version.go b/version/version.go index f2c5dd5..3981920 100644 --- a/version/version.go +++ b/version/version.go @@ -24,7 +24,8 @@ func (i *Info) UpdateGitHubVersion() error { versionUrl := "https://api.github.com/repos/tardisx/gropple/releases" resp, err := http.Get(versionUrl) if err != nil { - log.Fatal("Error getting response. ", err) + log.Printf("Error getting response. ", err) + return err } defer resp.Body.Close() @@ -65,11 +66,11 @@ func (i *Info) canUpgrade() bool { log.Printf("We are %s, github is %s", i.CurrentVersion, i.GithubVersion) if !semver.IsValid(i.CurrentVersion) { - log.Fatalf("current version %s is invalid", i.CurrentVersion) + log.Printf("current version %s is invalid", i.CurrentVersion) } if !semver.IsValid(i.GithubVersion) { - log.Fatalf("github version %s is invalid", i.GithubVersion) + log.Printf("github version %s is invalid", i.GithubVersion) } if semver.Compare(i.CurrentVersion, i.GithubVersion) == -1 { diff --git a/web/config.html b/web/config.html index 6fbdd07..ff5085f 100644 --- a/web/config.html +++ b/web/config.html @@ -5,6 +5,8 @@

gropple config

+

+

Upgrade is available - you have @@ -21,7 +23,7 @@ Server - + The port the web server will listen on. @@ -38,11 +40,11 @@ UI - + The width of popup windows in pixels. - + The height of popup windows in pixels. @@ -50,21 +52,49 @@

-
+
Download Profiles - - - The name of this profile. For your information only. +
- - +
+
+ +
+
+ @@ -74,7 +104,10 @@