From 2aba19770f4c5119cd72a04704c6950ece552d8f Mon Sep 17 00:00:00 2001
From: Justin Hawkins
Date: Tue, 28 Sep 2021 22:09:12 +0930
Subject: [PATCH] Start of the web frontend and backend for config handling.
---
config/config.go | 29 ++++++++-------
main.go | 32 ++++++++++++++---
web/config.html | 92 ++++++++++++++++++++++++++++++++++++++++++++++++
web/index.html | 6 ++--
web/popup.html | 2 +-
5 files changed, 141 insertions(+), 20 deletions(-)
create mode 100644 web/config.html
diff --git a/config/config.go b/config/config.go
index 832d164..816977a 100644
--- a/config/config.go
+++ b/config/config.go
@@ -7,27 +7,27 @@ import (
)
type Server struct {
- Port int `yaml:"port"`
- Address string `yaml:"address"`
- DownloadPath string `yaml:"download_path"`
+ Port int `yaml:"port" json:"port"`
+ Address string `yaml:"address" json:"address"`
+ DownloadPath string `yaml:"download_path" json:"download_path"`
}
type DownloadProfile struct {
- Name string `yaml:"name"`
- Command string `yaml:"command"`
- Args []string `yaml:"args"`
+ Name string `yaml:"name" json:"name"`
+ Command string `yaml:"command" json:"command"`
+ Args []string `yaml:"args" json:"args"`
}
type UI struct {
- PopupWidth int `yaml:"popup_width"`
- PopupHeight int `yaml:"popup_height"`
+ PopupWidth int `yaml:"popup_width" json:"popup_width"`
+ PopupHeight int `yaml:"popup_height" json:"popup_height"`
}
type Config struct {
- Server Server `yaml:"server"`
- UI UI `yaml:"ui"`
- DownloadProfiles []DownloadProfile `yaml:"profiles"`
- ConfigVersion int `yaml:"config_version"`
+ Server Server `yaml:"server" json:"server"`
+ UI UI `yaml:"ui" json:"ui"`
+ DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"`
+ ConfigVersion int `yaml:"config_version" json:"config_version"`
}
func DefaultConfig() Config {
@@ -41,11 +41,14 @@ func DefaultConfig() Config {
defaultConfig.DownloadProfiles = append(defaultConfig.DownloadProfiles, stdProfile)
defaultConfig.Server.Port = 6123
- defaultConfig.Server.Address = "localhost:6123"
+ defaultConfig.Server.Address = "http://localhost:6123"
defaultConfig.Server.DownloadPath = "./"
+
defaultConfig.UI.PopupWidth = 500
defaultConfig.UI.PopupHeight = 500
+ defaultConfig.ConfigVersion = 1
+
return defaultConfig
}
diff --git a/main.go b/main.go
index 03b0620..ba9a150 100644
--- a/main.go
+++ b/main.go
@@ -37,7 +37,7 @@ type download struct {
var downloads []*download
var downloadId = 0
-var versionInfo = version.Info{CurrentVersion: "v0.4.0"}
+var versionInfo = version.Info{CurrentVersion: "v0.5.0"}
//go:embed web
var webFS embed.FS
@@ -49,10 +49,13 @@ func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
+ r.HandleFunc("/config", ConfigHandler)
r.HandleFunc("/fetch", FetchHandler)
- r.HandleFunc("/fetch/info", FetchInfoHandler)
- r.HandleFunc("/fetch/info/{id}", FetchInfoOneHandler)
- r.HandleFunc("/version", VersionHandler)
+
+ r.HandleFunc("/rest/fetch/info", FetchInfoHandler)
+ r.HandleFunc("/rest/fetch/info/{id}", FetchInfoOneHandler)
+ r.HandleFunc("/rest/version", VersionHandler)
+ r.HandleFunc("/rest/config", ConfigRESTHandler)
http.Handle("/", r)
@@ -113,6 +116,27 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
}
+func ConfigHandler(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+
+ t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/config.html")
+ if err != nil {
+ panic(err)
+ }
+
+ err = t.ExecuteTemplate(w, "layout", nil)
+ if err != nil {
+ panic(err)
+ }
+}
+
+func ConfigRESTHandler(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+
+ b, _ := json.Marshal(conf)
+ w.Write(b)
+}
+
func FetchInfoOneHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
idString := vars["id"]
diff --git a/web/config.html b/web/config.html
new file mode 100644
index 0000000..6fbdd07
--- /dev/null
+++ b/web/config.html
@@ -0,0 +1,92 @@
+{{ define "content" }}
+
+
+
+
+{{ end }}
+
+{{ define "js" }}
+
+{{ end }}
\ No newline at end of file
diff --git a/web/index.html b/web/index.html
index 2aa693f..9fc483f 100644
--- a/web/index.html
+++ b/web/index.html
@@ -19,6 +19,8 @@
+ config
+
@@ -53,7 +55,7 @@
return {
items: [], version: {},
fetch_version() {
- fetch('/version')
+ fetch('/rest/version')
.then(response => response.json())
.then(info => {
this.version = info;
@@ -65,7 +67,7 @@
});
},
fetch_data() {
- fetch('/fetch/info')
+ fetch('/rest/fetch/info')
.then(response => response.json())
.then(info => {
// will be null if no downloads yet
diff --git a/web/popup.html b/web/popup.html
index 51d689b..b100f07 100644
--- a/web/popup.html
+++ b/web/popup.html
@@ -22,7 +22,7 @@
return {
eta: '', percent: 0.0, state: '??', filename: '', finished: false, log :'',
fetch_data() {
- fetch('/fetch/info/{{ .Id }}')
+ fetch('/rest/fetch/info/{{ .Id }}')
.then(response => response.json())
.then(info => {
this.eta = info.eta;